Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在MFC应用程序中,如果我调用;LoadLibraryA";从「;InitInstance";,它叫",;“初始化实例”;一次又一次_C++_Visual C++_Mfc_Loadlibrary - Fatal编程技术网

C++ 在MFC应用程序中,如果我调用;LoadLibraryA";从「;InitInstance";,它叫",;“初始化实例”;一次又一次

C++ 在MFC应用程序中,如果我调用;LoadLibraryA";从「;InitInstance";,它叫",;“初始化实例”;一次又一次,c++,visual-c++,mfc,loadlibrary,C++,Visual C++,Mfc,Loadlibrary,我已经使用VS2008向导创建了MFCApp。在我的应用程序的“InitInstance()”中,我正在调用“LoadLibraryA()”方法,因为我需要加载一些dll文件。但只要我调用“LoadLibraryA()”,它就会再次调用我的应用程序的“InitInstance()”,因此它就变成了一个无限递归的东西。我做错什么了吗 // CLoader_MFCApp initialization BOOL CLoader_MFCApp::InitInstance() { INITCOMMON

我已经使用VS2008向导创建了MFCApp。在我的应用程序的“InitInstance()”中,我正在调用“LoadLibraryA()”方法,因为我需要加载一些dll文件。但只要我调用“LoadLibraryA()”,它就会再次调用我的应用程序的“InitInstance()”,因此它就变成了一个无限递归的东西。我做错什么了吗

// 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;
}
调用堆栈:

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”是任何其他dll,与MyApp完全无关。在本例中,我尝试加载另一个dll“CORE.dll”


我只知道在InitInstance例程结束之前调用LoadLibrary。在InitInstance之后是否还有其他(可重写的)方法被调用???如果是这样,我可以尝试将LoadLibrary调用移动到该方法…

这与其说是一个真正的解决方案,不如说是一个解决方案(即,我不知道MFC中LoadLibrary的规则,因为我从来没有读过任何东西说你不能,我也没有在我们的MFC代码中使用这种技术)

但是,一般来说,如果windows由于操作顺序而出现问题,我只会将调用转移到另一个消息处理程序。您甚至可以向应用程序发布线程消息,并为该消息编写处理程序

比如:

// 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...!
}
注:以上为“大脑代码”-未经测试。毫无疑问,为了完全可编译,需要对细节进行修改

参考资料:
是的,你做错了什么。 您位于mfc90.dll的DllMain中,从DllMain调用LoadLibrary是不安全的,这里这样说:


我刚刚遇到了同样的
问题,
问题是由于
配置类型
被错误地设置为
exe
而不是
dll
以加载
dll


修复:
Project->Configuration Properties->General->Configuration Type=Dynamic Library(.dll)
(被错误地设置为Application(.exe))

使用调试构建(如果可能的话,也包括dll),并在LoadLibrary之后重新进入InitInstance时发布调用堆栈。什么是./abc/def.dll,它正在加载什么?它是否正在尝试加载您的库(循环依赖)?“CLoader_MFCApp”与此“def.dll”之间的关系是什么?是否存在任何相互依赖关系?def.dll中DllMain的代码可能相关。很可能代码正在执行某些操作,这些操作应该移动到DLL中的显式Init函数中。(允许您在DllMain本身中执行很少的操作。)但我尝试加载的每个dll都会发生这种情况:(