开发者

Importing Function From Unmanaged DLL in C# Application

开发者 https://www.devze.com 2023-01-30 19:23 出处:网络
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 functio

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 Walke开发者_运维知识库r 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.


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.


How are you declaring CreateScanEngine in your C++ code?

Try changing to:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号