single instance dialog

Posted on 2006-03-22
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

    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

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
    Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
    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.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now