Single Instance of an Application Class (2)

This article is actually a modification of the original article "Single Instance of an Application Class" by Kevin Lussier.

Kevin used the following piece of code to restore the application after a second instance is started:

.....
HWND hWnd = ::FindWindowEx( NULL, NULL, m_strClassName, NULL );
if ( hWnd != NULL ) {
    ::ShowWindow( hWnd, SW_RESTORE );        // This is modified
    ::BringWindowToTop( hWnd );
    ::SetForegroundWindow( hWnd );
}
// Return failure
return FALSE;

This works fine!

But, if your application (actually main window) contains modeless dialog boxes, they are not going to be restored. In order to allow the application to handle ::ShowWindow(...), several modifications are made.

Step 1:

Public static data member is added to class declaration. This will contain a message identifier of the registered window message. The class declaration now becomes:

class TSingleInstance {
    public:
        static UINT MsgId;            // New data member
    protected:
        HANDLE HMutex;
        CString ClassName;
    public:
        TSingleInstance();
        ~TSingleInstance();
        BOOL Create(UINT nID);
        CString GetClassName(void) const;
};

Step 2:

Message is registered in a constructor of TSingleInstance.

UINT TSingleInstance::MsgId = 0;
TSingleInstance::TSingleInstance()
{
    HMutex = NULL;
    MsgId = ::RegisterWindowMessage("SingleInstanceOfMyApplication");
}

Step 3:

Now, previous piece of code becomes the following:

.....

HWND hWnd = ::FindWindowEx( NULL, NULL, m_strClassName, NULL );
if ( hWnd != NULL ) {
    if (MsgId != 0) {        // In case ::RegisterWindowMessage() failed
        DWORD recipients = BSM_APPLICATIONS;
        ::BroadcastSystemMessage(BSF_POSTMESSAGE,&recipients,MsgId,0,0);
    } else {
        ::ShowWindow(hWnd, SW_RESTORE);
        ::BringWindowToTop( hWnd );
        ::SetForegroundWindow( hWnd );
    }
}

// Return failure
return FALSE;

Step 4:

Main application window will receive this message and has to handle it. In order to supply the handler for registered message, following line has to be added to a message map.

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    //{{AFX_MSG_MAP(CMainFrame)
    ON_REGISTERED_MESSAGE(TSingleInstance::MsgId,OnSingleInstanceRestore)
    ....
END_MESSAGE_MAP()
and the following line should be added to a declaration of CMainFrame class:
....
afx_msg LRESULT OnSingleInstanceRestore(WPARAM, LPARAM);
....

Step 5:

The implementation of this function should restore the main window and all other modeless dialog boxes (if they exist). Minimal implementation should only restore the main window, bring it to top and set it as a foreground window (the same as in the original implementation).

LRESULT CMainFrame::OnSingleInstanceRestore(WPARAM,LPARAM)
{
    ShowWindow(SW_RESTORE);
    // Do whatever else is necessary. Restore modeless dialogs etc.
    BringWindowToTop();
    SetForegroundWindow();
    return 0;
}

Step 6:

Everything else is the same as in the original implementation. This includes the following (see original article):

  • Add an instance of TSingleInstance class to a public section of your CWinApp derived class.
  • Add a code to CMainFrame::PreCreateWindow(CREATESTRUCT& cs).

Code:

