gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:12:23 25 4
gpt4 key购买 nike

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

这些是来自 DLL 'KeygenLibrary.dll' 的原始函数:

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

bool dfcCreateLicense(char* sMachineID, int iLen, char* sLicenseFilePath);

要导入这个 DLL,我试过:

方式一:

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);
}

方式二:

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”中找到名为“函数名称”的入口点。

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

最佳答案

建议:

1) 在您的 .dll 上运行“dumpbin”以确认问题确实是名称修改。

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

Entry Point Not Found Exception

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

b) 设置 EntryPointy == 损坏的名称

c) 设置 CallingConvention = CallingConvention.Cdecl

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

另请参阅此链接:

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

Laurent.... You can call the function using the mangled name as in:

To call a function using its fully decorated name "?fnWin32Test2@@YAJXZ" as

"Win32Test2" you can specify the static entry point as "?fnWin32Test2@@YAJXY":

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

And call it as:

System.Console.WriteLine(fnWin32Test2());

To look at the undecorated name use the undname tool as in:

`undname ?fnWin32Test@@3HA`

This converts the decorated name to "long fnWin32Test".

Regards, Jeff

关于c# - 将带有指针的 C++ DLL 导入 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20770065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com