Thread: Disabling multiple instances of a program

  1. #1
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630

    Disabling multiple instances of a program

    Ok im really mad right now b/c for some reason MSDN keeps crashing my computer! So I was wondering if someone could help me figure this out. I was wondering how I would go about checking if another instance of my program was running, and could stop a second from being started. I have an idea, that you have to perform some kind of check before anything else like registering the window and such, but I have no idea how to implement this. Any help or links to code is much appreciated.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  2. #2
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    You have a couple of choices. If your application creates a window, you can use the FindWindow() function to manually seek out the other instance and shut the current instance down without much trouble. Another approach would be to create a static signal that would automatically tell you if another instance was running. Mutexes are commonly used as such signs, and figuring out whether the same program was running would be as simple as observing the return value from creating the mutex. Both techniques are explained in further detail (complete with code) at http://www.bcbdev.com/faqs/faq74.htm.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try a search


    http://www.cprogramming.com/cboard/s...t=one+instance

    My example works, try it.
    "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

  4. #4
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Hmm, I like the mutex method, nice and simple! I was just wondering though, in the few examples ive seen, some call ReleaseMutex() followd by CloseHandle(), is this necessary or can I just call ReleaseMutex?
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    From msdn for CreateMutex:

    >>Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.<<

    I use CreateMutex and don't bother with closing the handle, leaving it to the system to do that for me. Works fine.

  6. #6
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    I think that's only true if the application terminates normally, but it it crashes for any reason, I think that the mutex is left in the system which can be a bad thing.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I use Mutexes for this, (and have done so for many years), never destroy them, and have never had a problem.

    Mind you, my programs don't crash.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  3. Replies: 5
    Last Post: 08-14-2007, 10:34 AM
  4. multiple instances and variables
    By FOOTOO in forum Windows Programming
    Replies: 1
    Last Post: 04-07-2005, 10:54 AM
  5. Replies: 18
    Last Post: 12-05-2003, 12:06 PM