28

I wonder if there is any way to debug c++ dll called from C# PInvoke in VS 2010. I tried to attach the project into c# application but it didn't work - didn't stop at a break point.

I also tried to record anything with OutputDebugString in C++ project but nothing printed with PInvoke call. Despite these issues, the actual function runs well.

Any advice will be appreciated.

Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
  • This might help, but it looks like you'd need the .pdb file to do anything meaningful: http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/91232ee3-a70c-43e4-b9f2-f8ff860c60ca/ – Jeff Sep 20 '12 at 16:40
  • @JeffE Of course, the project has .pdb file but it didn't matter. See Hans's answer. – Tae-Sung Shin Sep 20 '12 at 16:55
  • BTW, you can print out messages in your DLL. But to be able to see these messages in your console, you have to build the DLL in debug mode. – Kamran Bigdely Mar 23 '17 at 19:27

3 Answers3

50

Both require turning on the same option: Project > Properties > Debug tab > tick the "Enable unmanaged code debugging" option.

You can now set a breakpoint in the native DLL code, it will turn from hollow to solid as soon as the C# project loads the DLL. And OutputDebugString() output will go to the Output window thanks to the unmanaged debugging engine being used.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 9
    You must also set this option in the properties of the startup project, not just on the project using the unmanaged code. – Westy92 Aug 05 '14 at 20:09
4

When attaching, change the "Attach to" value to "Native". The process should not be running under the managed code debugger - instead of "Debug", use the "Run" command to start.

Also, the DLL needs to be compiled with debug info for any sensible debugging to take place. Make sure you're not P/Invoking the Release build.

The OutputDebugString() should work regardless of debugging mode, however.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
3

If you run up a C++ debugger while your program is running, and then go to Debug->Attach To Process->Find your process and attach to it. You should be able to debug it.

Make sure that you have compiled your DLL with the debugger symbols. (.pdb) file and that they are in the directory where you run things from.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415