在MFC APP中,如果我调用“LoadLibraryA”从“InitInstance”,它调用“InitInstance”一次又一次

发布于 2024-10-11 00:01:49 字数 1549 浏览 4 评论 0原文

我已经使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤者何惧 2024-10-18 00:01:49

是的,你做错了什么。
您位于 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

暗地喜欢 2024-10-18 00:01:49

这更像是一种解决方法,而不是真正的解决方案(即我不知道 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...!
}

注意:以上是“大脑代码” - 未经测试。毫无疑问,需要调整细节才能实现完全编译。

参考:
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:

// 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

凉世弥音 2024-10-18 00:01:49

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

修复:项目 ->配置属性->一般->配置类型 = 动态库 (.dll)(错误地设置为应用程序 (.exe))

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))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文