使用指针将C ++ DLL导入C#

时间:2013-12-25 07:09:36

标签: c# c++ dllimport

我知道我的问题有很多解决方案。我已经全部尝试过了;但是,我仍然得到错误。

这些是来自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 个答案:

答案 0 :(得分:1)

建议:

1)在.dll上运行“dumpbin”以确认问题确实是名称错误。

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

Entry Point Not Found Exception

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

b)设置EntryPointy ==受损名称

c)设置Call​​ingConvention = 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”。

     

问候,杰夫