简体   繁体   English

有没有办法调试从 C# DllImport 调用的 C++ dll?

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

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.我想知道是否有任何方法可以在 VS 2010 中调试从 C# PInvoke 调用的 c++ dll。我尝试将项目附加到 c# 应用程序中,但没有成功 - 没有在断点处停止。

I also tried to record anything with OutputDebugString in C++ project but nothing printed with PInvoke call.我还尝试在 C++ 项目中使用 OutputDebugString 记录任何内容,但没有使用 PInvoke 调用打印任何内容。 Despite these issues, the actual function runs well.尽管存在这些问题,但实际功能运行良好。

Any advice will be appreciated.任何建议将被认真考虑。

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.您现在可以在本机 DLL 代码中设置断点,一旦 C# 项目加载 DLL,它就会从空心变为实心。 And OutputDebugString() output will go to the Output window thanks to the unmanaged debugging engine being used.由于使用了非托管调试引擎,OutputDebugString() 输出将进入输出窗口。

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.如果您在程序运行时运行 C++ 调试器,然后转到 Debug->Attach To Process->Find your process 并附加到它。 You should be able to debug it.你应该能够调试它。

Make sure that you have compiled your DLL with the debugger symbols.确保您已使用调试器符号编译了 DLL。 (.pdb) file and that they are in the directory where you run things from. (.pdb) 文件,并且它们位于您从中运行的目录中。

When attaching, change the "Attach to" value to "Native".附加时,将“附加到”值更改为“本机”。 The process should not be running under the managed code debugger - instead of "Debug", use the "Run" command to start.该进程不应在托管代码调试器下运行 - 而不是“调试”,使用“运行”命令启动。

Also, the DLL needs to be compiled with debug info for any sensible debugging to take place.此外,DLL 需要使用调试信息进行编译,以便进行任何合理的调试。 Make sure you're not P/Invoking the Release build.确保您没有 P/Invoking 发布版本。

The OutputDebugString() should work regardless of debugging mode, however.然而,无论调试模式如何,OutputDebugString() 都应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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