Here is the complete implementation (I'm using different naming convention). Code in bold is a difference between this and original implementation by Kevin:

//====================================================================================
// Include file (.h)
class TSingleInstance {
    public:
        static UINT MsgId;
    protected:
        HANDLE HMutex;
        CString ClassName;
    public:
        TSingleInstance();
        ~TSingleInstance();
        BOOL Create(UINT nID);
        CString GetClassName(void) const;
};

//====================================================================================
// Source code (.cpp)
UINT TSingleInstance::MsgId = 0;
TSingleInstance::TSingleInstance()
{
    HMutex = NULL;
    MsgId = ::RegisterWindowMessage(_T("SingleInstanceOfMyApplication"));
}

TSingleInstance::~TSingleInstance()
{
    if (HMutex != NULL)
        ::ReleaseMutex(HMutex);
}

BOOL TSingleInstance::Create(UINT nID)
{
    CString strFullString;
    // Create our class name string
    if (strFullString.LoadString(nID)) {
        // Extract the first sub-string
        ::AfxExtractSubString(ClassName, strFullString, 0);
    }
    // Add the word 'Class' to the end
    ClassName += _T(" Class");
    // Create the mutex
    HMutex = ::CreateMutex(NULL, FALSE, ClassName);
    // Check for errors
    if (::GetLastError() == ERROR_ALREADY_EXISTS) {
        // Reset our mutext handle (just in case)
        HMutex = NULL;
        // The mutex already exists, which means an instance is already
        // running. Find the app and pop it up
        HWND hwnd = ::FindWindowEx(NULL, NULL, ClassName, NULL);
        if (hwnd != NULL) {
            if (MsgId != 0) {        // In case ::RegisterWindowMessage() failed
                DWORD recipients = BSM_APPLICATIONS;
                ::BroadcastSystemMessage(BSF_POSTMESSAGE,&recipients,MsgId,0,0);
            } else {
                ::ShowWindow(hwnd, SW_RESTORE);
                ::BringWindowToTop(hwnd);
                ::SetForegroundWindow(hwnd);
            }
        }
        // Return failure
        return FALSE;
    }
    // Register the unique window class name so others can find it.
    WNDCLASS wndcls;
    memset(&wndcls, 0, sizeof(WNDCLASS));
    wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
    wndcls.lpfnWndProc = AfxWndProc;
    wndcls.hInstance = ::AfxGetInstanceHandle();
    wndcls.hIcon = ::LoadIcon(wndcls.hInstance, MAKEINTRESOURCE(nID));//or AFX_IDI_STD_FRAME;
    wndcls.hCursor = ::LoadCursor(wndcls.hInstance, IDC_ARROW);
    wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    wndcls.lpszMenuName = NULL;             // You may need to fix this
    wndcls.lpszClassName = ClassName;       // My class name
    // Register name, exit if it fails
    if (!::AfxRegisterClass(&wndcls)) {
        //::AfxMessageBox( _T("Failed to register window class!"), MB_ICONSTOP | MB_OK );
        TRACE0(_T("TSingleInstance: Failed to register window class!\n"));
        return FALSE;
    }
    // Return success
    return TRUE;
}

CString TSingleInstance::GetClassName(void) const
{
 return ClassName;
}
//====================================================================================

Last updated: 17 May 1998.



Comments

  • Single Instance of an Application(2)

    Posted by Legacy on 09/17/2002 12:00am

    Originally posted by: Renato Leite

    Works perfectly. Thanks a lot

    Reply
  • SetForegroundWindow(HWND) doesn't work

    Posted by Legacy on 12/14/2001 12:00am

    Originally posted by: Luc Bloom

    Ok my window gets the message, cause it is un-minimized and flashed, but it dosn't come to the foreground... any ideas why?

    Reply
  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • You've managed to piece together the "DREAM TEAM." Your tech staff works like a well-oiled machine, keeping your company humming and thriving. And then it happens: dissension. For whatever reason, your employees have grown unhappy and you find out they're searching for new employment or losing productivity. What did you do wrong? Did you hire the wrong people? Did your company push them away? Or is it a combination of numerous factors? Read this white paper to learn how to build an environment that fosters …

  • Microsoft® Office 365 is a top choice for enterprises that want a cloud-based suite of productivity/ collaboration applications. With Office 365, you get access to Microsoft™ Office solutions practically anytime, anywhere, on virtually any device. It's a great option for current Microsoft users who can now build on their experience with Microsoft™ solutions while enjoying the flexibility of a cloud-based delivery. But even organizations with no previous investment in Microsoft will find that …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date