Thread: How do you only allow one instance of an app?

  1. #1
    Registered User
    Join Date
    Jan 2002

    Question How do you only allow one instance of an app?

    Sorry, I'm not all that good at windows programming here.
    But if I am programming for windows 2000/XP the traditional way, how would I prevent the user from initiating multiple instances of my application?

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001

    #include <windows.h>

    HWND FindWindow(
    LPCTSTR lpClassName, // pointer to class name
    LPCTSTR lpWindowName // pointer to window name

    at startup of your programs try this

    if it returns a window then the app is running close the program.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    train spotter
    Join Date
    Aug 2001
    near a computer
    Try this at the begining of your WinMain function

    hMapping = CreateFileMapping((HANDLE)0xffffffff, NULL, PAGE_READONLY, 0, 32, szAppName);
    if (hMapping)		// Check to see if app is already running, if so exit
       if (GetLastError() == ERROR_ALREADY_EXISTS)
           sprintf(sBuffer,"%s is already running.",szAppName);
           MessageBox(NULL, sBuffer,  "Error", MB_ICONERROR | MB_OK);
       MessageBox(NULL, "Error creating file mapping.", "Error",MB_ICONERROR| MB_OK);
    "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
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    the normal way of doing this is with mutexes.
    go to msdn and do a search for CreateMutex()
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    There's quite a good article here -

    about avoiding multiple instances.

  6. #6
    Simplified way to do it, add the following to the begining of your WinMain.

    HANDLE hMutex;

    hMutex = CreateMutex(NULL, FALSE, "myAppMutex");

    if (hMutex) {

    if (GetLastError() == ERROR_ALREADY_EXISTS) {

    return 0;


    This is just a REAL SIMPLE way of not starting the app if its already running.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Thanks for all the help. I went with a FindWindow solution but set my class name to some really long and unique name. I know that this was discredited as having race conditions, but in all fairness, i think the likelihood of such conditions ever arising to be slim to nill. And I don't want to spend that much time on such a simple task.

    Ofcourse, all help was appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another question :O need ideas and confirmation
    By Akkernight in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 03:29 PM
  2. Which one is "quicker" ?
    By AloneInTheDark in forum C# Programming
    Replies: 2
    Last Post: 02-08-2008, 09:23 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. best program to start
    By gooddevil in forum Networking/Device Communication
    Replies: 4
    Last Post: 05-28-2004, 05:56 PM
  5. pasword app
    By GanglyLamb in forum C Programming
    Replies: 2
    Last Post: 06-07-2003, 10:28 AM