简体   繁体   English

无法使用/调试来自C#的非托管DLL

[英]Can't use/debug unmanaged DLL from C#

I have a C# application and a C++ DLL, both x86. 我有一个C#应用程序和一个C ++ DLL,两者均为x86。 The Application is the startup project, the DLL project is inside the same solution and is referenced as a project. 该应用程序是启动项目,该DLL项目在同一解决方案中,并且被引用为一个项目。 The C++ DLL outputs its PDB file with the same name as the DLL inside the Debug folder of the Application. C ++ DLL输出其PDB文件,其名称与该应用程序的Debug文件夹中的DLL相同。

I have a function called SomeFunction that I'm trying to execute in C#. 我有一个要在C#中执行的名为SomeFunction的函数。 When the code reaches that line, it stops there. 当代码到达该行时,它在那里停止。 The UI of the C# application continues to be responsive, but the breakpoint never leaves that line (and that's in Form_Load). C#应用程序的UI继续保持响应状态,但是断点从不离开该行(并且该行位于Form_Load中)。

If I try to set the DLL as startup project and tell it to execute the C# application, then C# will crash at that line with: System.EntryPointNotFoundException: Unable to find an entry point named 'SomeFunction' in DLL 'SomeDLL.dll'. 如果我尝试将DLL设置为启动项目并告诉其执行C#应用程序,则C#将在该行崩溃,并带有以下内容:System.EntryPointNotFoundException:无法在DLL'SomeDLL.dll'中找到名为'SomeFunction'的入口点。

This is the declaration of the function that I'm trying to call: 这是我要调用的函数的声明:

[DllImport("SomeDLL.dll", EntryPoint = "SomeFunction", CallingConvention = CallingConvention.Cdecl)]
public static extern int SomeFunction(IntPtr hwnd);

This is the function's declaration from the C++ header: 这是C ++标头中的函数声明:

#define MYDLL_API __declspec(dllexport)
MYDLL_API extern int SomeFunction(HWND hWnd);

This is how I call it from C#: 这就是我从C#调用它的方式:

var someAnswer = SomeFunction(_hwnd);

UPDATE : since the discussion below could take some time to read, here's the answer in the nutshell: I was missing an extern "C". 更新 :由于下面的讨论可能需要一些时间才能阅读,因此简而言之,这就是答案:我错过了extern“ C”。 Also, in order to debug the DLL (which was also a problem), the project needs to support native debugging both from C++ and C#, here's an excellent list that I went through and at the end it was all good! 此外,为了调试DLL(这也是一个问题),该项目需要支持C ++和C#的本机调试,这是我经历的一个很棒的清单,最后一切都很好!

No Symbols loaded in mixed C# C(win32) project using VS2010 使用VS2010在混合C#C(win32)项目中未加载任何符号

I suspect the problem that it won't set the breakpoint beforehand is that the DLL is loaded on demand via p-invoke. 我怀疑它不会事先设置断点的问题是DLL是通过p调用按需加载的。 If VS doesn't think the DLL is a dependency, it may not allow you to set the breakpoint beforehand. 如果VS认为DLL不是依赖项,则可能不允许您事先设置断点。

You could add a DebugBreak to your c++ code. 您可以将DebugBreak添加到您的c ++代码中。 Then simply run your app, don't debug it. 然后只需运行您的应用程序,而不调试它。 Then when it executes this statement, you will get a Just in time debugging alert allowing you to jump into the c++ debugger. 然后,当它执行此语句时,您将收到“ 及时调试”警报,使您可以跳入c ++调试器。

C++ as startup C ++作为启动

Alternatively, you could make the DLL the startup project, but change the debug settings so that it launches an external process - in this case your app .exe. 或者,您可以使DLL成为启动项目,但可以更改调试设置,以使其启动外部进程-在本例中为您的应用程序.exe。

This time however, you don't need the DebugBreak(), just a regular breakpoint. 但是这一次,您不需要DebugBreak(),只需一个常规断点即可。

Now when you debug, your DLLs symbols are loaded thus allowing the break points to work. 现在,当您进行调试时,将加载DLL符号,从而使断点起作用。

I used this trick a lot with ActiveX/COM. 我在ActiveX / COM上经常使用此技巧。

  1. Run code that calls SomeFunction to ensure unmanaged .dll is loaded 运行调用SomeFunction代码以确保加载了非托管.dll
  2. Go into Debug -> Windows -> Modules and check the .dll path and symbols status. 进入调试-> Windows->模块并检查.dll路径和symbols状态。 You can try to load symbols in context menu of your c++ dll in that window. 您可以尝试在该窗口的c ++ dll的上下文菜单中加载symbols

(very often happens that .net loads different (not expected) version of unmanaged .dll) (.net经常会加载非托管.dll的不同(非预期)版本)

Now I'm pretty sure that the problem is that your exported function name is mangled. 现在,我很确定问题出在您的导出函数名称是否整齐。 Try to define function like this: 尝试这样定义函数:

extern "C" MYDLL_API int PASCAL SomeFunction(HWND hWnd);

In C#: 在C#中:

[DllImport("SomeDLL.dll")]
public static extern int SomeFunction(IntPtr hwnd);

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

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