How do you only allow one instance of an app?

This is a discussion on How do you only allow one instance of an app? within the Windows Programming forums, part of the Platform Specific Boards category; Sorry, I'm not all that good at windows programming here. But if I am programming for windows 2000/XP the traditional ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    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
    Posts
    2,267
    try

    #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
    Location
    near a computer
    Posts
    3,854
    Try this at the begining of your WinMain function

    Code:
    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);
           ExitProcess(1);
        }
    } 
    else
    {
       MessageBox(NULL, "Error creating file mapping.", "Error",MB_ICONERROR| MB_OK);
       ExitProcess(1);
    }
    "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
    Posts
    2,572
    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 :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

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

    http://www.codeproject.com/cpp/avoidmultinstance.asp

    about avoiding multiple instances.

  6. #6
    Unregistered
    Guest
    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
    Posts
    75
    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, 02:29 PM
  2. Which one is "quicker" ?
    By AloneInTheDark in forum C# Programming
    Replies: 2
    Last Post: 02-08-2008, 08:23 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21