- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
从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/
直到现在,我从未真正需要我正在开发的大型 MFC(如果重要,则为单文档界面)应用程序的 Winapp ExitInstance()。但现在我这样做了,主要是为了清理内存分配、卸载一些 DLL 等。好吧
我有一个带有函数 InitInstance 和 ExitInstance 的 Windows DLL。DLL 创建一些线程,使用 _beginthreadex 的工作线程和使用 Message Que
从MSDN文档中可以看出,在DllMain入口函数中不应该调用LoadLibrary/FreeLibrary。 The entry-point function should perform only
我是一名优秀的程序员,十分优秀!