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

I created MFCApp using the VS2008 wizard. Inside my application, "InitInstance ()", I call the LoadLibraryA () method because I need to load multiple DLL files. But as soon as I call "LoadLibraryA ()", it again calls the "InitInstance ()" of my application and, therefore, it becomes infinite recursive material. 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 is not completely associated with MyApp. In this case, I am trying to load another dll "CORE.dll"

, , , LoadLibrary InitInstance. - () , InitInstance??? , LoadLibrary ...

+3
3

, - . mfc90.dll DllMain, LoadLibrary DllMain, :

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

+1

, (.. LoadLibrary MFC, , , , MFC).

, , - , . .

- :

// 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...!
}

. " " - . , , .

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

+1

I just got the same ones issuethat caused by Configuration typewere incorrectly set to exenot dllfor the download dll.

Bugfix: Project -> Configuration Properties -> General -> Configuration Type = Dynamic Library (.dll)(application (.exe) was installed incorrectly)

0
source

Source: https://habr.com/ru/post/1783834/


All Articles