简体   繁体   中英

How to use Visual Studio to debug across a C# program calling a C++ dll?

I'm writing a C# application in Visual Studio 2019 that calls a C++ dll (to handle some CUDA programming) and I am not able to debug both sections of the code. The debugger works fine in the C# project but is not loading symbols in the C++ project even though they are all running in the same solution and are all set to DEBUG.
To be clear this is not a question about debugging CUDA code)

Any ideas how I might proceed?
I am not sure it is relevant, but here is the test code I am using...

C#

[DllImport("HoughTest.dll", CallingConvention = CallingConvention.Cdecl)]  
        public static extern float sumArray([MarshalAs(UnmanagedType.LPArray)] float[] x, int n);  

private void button_Click(object sender, EventArgs e)  
        {  
           // break points placed in this routine do trigger  
            float[] x = new float[] { 0.5f, 1, 2, 3 };  
            float sum = sumArray(x, x.Length);  
            MessageBox.Show("Sum is "+sum);  
        }  

C++

extern "C" __declspec(dllexport) float sumArray(float* x, int n)  
    {  
        // break points placed in this routine do not trigger  
        float sum = 0;  
        for (int i = 0; i < n; i++)  
        {  
            sum += x[i];  
        }  
        return sum;  
    }  

How to use Visual Studio to debug across a C# program calling a C++ dll?

Just add an answer for you and thanks to all the community members for their help.

To debug native C++ code in a managed project, it actually can be done on Visual Studio so far. You can check this official document-- Debug C# and C++ in the same debugging session .

Suggestion

1) First , before you build your native c++ dll project, you should make sure that the platform is the same as the c# managed project.

For an example, C++ project is X64 and you should make sure that your managed project also uses x64 .

Besides , right-click on the c++ project--> Properties --> Debugging -->set Debugger Type to Auto or Mixed .

2) Second , when you build your c++ project to generate the dll, you should copy both c++ dll and its pdb file into the managed project.

In other words , when you use dllimport to reference a c++ DLL, you must make sure that both the dll and the pdb exist in that path.

3) When you debug your C# project, you should check the option enable native code debugging under managed project Properties --> Debug .

==========================

If the breakpoint is still not hit, you should try to disable the option Enable Just My Code under Tools --> Options --> Debugging --> General .

In addition , you can also refer to this similar issue for more help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM