在 C# 应用程序中从非托管 DLL 导入函数
我需要将我用 C++ 编写且现已编译为 DLL 的函数导入到我的 C# 应用程序中。一切构建都没有错误或警告,但是当我单步执行代码时,对 DLL 的第一个函数调用会抛出 Exception
,并显示消息“无法在 DLL 'WMIQuery 中找到名为 'CreateScanEngine' 的入口点” .dll'。” 该函数在我的 C# 应用程序中是这样声明的:
internal static class WMIQuery
{
[DllImport("WMIQuery.dll", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
internal static extern void CreateScanEngine();
}
Dependency Walker 在 DLL 中显示该函数,如下所示:
序数:1(0x0001)
提示:0(0x0000)
函数 ^: void CreateScanEngine(void)
入口点:0x00001860
Dependency Walker 还显示 DLL 的这些错误/警告:
错误:至少未找到所需的隐式或转发依赖项。
警告:至少未找到一个延迟加载依赖模块。
警告:由于延迟加载相关模块中缺少导出函数,至少有一个模块存在未解析的导入。
这会有所作为吗?另外,我尝试添加 DLL 作为对 C# 项目的引用,但收到此错误:
无法添加对[我的DLL]的引用。请确保该文件可访问,并且它是有效的程序集或 COM 组件。
有谁知道我做错了什么?谢谢。
I need to import a function that I wrote in C++, and is now compiled into a DLL, into my C# application. Everything builds without errors or warnings, but when I step through the code the first function call into the DLL throws an Exception
with a message of "Unable to find an entry point named 'CreateScanEngine" in DLL 'WMIQuery.dll'." The function is declared like so in my C# application:
internal static class WMIQuery
{
[DllImport("WMIQuery.dll", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
internal static extern void CreateScanEngine();
}
Dependency Walker shows the function there in the DLL as such:
Ordinal: 1(0x0001)
Hint: 0(0x0000)
Function ^: void CreateScanEngine(void)
Entry Point: 0x00001860
Dependency Walker also shows these errors/warnings for the DLL:
Error: At least one required implicit or forwarded dependency was not found.
Warning: At least one delay-load dependency module was not found.
Warning: At least one module has an unresolved import due to a missing export function in a delay-load dependent module.
Will this make a difference? Also, I tried adding the DLL as a reference to the C# project, and I got this error:
A reference to [my DLL] could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.
Does anyone know what I'm doing wrong? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用 Dependency Walker,它会解析函数的导出名称,并且不会告诉您真正的导出名称。请注意,它足够聪明,可以看到它不带任何参数,也没有返回值。仅当它看到由 C++ 编译器修饰的名称时,它才能执行此操作。
在 DLL 上使用 Dumpbin.exe /exports 查看真实名称。它应该是“?CreateScanEngine@@YGXXZ”,使用 [DllImport] 属性中的 EntryPoint 属性。您可能还想使用 extern "C" 声明该函数,这样就不会发生这种名称修改。
Don't use Dependency Walker, it unmangles the exported name of the function and doesn't tell you the real export name. Note that it is smart enough to see that it takes no arguments and has no return value. It can only do this if it sees the name as decorated by the C++ compiler.
Use Dumpbin.exe /exports on the DLL to see the real name. It ought to be "?CreateScanEngine@@YGXXZ", use the EntryPoint property in the [DllImport] attribute. You may also want to declare the function with extern "C" so this name mangling doesn't happen.
您如何在 C++ 代码中声明 CreateScanEngine?
尝试更改为:
How are you declaring CreateScanEngine in your C++ code?
Try changing to: