CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2009
    Posts
    84

    [MFC] counting application's instances

    Hello everyone!

    I'd need to count how many instances of my own application are actually running...how I could do that?... I tried with GetProcessHandleCount() giving as first parameter this:

    HANDLE hInstance(AfxGetInstanceHandle());

    but I always get 0 as count... so I think this functions isn't the right one...

    Any idea?...

    Thanks
    Ciao
    Luigi

  2. #2
    Join Date
    Dec 2009
    Posts
    2

    Re: [MFC] counting application's instances

    The GetProcessHandleCount() retrieves the number of handles your application use and have nothing to do with process count.

    There are several ways to achieve what you want, have a look at this article, especially method 3 and 4:
    http://www.codeproject.com/KB/cpp/limitnumins.aspx

    or you can take a snapshot of the currently running processes and look up your applications process by name

    Code:
    DWORD GetProcessCount(const char *szProcessName) 
    {
        DWORD dwCount = 0;
        HANDLE hSnap = NULL;
        PROCESSENTRY32 proc32;
    
        if((hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)) == INVALID_HANDLE_VALUE)
            return -1;
    
        proc32.dwSize=sizeof(PROCESSENTRY32);
        while((Process32Next(hSnap, &proc32)) == TRUE)
            if(stricmp(proc32.szExeFile,pProcessName) == 0)
                ++dwCount;
    
        CloseHandle(hSnap); 
        return dwCount;
    }
    Regards,
    Black Spectrum

  3. #3
    Join Date
    Aug 2009
    Posts
    84

    Re: [MFC] counting application's instances

    Thanks Blackspectrum, I tried that GetProcessCount example you showed out and it was exactly what I wanted! Thanks again :-)

    Ciao,
    Luigi

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: [MFC] counting application's instances

    The question is what you mean by "own application".
    For instance, if you have the "own application" named MyApp.exe and some copies of it ("Copy of MyApp.exe", "Copy of Copy of MyApp.exe", ...), will you consider the instance started as Copy of MyApp.exe as the "own application" or not?
    Victor Nijegorodov

  5. #5
    Join Date
    Aug 2009
    Posts
    84

    Re: [MFC] counting application's instances

    VictorN, no I meant like if I execute more times the same MyApp.exe...

    Anyway I solved with that function Blackspectrum posted, it's what I was looking for... :-)

    Ciao,
    Luigi

    P.S.: really nice place Zurich...I've been sometimes there to see a friend of mine... :-)

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: [MFC] counting application's instances

    As VictorN pointed out... If someone copies MyApp and executes both the original and the copies, you'll have many more copies running than you actually think.

    The method will also fail if you have multiple different programs having the same name. Somewhat difficult to actually get this situation but it is possible if someone's out to sabotage your work.

    You're not saying what you want to achieve (why do you need to know how many copies), so it's possible a namecheck via the process snapshot is enough for you.
    If you really need to have a running count of how many times your exe is running, regardless of it being the original file or copies of it, or even older versions, then the right approach is to follow the Mutex principle as outlined in the link Blackspectrum linked, but instead of a mutex, use a semaphore.

    Of that same link:
    Method 0: doesn't work on Win32/64
    Method 1: ok
    Method 2: technically ok, however the majority of examples given using this technique fail in proper execution. It also has the huge disadvantage that in the case of counting number of running modules, that if any instance of the exe crashes, it's count will be 'locked' until the last instance of the modules is exited.
    Method 3: For all practical intent, under Win32/Win64, a shared data segment is a memory mapped file. So See Method 2. On top of that this particular example subtly fails because (as most of these examples) it has a separate increment and test, whereas you want the increment test and 'recover' combined to be atomic.
    Method 4: Can't say. Can't download the source without logging on. Technically feasible, but the description seems to imply needing multiple mutexes, this looks like a LOT of hassle, and it's also going to be prone to subtle failures.
    Method 5: REALLY bad idea. A crash now persists the count through reboots.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured