We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


single instance dialog

docieb asked
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.
Watch Question

Author of the Year 2009
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.