Thread: Prevent Multiple Instances

  1. #1
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256

    Prevent Multiple Instances

    Anybody know how I would go about detecting other instances of a program, to ensure that only one instance is ever running?
    Code:
    void function(void)
     {
      function();
     }

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    The common technique is to use mutexes.

    http://msdn.microsoft.com/en-us/libr...27(VS.85).aspx

    Simply CreateMutex() when the program starts and CloseHandle() on exit (optional). If CreateMutex() fails and GetLastError() returns ERROR_ALREADY_EXISTS, then you know one instance is already running.
    Last edited by maxorator; 01-14-2009 at 03:10 PM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Did you try a search?

    8 years ago I posted code to do this with file mapping. Many others have posted similar code.
    "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
    Join Date
    Apr 2007
    Posts
    137
    First codes on Win32 mutexes appeared more than 8 years ago
    Google Groups : first reference : 1995... : 14 years ago...

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is there a really good reason for preventing multiple instances? Many users will hate it if they don't have the option to run multiple instances if they wish.
    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
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Well, in this case, it's a pseudo-standalone screensaver, which you can activate on command in addition to letting Windows activate it for you. It wouldn't really hurt anything, I suppose, to have multiple instances, but I like to be neat. Thanks, guys.
    Code:
    void function(void)
     {
      function();
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple instances and variables
    By FOOTOO in forum Windows Programming
    Replies: 1
    Last Post: 04-07-2005, 10:54 AM
  2. Multiple instances of the same object.
    By suzakugaiden in forum Game Programming
    Replies: 12
    Last Post: 03-19-2005, 11:49 PM
  3. multiple instances of cin
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2004, 04:51 PM
  4. Multiple instances of form resource
    By knutso in forum Windows Programming
    Replies: 4
    Last Post: 05-25-2004, 03:47 PM
  5. multiple object instances at runtime
    By mrukok in forum C++ Programming
    Replies: 1
    Last Post: 03-25-2003, 07:26 AM