FreeLibrary vs implicit unloading DLL

Posted by Adil on Stack Overflow See other posts from Stack Overflow or by Adil
Published on 2010-04-28T04:45:19Z Indexed on 2010/04/28 4:53 UTC
Read the original article Hit count: 306

Filed under:
|
|

I have implemented a DLL including DllMain() entry function:-

BOOL APIENTRY DllMain( HMODULE hModule,
  DWORD  ul_reason_for_call,
  LPVOID lpReserved
  )
{
    case DLL_PROCESS_ATTACH:
    ...
    case DLL_THREAD_ATTACH:
    ...
    case DLL_THREAD_DETACH:
    ...
    case DLL_PROCESS_DETACH:
    ...

}

Unfortunately i made a mistake in DLL_PROCESS_DETACH case and accessing illegal memorey (access violation).

I made a sample program which loads the library using LoadLibrary() function, uses the library function and finally call FreeLibrary() and return.

When i executed this program, i didnt get any error message. But if i remove FreeLibrary(), in that case the DLL_PROCESS_DETACH case is executed implicitly and this time it gives error dialog box mentioning that there is access violation.

Why calling FreeLibrary() suppress this error? OR internally it handles this exception? What is suggested way.

© Stack Overflow or respective owner

Related posts about c++

Related posts about dll