TIP: Dialog-Based, Single-Instance Applications | CodeGuru

TIP: Dialog-Based, Single-Instance Applications

Introduction There are lots of articles on CodeGuru about limiting your program to a single instance. In this article, I want to show how easily it can be done by simply modifying the dialog template and adding few lines of code in InitInstance(). Although I use MFC for this sample, you can use the same […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 24, 2006
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Introduction

There are lots of articles on CodeGuru about limiting your program to a single instance. In this article, I want to show how easily it can be done by simply modifying the dialog template and adding few lines of code in InitInstance(). Although I use MFC for this sample, you can use the same technique for ATL/WTL and generic Win32 applications.

Implementation

  • Create an MFC dialog-based project.
  • Open the Resource Script file (.rc), find your main dialog template, and add the following lines:
    CLASS "SINGLE_INSTANCE_APP"
    
    IDD_SINGLEINSTANCE_DIALOG DIALOGEX 0, 0, 320, 200
       STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS |
             WS_POPUP | WS_VISIBLE |
             WS_CAPTION | WS_SYSMENU
       EXSTYLE WS_EX_APPWINDOW
       CLASS "SINGLE_INSTANCE_APP"
       CAPTION "Single Instance Application"
       ...
    

    This will instruct Windows to use your own windows class “SINGLE_INSTANCE_APP” instead of a standard dialog class.

  • Now, you have to register the “SINGLE_INSTANCE_APP” windows class. The best place for this is InitInstance().
  •    WNDCLASS wc = {0};
       wc.style = CS_BYTEALIGNWINDOW|CS_SAVEBITS|CS_DBLCLKS;
       wc.lpfnWndProc = DefDlgProc;
       wc.cbWndExtra = DLGWINDOWEXTRA;
       wc.hInstance = m_hInstance;
       wc.hIcon = LoadIcon(IDR_MAINFRAME);
       wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
       wc.hbrBackground =
          CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
       wc.lpszClassName = _T("SINGLE_INSTANCE_APP");
       ATOM cls = RegisterClass(&wc);
    
  • After this, you can find your main dialog by its class name.
  • CWnd* pWnd =
       CWnd::FindWindow(_T("SINGLE_INSTANCE_APP"), NULL);
    if (pWnd)
    {
       pWnd->ShowWindow(SW_SHOW);
       pWnd->SetForegroundWindow();
       return FALSE;
    }
    
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.