我有以下代码:
typedef int (WINAPI* fnEngineStart)();
int __stdcall EngineStart()
{
BOOL FreeResult = 0, RunTimeLinkSuccess = 0; //variables for later use
HMODULE LibraryHandle = 0; //Here the handle to API module/dll will be stored.
fnEngineStart fn = 0;
LibraryHandle = AfxLoadLibrary(L"FlowEngine.dll"); //get the handle of our API module
//so it will now be loaded.
if (LibraryHandle != NULL) //if the library loading was successfull..
{
fn = (fnEngineStart)GetProcAddress(LibraryHandle,
"fnEngineStart");
if (RunTimeLinkSuccess = (fn != NULL)) //if operation was successful...
{
int ReturnValue = fn(); //call messageboxa function
//from user32.dll
}
else
{
MessageBox(0, L"Error", 0, 0);
}
FreeResult = FreeLibrary(LibraryHandle);
//from this process...
return FreeResult; //routine was successful
}
return EXIT_FAILURE; //else, it failed
}
此代码适用于例如user32.dll和MessageBoxA,但不适用于我自己的dll ......
int __declspec(dllexport) __stdcall fnEngineStart()
{
MessageBox(0, L"Succes!", 0, 0);
return 0;
}
我如何让它为我自己的dll工作? 提前谢谢。
答案 0 :(得分:0)
您正在处理的问题是Name Mangling。使用extern "C"
,这样编译器就不会破坏名称。例如:
extern "C" int __declspec(dllexport) __stdcall fnEngineStart()
{
MessageBox(0, L"Succes!", 0, 0);
return 0;
}
注意:__stdcall
函数使用前导下划线进行名称修饰,后跟@,然后是堆栈上传递的参数的数量(以字节为单位)。在32位对齐的机器上,此数字始终是4的倍数。 Source
如果您的编译器支持它,您可以在您的dll中执行此操作,一切都应该按照现在的方式工作。
extern "C" int __declspec(dllexport) __stdcall fnEngineStart()
{
#pragma comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__)
MessageBox(0, L"Succes!", 0, 0);
return 0;
}