开发者

In MFC APP, if I call "LoadLibraryA" from "InitInstance", it calls "InitInstance" again and again

I have created an MFCApp using VS2008 wizard. Inside my application's "InitInstance()" I'm calling "LoadLibraryA()" method as I need to load a few dll files. But as soon as I call "LoadLibraryA()", it again calls "InitInstance()" of my application and hence it becomes a infinite recursion stuff. Is there something I'm doing wrong?

// CLoader_MFCApp initialization
BOOL CLoader_MFCApp::InitInstance()
{
  INITCOMMONCONTROLSEX InitCtrls;
  InitCtrls.dwSize = sizeof(InitCtrls);
  InitCtrls.dwICC = ICC_WIN95_CLASSES;
  InitCommonControlsEx(&InitCtrls);
  CWinAppEx::InitInstance();
  SetRegistryKey(_T("MyApp"));

  HMODULE hm = LoadLibraryA("./abc/def.dll");
  // after above line InitInstance() gets called again

  // more code
  return FALSE;
}

Call Stack:

MyApp.exe!CLoader_MFCApp::InitInstance()    C++
CORE.dll!InternalDllMain(HINSTANCE__ *, unsigned long, void *)  C++
CORE.dll!__DllMainCRTStartup(void *, unsigned long, void *)     C
CORE.dll!_DllMainCRTStartup(void *, unsigned long, void *)  C
ntdll.dll!_LdrpCallInitRoutine@16()     
ntdll.dll!_LdrpRunInitializeRoutines@4()    
ntdll.dll!_LdrpLoadDll@24()     
ntdll.dll!_LdrLoadDll@16()  
kernel32.dll!_LoadLibraryExW@12()   
kernel32.dll!_LoadLibraryExA@12()   
kernel32.dll!_LoadLibraryA@4()  
MyApp.exe!CLoader_MFCApp::InitInstance()    C++
mfc90.dll!AfxWinMain(HINSTANCE__ *, HINSTANCE__ *, char *, int)     C++
MyApp.exe!__tmainCRTStartup()   C
kernel32.dll!_BaseProcessStart@4()  

"Def.dll" is any other dll and completely unrelated from MyApp. In this case, I'm trying to load another dll "CORE.dll"

All I can figure out is that I'm calling LoadLibrary before In开发者_开发知识库itInstance routine is over. Is there any other (overridable) method which is called after InitInstance??? If so, I can try moving LoadLibrary calls to that method...


Yes, you are doing something wrong. You are in mfc90.dll's DllMain and it is not safe to call LoadLibrary from DllMain, says so right here:

http://msdn.microsoft.com/en-us/library/ms684175%28v=vs.85%29.aspx


This is more of a workaround than a true solution (i.e. I don't know the rules for LoadLibrary in MFC, as I've never read anything to say you can't, nor do I happen to use this technique in our MFC code).

However, Generally speaking, if windows coughs up a hairball due to order of operations, I just move the calls out to another message handler. You can even post a thread message to your application, and write a handler for that message.

Something like:

// in InitInstance - post a message to our main thread to handle after init instance...
PostMessage(NULL, WM_PostInit);

// in your message table
ON_THREAD_MESSAGE(WM_PostInit, OnPostInit)

// in your app
void MyApp::OnPostInit(WPARAM,LPARAM) // both args unused
{
  // try load library now...!
}

NOTE: The above is "brain code" - untested. Details undoubtedly need to be massaged for full compilability.

References: http://msdn.microsoft.com/en-us/library/ms644944%28v=VS.85%29.aspx


I've just had the same issue, caused by the Configuration type being incorrectly set to exe not dll for the dll to be loaded.

Fix: Project -> Configuration Properties -> General -> Configuration Type = Dynamic Library (.dll) (was incorrectly set to Application (.exe))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