Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug c++ dll in C#

People also ask

How do I debug a 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.


If I understand you correctly, you want to debug a C++ coded DLL that you created, in a C# project that calls the DLL, which you also created?

I've done this before by going into your C# project properties, and under the Debug section, checking the "Enable unmanaged code debugging" check box. This should allow you to step into your C++ DLL.


To debug a C++ from C# there a couple of things you have to do.

  1. Add a C# project to you solution for your debug application.
  2. Edit the properties of the C# project to "Allow unmanaged code debugging" on the "Debug" tab of the project properties.
  3. Set the C++ project as a dependency of the C# project.
  4. Write code in your C# project to use the DLL either using P/Invoke or COM.
  5. Set some breakpoints in your C++ code and run the C# project.

I overcame this in Visual Studio 2019 by selecting "Enable native code debugging" as shown under my C# project's properies.

enter image description here


Visual Studio cannot execute a dll on its own.

You need to set the startup .exe that will be using your C++ dll in the properties of your dll project. You can do so from properties --> debugging --> command specifying the path of the executable that's gonna call your dll and any command line argument needed.


For VS 2017 Pro, go to the property page of the main project (your c# project in the solution) by right mouse clicking it. At the Debug menu item, find the option in Debugger engines, choose Enable native code debugging.


To complement the useful answers by heavyd and CCicotta and Noobie3001, in Visual Studio 2017 I also had to change the following when attaching to a running C# process calling a C++ DLL: in Debug > Attach to process ..., ensure Native code is listed by Attach to:

enter image description here