Is DllMain's DLL_PROCESS_DETACH called when process is killed?

This is a discussion on Is DllMain's DLL_PROCESS_DETACH called when process is killed? within the Windows Programming forums, part of the Platform Specific Boards category; I have a dll with DllMain() function defined as below. Code: int DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*pReserved*/) { BOOL ...

  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,260
    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,189
    It is called whenever a process detaches, so if the process terminates or the process calls FreeLibrary().
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  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, 07: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, 02: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, 06:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21