DLL中有DllMain函数(没有返回值) LoadLibray总是失败 的原因

//Dll中的代码 #include <windows.h> #pragma data_seg("MyData") HINSTANCE g_hInst = NULL; #pragma data_seg() BOOL DllMain(HINSTANCE hDllInst,DWORD dwReason,LPVOID lpReserved) { if(DLL_PROCESS_ATTACH == dwReason) { g_hInst = hDllInst; } return TRUE; } extern "C" _declspec(dllexport) int Add(int a,int b) { return a+b; } //主程序加载Dll的代码 #include <iostream> #include <windows.h> using namespace std; void main(int argc,TCHAR*argv[]) { HINSTANCE hInst = LoadLibrary("dll1.dll"); if(!hInst) { cout<<"fail"<<endl; return; } typedef int (*LPADD)(int,int); LPADD add = (LPADD)GetProcAddress(hInst,"Add"); if(!add) { cout<<"fail2"<<endl; FreeLibrary(hInst); return; } cout<<add(9,8)<<endl; FreeLibrary(hInst); }

原因:DllMain里面的代码是在LoadLibrary的时候调用的,执行完DllMain里面的代码,如果DllMain返回true,那么LoadLibrary才会返回成功

你可能感兴趣的:(DLL中有DllMain函数(没有返回值) LoadLibray总是失败 的原因)