Can we call FreeLibrary from ExitInstance

2019-04-28 06:33发布

问题:

From the MSDN document, we can see that, we should not call LoadLibrary/FreeLibrary in the DllMain entry point function.

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.

My question is: Can we call FreeLibrary from ExitInstance()? for example:

Test.exe - main executable

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 - Regular DLLs dynamically linked to MFC

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 - Regular DLLs, resource

...

I think we should not, but from the implmentation of CWinApp::ExitInstance(), we can see that, it was also trying to unload the resource dll. Does that mean we can call FreeLibrary in the ExitIntance()?

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

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

I also found a document which confirm there is a bug while calling FreeLibrary from ExitInstance in Win95.

BUG: Assert When Calling AfxFreeLibrary from ExitInstance 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.

回答1:

If in fact ExitInstance is being called from DllMain (as confirmed by the stack trace) then all the rules for DllMain apply, including the prohibition on loading or unloading other DLLs recursively. (Note that putting your CWinApp in a DLL is highly unusual and MFC may have other issues with it, such as the one noted in the KB article. Not saying that there are or are not any more issues lurking, but adding a note of additional caution.)



标签: windows dll mfc