gpt4 book ai didi

windows - 我们可以从 ExitInstance 调用 FreeLibrary

转载 作者:可可西里 更新时间:2023-11-01 13:26:18 24 4
gpt4 key购买 nike

从MSDN文档中可以看出,在DllMain入口函数中不应该调用LoadLibrary/FreeLibrary。

The entry-point function should perform only simple initialization or termination tasks. It must not call the LoadLibrary or LoadLibraryEx function (or a function that calls these functions), because this may create dependency loops in the DLL load order. This can result in a DLL being used before the system has executed its initialization code. Similarly, the entry-point function must not call the FreeLibrary function (or a function that calls FreeLibrary) during process termination, because this can result in a DLL being used after the system has executed its termination code.

我的问题是:我们可以从 ExitInstance() 调用 FreeLibrary 吗? 例如:

Test.exe - 主要可执行文件

HINSTANCE hDllMFC = LoadLibrary(L"TestApp.dll");
if (hDllMFC != NULL)
{
FreeLibrary(hDllMFC);
}

while unload the hDllMFC, the call stack looks like:

TestApp.dll!CTestAppApp::ExitInstance() Line 42 C++
TestApp.dll!InternalDllMain() Line 155 C++
TestApp.dll!DllMain() Line 272 C++
TestApp.dll!__DllMainCRTStartup() Line 512 C
TestApp.dll!_DllMainCRTStartup() Line 477 C
ntdll.dll!LdrpUnloadDll() Unknown
ntdll.dll!LdrUnloadDll() Unknown
KernelBase.dll!FreeLibrary() Unknown
Test.exe!wmain() Line 17 C++

TestApp.dll - 动态链接到 MFC 的常规 DLL

CTestApp theApp;
HINSTANCE hDllResource = NULL;

BOOL CTestApp::InitInstance()
{
hDllResource = ::LoadLibrary(L"TestApp_Resource.dll");

return CWinApp::InitInstance();
}

int CTestApp::ExitInstance()
{
::FreeLibrary(hDllResource);

return CWinApp::ExitInstance();
}

TestApp_Resource.dll - 常规 DLL、资源

...

我认为我们不应该,但是从 CWinApp::ExitInstance() 的实现中,我们可以看出,它也在尝试卸载资源 dll。这是否意味着我们可以在 ExitIntance() 中调用 FreeLibrary?

int CWinApp::ExitInstance()
{
//...

if (m_hLangResourceDLL != NULL)
{
::FreeLibrary(m_hLangResourceDLL);
m_hLangResourceDLL = NULL;
}
//...
}

我还找到了一份文档,确认在 Win95 中从 ExitInstance 调用 FreeLibrary 时存在错误。

错误:从 ExitInstance 调用 AfxFreeLibrary 时断言 http://support.microsoft.com/kb/187684

STATUS: Microsoft has confirmed this to be a bug in Windows 95. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.

最佳答案

如果实际上是从 DllMain 调用 ExitInstance(由堆栈跟踪确认),则适用 DllMain 的所有规则,包括禁止递归加载或卸载其他 DLL。 (请注意,将您的 CWinApp 放在 DLL 中是非常不寻常的,MFC 可能还有其他问题,例如知识库文章中提到的问题。并不是说有或没有更多的问题潜伏,而是添加一个额外的注释注意。)

关于windows - 我们可以从 ExitInstance 调用 FreeLibrary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14042546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com