Thread: How to know last process exited dll

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    How to know last process exited dll

    Hello all,

    I have dll which many process calls it.
    In DLL_PROCESS_ATTACH I allocate shared memory for the first application to attach the thread and call InitializeCriticalSection.

    In PROCESS_DETACH I want to call DeleteCriticalSection but only when the last process detach the dll. Is there anyway to do it without adding counter and wrapping it with criticalsection?

    Note that I am using CreateFileMapping() and MapViewOfFile() to create the shared memory in DLL_PROCESS_ATTACH and call UnmapViewOfFile() and CloseHandle() in PROCESS_DETACH always. UnmapViewOfFile() is smart enough to know if there are still processes using this resource.

    Thanks you for you help.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by alexia View Post
    In PROCESS_DETACH I want to call DeleteCriticalSection but only when the last process detach the dll. Is there anyway to do it without adding counter and wrapping it with criticalsection?
    you do not need critical section for the counter - just use InterlockedIncrement and InterlockedDecrement API InterlockedIncrement Function (Windows)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    Quote Originally Posted by vart View Post
    you do not need critical section for the counter - just use InterlockedIncrement and InterlockedDecrement API InterlockedIncrement Function (Windows)
    Hello vart,
    First thank you for your reply.

    Actually, I read somewhere that only one process/thread can be in DllMain() at a time so you are right I don't need critical section. However my question was to know if there's any API which returns the number of attached processes/threads to the dll.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by alexia View Post
    Hello vart,
    First thank you for your reply.

    Actually, I read somewhere that only one process/thread can be in DllMain() at a time so you are right I don't need critical section. However my question was to know if there's any API which returns the number of attached processes/threads to the dll.
    There's always only 1 process attached to a dll. Each process gets their own instance of it.
    As for threads, I'm not sure but I think the only way is to count DLL_THREAD_ATTACH/DETACH events yourself.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is quite possible to attach the same DLL to multiple processes. That's one advantage of a DLL: they are shared between processes.
    Although I believe every process will get the DLL mapped into their virtual memory, but there would only be one copy in physical memory.

    I am unaware of any API to do what you want.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Elysia View Post
    It is quite possible to attach the same DLL to multiple processes. That's one advantage of a DLL: they are shared between processes.
    Although I believe every process will get the DLL mapped into their virtual memory, but there would only be one copy in physical memory.
    Yes and no. If you have 1000 processes all loading the same dll then it would only be loaded once in to memory and mapped in to each process' virtual address space like you said. But this is only true for the code sections. And on a side note, if one process modifies a code page it gets a new unique memory page through a copy-on-write implementation.
    Heap and stack pages for the dll are unique to each application though. That's what I meant by "each process gets their own instance of [the dll]".
    If you had the following code
    Code:
    int counter = 0;
    
    BOOL WINAPI DllMain(HINSTANCE Instance, DWORD Reason, LPVOID Reserved)
    {
        switch(Reason)
        {
        case DLL_PROCESS_ATTACH:
            counter++;
        default: break;
        }
        return TRUE;
    }
    and an exported function to get the counter variable then each process out of the 1000 ones running would get a value of 1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why isn't the execlp() function doing anything?
    By jsrig88 in forum C Programming
    Replies: 5
    Last Post: 10-12-2009, 10:09 AM
  2. sequenceing or queueing multiple process
    By sv_joshi_pune in forum Windows Programming
    Replies: 1
    Last Post: 08-14-2009, 09:43 AM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  5. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM