Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I debug a native code project from inside a managed code project? C++/C#

How do I debug a native code project from inside a managed code project? I am using C# to call C++ code and need to debug the C++ code. Otherwise, I must exit out of the project, open the C++ project, create a tester and then debug the code. This is horrible.

like image 441
LunchMarble Avatar asked Jul 18 '11 19:07

LunchMarble


People also ask

How do I Debug the native code?

For Visual Basic, select Debug in the left pane, select the Enable native code debugging check box, and then close the properties page to save the changes. Select Debug in the left pane, select the Enable native code debugging check box, and then close the properties page to save the changes.

How do I run a project in Debug mode?

Set the project as the main project and choose Debug > Debug Main Project (Ctrl+F5) from the main menu or right-click the project in the Projects window and choose Debug.


2 Answers

Add your c++ project to the solution containing your C# code

In the C# project properties pages, under the debug tab

Check "Enable unmanaged code debugging"

If that doesn't work you also need to open the Options dialog from either the Debug or the Tools menu (in VS2017) and go to Debugging->General. Check the option

"Suppress JIT optimization on module load (Managed only)."

You will then be able to debug into your C++/CLI and C++ code. (This final tip comes from Kim Togo Andersen.)

like image 160
Eric Avatar answered Oct 03 '22 10:10

Eric


In the debugging options for visual studio you HAVE to specify the correct debugger type.

Open up the property dialog window for the project, and under the Configuration Properties select Debugging. For the Debugger Type option, select the property that applies:

This can be auto, or mixed. I prefer mixed as it is explicitly stating you want both managed and native debugging.

As a side note, you can pick native only, but you won't be able to set a breakpoint in managed code. I'm not sure if this is an issue for you or not.

If you pick managed only, you obviously won't hit any breakpoints in native code.

like image 37
C Johnson Avatar answered Oct 03 '22 11:10

C Johnson