Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 457
Tae-Sung Shin Avatar asked Sep 20 '12 16:09

Tae-Sung Shin


People also ask

How do I Debug a compiled DLL?

Debug from the DLL project Set breakpoints in the DLL project. Right-click the DLL project and choose Set as Startup Project. Make sure the Solutions Configuration field is set to Debug. Press F5, click the green Start arrow, or select Debug > Start Debugging.

Can you Debug C in Visual Studio?

Visual Studio Code supports the following debuggers for C/C++ depending on the operating system you are using: Linux: GDB. macOS: LLDB or GDB. Windows: the Visual Studio Windows Debugger or GDB (using Cygwin or MinGW)


2 Answers

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.

like image 100
Hans Passant Avatar answered Sep 23 '22 13:09

Hans Passant


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.

like image 27
Tony The Lion Avatar answered Sep 25 '22 13:09

Tony The Lion