在MFC APP中,如果我调用“LoadLibraryA”从“InitInstance”,它调用“InitInstance”一次又一次
我已经使用 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 调用移至该方法...
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 InitInstance 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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,你做错了什么。
您位于 mfc90.dll 的 DllMain 中,从 DllMain 调用 LoadLibrary 是不安全的,如下所示:
http://msdn.microsoft.com/en-us/library/ms684175%28v=vs.85%29.aspx
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
这更像是一种解决方法,而不是真正的解决方案(即我不知道 MFC 中 LoadLibrary 的规则,因为我从未读过任何说不能的内容,我也没有碰巧在我们的 MFC 代码中使用此技术)。
然而,一般来说,如果 Windows 由于操作顺序而出现问题,我只需将调用移至另一个消息处理程序即可。您甚至可以发布一条线程消息到您的应用程序,并为该消息编写一个处理程序。
类似于:
注意:以上是“大脑代码” - 未经测试。毫无疑问,需要调整细节才能实现完全编译。
参考:
http://msdn.microsoft.com/en -us/library/ms644944%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:
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
我刚刚遇到了同样的
问题
,是由于
配置类型
被错误地设置为exe
而不是引起的>dll
用于加载dll
。修复:
项目 ->配置属性->一般->配置类型 = 动态库 (.dll)
(错误地设置为应用程序 (.exe))I've just had the same
issue
,caused by
theConfiguration type
being incorrectly set toexe
notdll
for thedll
to be loaded.Fix:
Project -> Configuration Properties -> General -> Configuration Type = Dynamic Library (.dll)
(was incorrectly set to Application (.exe))