Thread: Is DllMain's DLL_PROCESS_DETACH called when process is killed?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    34

    Is DllMain's DLL_PROCESS_DETACH called when process is killed?

    I have a dll with DllMain() function defined as below.

    Code:
    int DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*pReserved*/)
    {
        BOOL fRet = TRUE;
    
        switch (dwReason)
        {
        case DLL_PROCESS_ATTACH:
            {
                ...Do some initialization...  
            }
            break;
    
        case DLL_PROCESS_DETACH:
            {
                ...Do some uninitialization...
            }
            break;
        }
    
        return fRet;
    }
    Let's say that we load a dll file in an exe file. If we kill the running instance of that exe file, would DLL_PROCESS_DETACH part of the DllMain function of the dll file be called properly? Or would the DllMain function be called only when FreeLibrary(...) invocation is made from the exe file on the dll?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    From msdn:
    Quote Originally Posted by MSDN
    The DLL is being unloaded from the virtual address space of the calling process because it was loaded unsuccessfully or the reference count has reached zero (the processes has either terminated or called FreeLibrary one time for each time it called LoadLibrary).

    The lpReserved parameter indicates whether the DLL is being unloaded as a result of a FreeLibrary call, a failure to load, or process termination.

    The DLL can use this opportunity to call the TlsFree function to free any TLS indices allocated by using TlsAlloc and to free any thread local data.

    Note that the thread that receives the DLL_PROCESS_DETACH notification is not necessarily the same thread that received the DLL_PROCESS_ATTACH notification.
    Looks like it is called when the process is terminated, not just when FreeLibrary is called. Of course this can easily be verified in 5 minutes by writing a test application.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    It is called whenever a process detaches, so if the process terminates or the process calls FreeLibrary().

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    Quote Originally Posted by bithub View Post
    Of course this can easily be verified in 5 minutes by writing a test application.
    1 minute with Win32 wizard from VS and a Beep()...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Killing A Process
    By tommyb05 in forum C Programming
    Replies: 8
    Last Post: 06-01-2009, 06:41 AM
  2. Understanding fork()
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 02-27-2009, 12:09 PM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  4. Round Robin Scheduling using c.
    By eclipt in forum C Programming
    Replies: 8
    Last Post: 12-28-2005, 04:58 PM
  5. Replies: 12
    Last Post: 05-17-2003, 05:58 AM