将dll方法从C导出到C#.我为什么需要:“ extern” C“”

在我的dll中,有一种我想导出的方法.

//作品:

extern "C" __declspec(dllexport)

//不会工作

__declspec(dllexport)

C出口:

 extern "C" __declspec(dllexport) int Test();

C#导入:

[DllImport("CircleGPU2_32.DLL", EntryPoint = "Test", 
    CallingConvention = CallingConvention.StdCall)]
public static extern int Test();

为什么需要外部字母“ C”?

解决方法:

主要原因是要防止C名称修饰符修饰函数名称.

尝试在不使用外部“ C”的情况下导出它,并在Dependency Walker中检查生成的DLL,您将看到导出函数的名称完全不同.

上一篇:14.decltype


下一篇:c – __declspec(dllimport)如何加载库