简体   繁体   English

在C#应用程序中从非托管DLL导入功能

[英]Importing Function From Unmanaged DLL in C# Application

I need to import a function that I wrote in C++, and is now compiled into a DLL, into my C# application. 我需要将用C ++编写的函数导入到C#应用程序中,该函数现在已编译为DLL。 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: 一切都建立在没有错误或警告的情况下,但是当我逐步执行代码时,对DLL的第一个函数调用将引发Exception并显示一条消息“无法在DLL'WMIQuery.dll'中找到名为'CreateScanEngine'的入口点”。在我的C#应用​​程序中这样声明:

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: Dependency Walker在DLL中显示了该函数,如下所示:

Ordinal: 1(0x0001) 序数:1(0x0001)
Hint: 0(0x0000) 提示:0(0x0000)
Function ^: void CreateScanEngine(void) 函数^:void CreateScanEngine(void)
Entry Point: 0x00001860 入口点:0x00001860

Dependency Walker also shows these errors/warnings for the DLL: Dependency Walker还会显示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: 另外,我尝试添加DLL作为对C#项目的引用,但出现此错误:

A reference to [my DLL] could not be added. 无法添加对[我的DLL]的引用。 Please make sure that the file is accessible, and that it is a valid assembly or COM component. 请确保该文件可访问,并且它是有效的程序集或COM组件。

Does anyone know what I'm doing wrong? 有人知道我在做什么错吗? Thanks. 谢谢。

Don't use Dependency Walker, it unmangles the exported name of the function and doesn't tell you the real export name. 不要使用Dependency Walker,它会破坏函数的导出名称,并且不会告诉您真实的导出名称。 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. 仅当看到由C ++编译器修饰的名称时,它才能执行此操作。

Use Dumpbin.exe /exports on the DLL to see the real name. 在DLL上使用Dumpbin.exe / exports查看真实名称。 It ought to be "?CreateScanEngine@@YGXXZ", use the EntryPoint property in the [DllImport] attribute. 应为“?CreateScanEngine @@ YGXXZ”,使用[DllImport]属性中的EntryPoint属性。 You may also want to declare the function with extern "C" so this name mangling doesn't happen. 您可能还想用extern“ C”声明该函数,因此不会发生此名称修改的情况。

How are you declaring CreateScanEngine in your C++ code? 您如何在C ++代码中声明CreateScanEngine?

Try changing to: 尝试更改为:

extern "C" __declspec(dllexport) void __stdcall CreateScanEngine();

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

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