Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach debugger to step into native (C++) code from a managed (C#) wrapper?

I have a wrapper around a C++ function call which I call from C# code. How do I attach a debugger in Visual Studio to step into the native C++ code?

This is the wrapper that I have which calls GetData() defined in a C++ file:

    [DllImport("Unmanaged.dll", CallingConvention=CallingConvention.Cdecl, 
               EntryPoint = "GetData", BestFitMapping = false)]
        public static extern String GetData(String url);

The code is crashing and I want to investigate the root cause.

Thanks, Nikhil

like image 542
Nikhil Avatar asked Sep 11 '08 22:09

Nikhil


People also ask

How do you attach code or debugger?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.

What is managed debugging?

Managed debugging assistants (MDAs) are debugging aids that work with the common language runtime (CLR) to provide information on runtime state. The assistants generate informational messages about runtime events that you cannot otherwise trap.

How do I debug a remote process in Visual Studio?

You can attach the Visual Studio debugger to a running process on a local or remote computer. After the process is running, select Debug > Attach to Process or press Ctrl+Alt+p in Visual Studio, and use the Attach to Process dialog to attach the debugger to the process.


1 Answers

Check the Debug tab on your project's properties page. There should be an "Enable unmanaged code debugging" checkbox. This worked for me when we developed a new .NET UI for our old c++ DLLs.

If your unmanaged DLL is being built from another project (for a while ours were being built using VS6) just make sure you have the DLL's pdb file handy for the debugging.

The other approach is to use the C# exe as the target exe to run from the DLL project, you can then debug your DLL normally.

like image 126
Lou Avatar answered Sep 23 '22 22:09

Lou