single instance dialog

Posted on 2006-03-22
Medium Priority
Last Modified: 2013-11-20
I have a dialog application and I only want a single instance of it running at one time.  How can I do this.  I have tried these articles and they didn't seem to work for me:
I must be missing something obvious so I've decided to take a step back and pretend I didn't read any of them and ask how to do it.
Question by:docieb
LVL 49

Accepted Solution

DanRollins earned 2000 total points
ID: 16261480
Those techniques *do* work.  Here is my version:

#ifndef LimitSingleInstance_H
#define LimitSingleInstance_H

#include <windows.h>

//this code is from Q243953 in case you lose the article and wonder
//where this code came from...
class CLimitSingleInstance
  DWORD  m_dwLastError;
  HANDLE m_hMutex;

  CLimitSingleInstance(TCHAR *strMutexName)
    //be sure to use a name that is unique for this application otherwise
    //two apps may think they are the same if they are using same name for
    //3rd parm to CreateMutex
    m_hMutex = CreateMutex(NULL, FALSE, strMutexName); //do early
    m_dwLastError = GetLastError(); //save for use later...
    if (m_hMutex)  //don't forget to close handles...
       CloseHandle(m_hMutex); //do as late as possible
       m_hMutex = NULL; //good habit to be in

  BOOL IsAnotherInstanceRunning()
    return (ERROR_ALREADY_EXISTS == m_dwLastError);
#endif  //#ifndef LimitSingleInstance_H


Then in the app...

CLimitSingleInstance g_SingleInstanceObj( "CreditBaseApp" ); // checked in InitInstance

in InitInstance...

   //------------------------------------ avoid running two copies
    if ( g_SingleInstanceObj.IsAnotherInstanceRunning() ) {
        CWnd* pWnd=  CWnd::FindWindow( NULL, CSTR_AppWinTitle );
        if ( pWnd )   {
              CWnd* pWndChild= pWnd->GetLastActivePopup(); // If so, does it have any popups?
            // if ( pWnd->IsIconic() ) {            // If iconic, restore the main window
            if ( !pWnd->IsWindowVisible() ) {            // If iconic, restore the main window
                    pWnd->ShowWindow(SW_RESTORE); // tray app stuff
              pWndChild->SetForegroundWindow(); // Bring the main window or its popup to the foreground
        return FALSE; // dont start this instance
.... else, run normally ...

-- Dan

Author Comment

ID: 16261980
Ahh, dumb me.  I figured out what I was doing.  I forgot that I changed the folder that I was placing the release in and I kept trying it with older version.  I used yours and it works and the others probably works also.  Man the last few hours was a waste of time.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question