C++源码如下:
—————————————————a.h————————————————— #ifdef A_EXPORTS #define A_API __declspec(dllexport) #else #define A_API __declspec(dllimport) #endif A_API int F(void); —————————————————a.cpp————————————————— #include "stdafx.h" #include "a.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } A_API int F(void) { MessageBox(NULL, "1212", "1212", MB_OK); return 0; }
C#源码函数原型声明:
[DllImport("a.dll")] public extern static int F();
调用后提示找不到入口点
在命令行用dumpbin /exports 看函数名:
dumpbin /exports a.dll
函数名不是"F"?而是"?F@@YAHXZ"
C#函数声明写成:
这样调用成功!
原因:在C++函数声明时要将 extern "C" 添加在 DLL 函数声明之前
主要注意包含 DllImport 的代码行。此代码行根据参数值通知编译器,使之声明位于 User32.dll 中的函数并将签名中出现的所有字符串(如参数或返回值)视为 Unicode 字符串。如果缺少 EntryPoint 参数,则默认值为函数名。另外,由于 CharSet 参数指定 Unicode,因此公共语言运行库将首先查找称为 MessageBoxW(有 W 是因为 Unicode 规范)的函数。如果运行库未找到此函数,它将根据调用约定查找 MessageBox 以及相应的修饰名。受支持的调用约定只有
__cdecl 和 __stdcall。
当调用用户定义的 DLL 中所包含的函数时,如下所示:
// The function declaration in SampleDLL.h file extern "C" SAMPLEDLL_API int fnSampleDLL(void);