将带有指针的C++DLL导入C#

Import C++ DLL with pointer into C#

本文关键字:C++DLL 导入 指针      更新时间:2023-10-16

我知道我的问题有很多解决方案。我都试过了;然而,我还是会出错。

这些是DLL"KeygenLibrary.DLL"中的原始函数:

bool dfcDecodeMachineID(char* sEncodeMachineID, int iInLen, char* sMachineID, int& iOutLen);
bool dfcCreateLicense(char* sMachineID, int iLen, char* sLicenseFilePath);

要导入这个DLL,我尝试过:

方式1:

unsafe public class ImportDLL
{
[DllImport("KeygenLibrary.dll", EntryPoint = "dfcDecodeMachineID")]
unsafe public static extern bool dfcDecodeMachineID(char* sEncodeMachineID, int iInLen, char* sMachineID, ref int iOutLen);
[DllImport("KeygenLibrary.dll", EntryPoint = "dfcCreateLicense")]
unsafe public static extern bool dfcCreateLicense(char* sMachineID, int iLen, char* sLicenseFilePath);
}    

方式2:

public class ImportDLL
{
[DllImport("KeygenLibrary.dll", EntryPoint = "dfcDecodeMachineID")]
public static extern bool dfcDecodeMachineID([MarshalAs(UnmanagedType.LPStr)] string sEncodeMachineID, int iInLen, [MarshalAs(UnmanagedType.LPStr)] string sMachineID, ref int iOutLen);

[DllImport("KeygenLibrary.dll", EntryPoint = "dfcCreateLicense")]
public static extern bool dfcCreateLicense([MarshalAs(UnmanagedType.LPStr)] string sMachineID, int iLen, [MarshalAs(UnmanagedType.LPStr)] string sLicenseFilePath);
}

然而,以上两种方式都给了我错误:

在DLL"KeygenLibrary.DLL"中找不到名为"function name"的入口点。

我该如何解决我的问题?非常感谢。

建议:

1) 在.dll上运行"dumpbin"以确认问题确实是名称篡改。

2) 如果是这样,请尝试此链接中的建议:

未找到入口点异常

a) 使用undname获取未修饰的名称

b) Set EntryPointy==损坏的名称

c) 设置CallingConvention=Cdecl

d) 为您的C#方法签名使用未标记的名称和签名

另请参阅此链接:

http://bytes.com/topic/c-sharp/answers/428504-c-program-calling-c-dll

Laurent。。。。您可以使用损坏的名称调用函数,如:

使用函数的全修饰名称调用函数"?fnWin32Test2@@YAJXZ"作为

"Win32Test2"您可以将静态入口点指定为"?fnWin32Test2@@YAJXY":

[DllImport("Win32Test.dll", EntryPoint= "?fnWin32Test2@@YAJXZ")]
public static extern int fnWin32Test2();

并称之为:

System.Console.WriteLine(fnWin32Test2());

要查看未修饰的名称,请使用undname工具,如下所示:

`undname ?fnWin32Test@@3HA`

这会将修饰的名称转换为"long-fnWin32Test"。

问候,Jeff