41

I have a .dll from c++ and I want to debug it in C#, but I don't know how to.

When I compiled the c++ project, Visual studio asked me to execute an ".exe".

I supposed that I had to create a project to execute the dll.

But I am lost, how could I debug it?

Gabriel Devillers
  • 3,155
  • 2
  • 30
  • 53

8 Answers8

46

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.

CCicotta
  • 595
  • 4
  • 9
  • 12
    Also, in Visual Studio 2012 I found the option to be called 'Enable native code debugging' – Darius Aug 10 '14 at 15:45
  • 1
    ...yes, that's what my VS 2017 Pro has -- no "Enable unman code deb." – Doug Null May 29 '18 at 15:56
  • Similarly, if you have a C# DLL to debug in native c++ project, choose 'Debugger type = mixed' in Debugging section of native c++ project. This will allow to step into cli/clr and c# code. – Fil May 06 '20 at 20:07
  • Does anybody know how to configure this for a .NET Core app in VSCode? – Chris Jul 23 '20 at 13:07
22

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.
heavyd
  • 17,303
  • 5
  • 56
  • 74
7

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

Noobie3001
  • 1,230
  • 18
  • 31
5

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.

JohnIdol
  • 48,899
  • 61
  • 158
  • 242
1

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.

1

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

Gabriel Devillers
  • 3,155
  • 2
  • 30
  • 53
0

SOS (Son of Strike) Debugging Extension (SOS.dll) helps you debug managed code in Visual Studio by providing information about the internal CLR environment

refer the below article:-

MSDN ARTICLE

V J
  • 77
  • 12
0

The option "Allow unmanaged code debugging" in the "Debug" tab works for most people, but it doesn't work for me. I have to manually select "Native" as the following to debug c++ dll in my c# code. FYI: I'm using vs2019, and the dll is written with c++ and loaded into C# app with "dllimport".

enter image description here

Jun Ge
  • 408
  • 4
  • 13