Tae-Sung Shin
Tae-Sung Shin

Reputation: 20620

Is there any way to debug c++ dll called from C# DllImport?

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.

Upvotes: 32

Views: 17066

Answers (3)

Hans Passant
Hans Passant

Reputation: 941873

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.

Upvotes: 54

Seva Alekseyev
Seva Alekseyev

Reputation: 61378

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.

Upvotes: 4

Tony The Lion
Tony The Lion

Reputation: 63240

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.

Upvotes: 3

Related Questions