Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9043457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:44:11+00:00 2026-06-16T10:44:11+00:00

From the MSDN document, we can see that, we should not call LoadLibrary/FreeLibrary in

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:44:13+00:00Added an answer on June 16, 2026 at 10:44 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From MSDN document i understand ( I am not sure,i understand perfectly ) :
From MSDN doc : A delegate is a type that safely encapsulates a method,
From MSDN Types that implement IComparable must override Equals.Types that override Equals must also
In accordance with the steps from MSDN , I can debug .net framework source
In the MSDN documentation, it documents that you can set the UpdateSourceTrigger to an
It seems from the MSDN overview that the only change to ADO.Net in .Net
CSDL includes elements Documentation/Summary and Documentation/LongDescription that, according to MSDN , can be used
Quick question (hopefully), how do I properly call the win32 function CreateProfile() from C#
From MSDN ( link ): The type PrintDocumentImageableArea has no public constructor. Hence, the
It seems from msdn (comment at the page bottom), if you set the Expires

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.