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

  • Thanks to wide spread cloud hosting and innovations small businesses can meet and exceed the legacy systems of goliath corporations. Explore the freedom to work how you want, with a phone system that will adapt to your evolving needs and actually save you lots of expense—read Get an Enterprise Phone System without High Cost and Complexity. The article clearly illustrates: The only hardware you'll need is phone equipment for advanced voice and fax. How to join all your employees, mobile devices, …

  • Do you spend a lot of time thinking about your enemies? Attacker attribution - figuring out who's out to get you - is one of the most important things an organization can do to protect itself.  Because you have no hope of defending yourself if you don't understand who the attackers are. Good news? Every organization isn't targeted by all the attackers. Bad news? No one can identify your potential attackers as well as you. Read this graphics-rich threat summary for 2014 to determine who might be your next …

Most Popular Programming Stories

More for Developers

RSS Feeds

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