入口点未找到异常

14

我正在尝试在C#项目中使用C++非托管DLL,在调用函数时出现了一个错误,提示找不到入口点。

public class Program
{

    static void Main(string[] args)
    {
        IntPtr testIntPtr = aaeonAPIOpen(0);            
        Console.WriteLine(testIntPtr.ToString());
    }

    [DllImport("aonAPI.dll")]
    public static extern unsafe IntPtr aaeonAPIOpen(uint reserved);
}

这是该函数的dumpbin:

5    4 00001020 ?aaeonAPIOpen@@YAPAXK@Z

我将dll导入更改为[DllImport("aonAPI.dll", EntryPoint="?aaeonAPIOpen")][DllImport("aonAPI.dll", EntryPoint="_aaeonAPIOpen")],但是没有成功。

2个回答

16

使用undname.exe实用程序,该符号解缩为

 void * __cdecl aaeonAPIOpen(unsigned long)

应该如何正确声明:

    [DllImport("aonAPI.dll", EntryPoint="?aaeonAPIOpen@@YAPAXK@Z", 
        ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr aaeonAPIOpen(uint reserved);

+1,我没想到你可以这样做。这种方法有多安全?混淆的名称会在每个版本中改变还是在同一代码的不同版本中保持一致? - JaredPar
@Jared:名称混淆纯粹基于 C++ 函数的声明。这就是为什么 undname.exe 能够工作的原因。只要声明不改变,它就是稳定的。这使其比 extern "C" 声明更安全 - Hans Passant

8

看起来你尝试调用的函数被编译为C++函数,因此它的名称被混淆了。PInvoke不支持混淆的名称。你需要在函数定义周围添加一个extern "C"块以防止名称混淆。

extern "C" {
  void* aaeonAPIOpen(uint reserved);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接