Thread: Have app process end if another instance running...

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

    Have app process end if another instance running...

    I'm looking for some C++ code or direction in writing some to close an app if it detects another instance running.

    Our little update utility writes a log file, which I'm thinking of using as the clue. I was thinking that before the second instance attempts to write to the .log file, if the MyApp.log is opened, I could just skip out.

    How can I get that done with C++? I'm not a C++ guys, as you may have gathered from other posts, so if there is anything special I have to do to close an app (something needed other than a Me.Close type statement), please let me know.

    Any help GREATLY appreciated!!
    Last edited by Superfreak3; 03-07-2013 at 03:37 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    You can do a few hacks to make this happen using only C++, using file I/O. But what if someone makes a copy of the exe and runs it from a different directory? How will you know where all the other instances reside?
    On the other hand, all systems provide one way or another to do what you're asking. I certainly know that Windows & Linux do. No portable way though.
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    For which operating system?
    Then I can move this thread to the right place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Common solution for Win32:

    Method1.c

    This approach looks for another window with the same WNDCLASS name, and this obviously has some flaws/limitations: Your program may not have a window (maybe it's a service running in the background) or there might be another window from another program with the same name.

    If it's a no to both of the above, i'd say that would be the easiest method. But since im not clairvoyant and you keep refering to your program as an 'app', there is a fair chance you're not using Windows, in which case this solution is....suboptimal.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I think the most portable (at least for the large number of systems that support it) would be to try to bind to some fixed port on the local interface 127.0.0.1. sure, there's the chance of collision with other processes that may try to bind to that port, but for something that works across many platforms, I think this would be the most likely to succeed.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    It's a little widget that runs on Windows system startup to check if our Client app needs to be updated. I wouldn't worry too much about the .exe being copied and run elsewhere. The app just checks a local .ini and an .ini file on the server for versions to trigger the call to Winodws Installer packages (.msi, .msp). It runs silently - no interface.

    The reason I want to make this change is for some reason, on an end users XP systems, it seems that the widget is running twice. It looks like there are duplicate entries in the .log that is created. I'm just trying to prevent this from occuring.

    I was thinking that I could check to see if the .log file is open and if so, dump out.

    Again, I'm no C++ guy so take it easy on me

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by Superfreak3 View Post
    It's a little widget that runs on Windows system startup to check if our Client app needs to be updated. I wouldn't worry too much about the .exe being copied and run elsewhere. The app just checks a local .ini and an .ini file on the server for versions to trigger the call to Winodws Installer packages (.msi, .msp). It runs silently - no interface.

    The reason I want to make this change is for some reason, on an end users XP systems, it seems that the widget is running twice. It looks like there are duplicate entries in the .log that is created. I'm just trying to prevent this from occuring.

    I was thinking that I could check to see if the .log file is open and if so, dump out.

    Again, I'm no C++ guy so take it easy on me
    Something from the past, I first posted it over 10 years ago....

    You should just remove the messagebox bit and exit.

    Your app should call CloseHandle(hMapping) when it exists.


    Code:
    hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,PAGE_READONLY, 0, 32, szAppName);
    
    if (hMapping)// Check to see if app is already running
    {
        if (GetLastError() == ERROR_ALREADY_EXISTS)
        {
    	sprintf(sBuffer,"%s is already running. Create another instance?",szAppName);
    	if(MessageBox(NULL, sBuffer,"Error",MB_ICONERROR| MB_YESNO)==IDNO)
               {
                    CloseHandle(hMapping);
    		ExitProcess(1);
               } 
        }
    } 
    else
    {
       MessageBox(NULL, "Error creating file mapping.", "Error",MB_ICONERROR| MB_OK);
       ExitProcess(1);
    }

    Other methods;

    How do you only allow one instance of an app?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    On Windows I would just use a mutex. If it can't get the mutex on startup then it should quit straight away.

    If you choose some other solution, make sure it is also one that doesn't end up with the problem where both copies see one-another running and decide to exit.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by iMalc View Post
    On Windows I would just use a mutex. If it can't get the mutex on startup then it should quit straight away.
    To add to this, it is best to pick a mutex with a unique name, to minimise chances of using a mutex that is used by another application (or, if you want to avoid accidents, that another person might guess). A name based on a GUID is handy for this.

    Theoretically, when your program is terminating, the mutex will be cleaned up. It is, however, a good idea to do that explicitly with a call of CloseHandle(). It takes little effort, and accounts for the fact that the difference between theory and practice is often the practice.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Dec 2010
    Location
    Trinidad, CO (log cabin in middle of nowhere)
    Posts
    148
    I'd third what iMalc and Grumpy said. I'd use a mutex (Mutual Exclusion Synchronazation Object). I use those all the time to prevent two instances of a running program from accessing the same flat file not designed as multi-use. I name the mutex based on the file name.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find out if a process is running (by name)
    By rm55en in forum Windows Programming
    Replies: 3
    Last Post: 11-04-2011, 09:52 AM
  2. Check Running Process Dev-C++
    By gamesplant in forum Windows Programming
    Replies: 23
    Last Post: 10-19-2009, 03:20 AM
  3. [c++] check if a process is already running
    By liquid_ice in forum Linux Programming
    Replies: 0
    Last Post: 05-12-2009, 03:30 AM
  4. Monitor a running instance of MS Word
    By BobS0327 in forum C# Programming
    Replies: 0
    Last Post: 07-18-2008, 12:40 PM
  5. Getting VmSize of a running process
    By ruairi in forum Linux Programming
    Replies: 1
    Last Post: 04-07-2003, 10:39 AM