Menu

Preventing multiple instances of an App

2004-06-07
2012-09-26
  • Brad Kartchner

    Brad Kartchner - 2004-06-07

    In case it matters, I'm using Dev-C++ 4.9.8.7 (and LOVING it).

    I'm looking for a way to prevent multiple instances of my application from running at the same time.  Instead of starting a new instance of an application if one is already running, I need the original window to popup automatically. 

    I have found a few code examples for doing this, but most of them  require using MFC.  I did find an article on doing this on developerfusion titled "Avoiding Multiple Instances of an Application" (http://www.developerfusion.com/show/1716/), which has a very clear example using a named mutex to detect multiple instances, and a shared data segment to store the handle of the main window.  This is done with the following code:

    #pragma comment(linker, "/SECTION:.shr,RWS")
    #pragma data_seg(".shr")
    HWND hGlobal = NULL;
    #pragma data_seg

    Using the code in this article works to the extent that a second instance of my application will NOT start, but I have been unable to have the original instance's window popup like it should.  I suspect it has to do with the #pragma directives (which I freely admit, I do NOT understand). 

    Can anyone help me?  Is there another way to do this or am I out of luck?

     
    • Nobody/Anonymous

          // Verify that we are not already running...

              // Create mutex...
              CreateMutex(NULL, TRUE, "MyProgram");

                  // Already running, terminate...
                  if(GetLastError() == ERROR_ALREADY_EXISTS)
                      ExitProcess(1);

      =)

      Kip

       
    • Derek Baker

      Derek Baker - 2004-06-07

      I implemented this myself just a few days ago.

      HANDLE Mutex = CreateMutex(NULL, TRUE, "DBClip");

             if (GetLastError() != ERROR_SUCCESS)
             {
              CloseHandle(Mutex);
               
              return 0;   
             }

      Change DBClip to what ever you want, as long as it's likely to be unique.

      I put this at the start of WinMain, so the return terminates the program.

      Derek

       
    • Derek Baker

      Derek Baker - 2004-06-07

      Two points:

      The CloseHandle(Mutex); in my mine isn't really necessary, as Windows will delete the mutex when the app terminates.

      Some people say check for ERROR_SUCCESS as I do, rather than ERROR_ALREADY_EXISTS, for example: http://tinyurl.com/23fsr

      Derek

       
    • qWake

      qWake - 2004-06-08

      Your application will register its own window class, right?  Then use something like this in WinMain:

      HWND existingAppWindow = FindWindow("MyClassName", 0);
      if (IsWindow(existingAppWindow)) {
          if (IsIconic(existingAppWindow)) {
              ShowWindow(existingAppWindow, SW_RESTORE);
          }
          BringWindowToTop(existingAppWindow);
          SetForegroundWindow(existingAppWindow);
      }

      qWake

       
    • Derek Baker

      Derek Baker - 2004-06-08

      There are those that say use don't use FindWindow: http://tinyurl.com/2rz4o

      Derek

       
      • qWake

        qWake - 2004-06-08

        Good point, I never thought of the race condition.  I shall change my evil ways (and a couple of program lines).

        qWake

         
    • Brad Kartchner

      Brad Kartchner - 2004-06-08

      So, does anyone know if it's possible to use a shared memory segment in Dev-C++, as the tutorial tries to do?

       
    • Nobody/Anonymous

      Yes. Just use GlobalAlloc() for a structure of some sort.

      Kip

       
    • Liviu Nicolescu

      Liviu Nicolescu - 2004-06-09

      It seems that it's possible to use shared sections with gcc:

      int foo __attribute__((section ("shared"), shared)) = 0;

      quoted from

      http://gcc.gnu.org/onlinedocs/gcc-3.4.0/gcc/Variable-Attributes.html#Variable%20Attributes

      Liviu

       
    • Brad Kartchner

      Brad Kartchner - 2004-06-09

      That's insanely usefull and exactly what I was looking for. I didn't even know that site existed.  Thanks to everyone for the responses!

       
    • Nobody/Anonymous

      so cool!

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.