Forcing Only One Instance of an Application to Run at a Time in Visual C++

This document is the easist way to check for multiple instances and bring the first instance on to the focus.

#pragma comment(linker, "/SECTION:.shr,RWS")
#pragma data_seg(".shr")
HWND g_hWnd = NULL;
#pragma data_seg()

On the App::InitInstance() add the code below:

//For single instance checking
if (g_hWnd)
{
  ::SetForegroundWindow(g_hWnd);
  if (IsIconic(g_hWnd)) ::ShowWindow(g_hWnd, SW_RESTORE);
  // terminates the creation
}

After Creating the Main Frame, get the handle and store it on the shared variable below.

g_hWnd = m_pMainWnd->m_hWnd;


Comments

  • how could i use this in SDI

    Posted by Legacy on 03/03/2003 12:00am

    Originally posted by: Winch Fang

    ?_?

    Reply
  • Code Update using CreateMutex

    Posted by Legacy on 01/17/2003 12:00am

    Originally posted by: Simon Soosai

    This document is the easist way to check for multiple instances and bring the first instance on to the focus.
    
    

    //Step 1 ***********************************
    //Top of the App .cpp file
    #pragma comment(linker, "/SECTION:.shr,RWS")
    #pragma data_seg(".shr")
    HWND g_hWnd = NULL;
    #pragma data_seg()

    //Step 2 ***********************************
    //On the App::InitInstance() add the code below:
    //For single instance checking
    HANDLE hMutexOneInstance = ::CreateMutex( NULL, TRUE, _T("AFX_Ideas_H__5B5F33E8_2175_4C23_BB38_A334CADD6B88"));
    bool AlreadyRunning = (GetLastError() == ERROR_ALREADY_EXISTS);
    if (hMutexOneInstance != NULL) ::ReleaseMutex(hMutexOneInstance);
    if (AlreadyRunning)
    {
    if (g_hWnd) /* pop up */
    {
    ::SetForegroundWindow(g_hWnd);
    if (IsIconic(g_hWnd)) ::ShowWindow(g_hWnd, SW_RESTORE);
    }
    return FALSE; // terminates the creation
    }

    //Step 3 ***********************************
    //After Creating the Main Frame, get the handle and store it on the shared variable below.

    g_hWnd = m_pMainWnd->m_hWnd;


    Reply
  • Is it thread safe & high security?

    Posted by Legacy on 01/15/2003 12:00am

    Originally posted by: Minh Tuan

    The code is nice but I think there are 2 problems:
    1. Should we use the InterlockedExchange function to get/set the window handle? Because this function is thread & process safe.
    2. I think it's not suitable for Window Services because other untrusted applications can modify our shared variable. For example, they may terminate or forbit our application to start.

    Reply
  • Easiest != Safest

    Posted by Legacy on 01/15/2003 12:00am

    Originally posted by: Blake Miller

    "This document is the easist way to check for multiple instances and bring the first instance on to the focus"
    He makes no claims that it is the safest or most reliable...

    Reply
  • Good article,thanks.

    Posted by Legacy on 01/14/2003 12:00am

    Originally posted by: Artem

    And by the way, thank you very much for discovering to
    me - what is the 'beast' - "pragma <....>".
    Good luck,man!

    Reply
  • Not thread safe, use mutex instead

    Posted by Legacy on 01/14/2003 12:00am

    Originally posted by: Brian Friesen

    If you need to solve the problem of a single instance application, please use one of the mutex based solutions. It's the only way to guarantee it is thread-safe.

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

Top White Papers and Webcasts

  • Live Event Date: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • You probably have several goals for your patient portal of choice. Is "community" one of them? With a bevy of vendors offering portal solutions, it can be challenging for a hospital to know where to start. Fortunately, YourCareCommunity helps ease the decision-making process. Read this white paper to learn more. "3 Ways Clinicians can Leverage a Patient Portal to Craft a Healthcare Community" is a published document owned by www.medhost.com

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds