Click here to Skip to main content
Click here to Skip to main content

Dialog based single instance applications

, 8 Mar 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Limiting dialog based programs to single instance by modifying the dialog template – my way.

Introduction

Sometimes it is necessary to limit your program to only one running instance. For example, you create an Internet program that listens to a particular TCP/IP port on the user’s computer, or you create a program that works in the background and puts an icon in the system tray (near the clock).

There are several good articles on CodeProject and CodeGuru discussing different techniques that you can use. For example, you can use named kernel mode objects (mutex, event, etc.), or use Tokens, or you can search for your executable name in the running programs list (EnumProcesses or CreateToolhelp32Snapshot).

In this article, I will discuss using FindWindow for dialog based programs. The problem is that Windows, by default, uses a special (non unique) class name “#32770 (Dialog)” to create dialog boxes. We need to create the dialog box with our unique class name instead of this standard class name.

I will show you:

  1. How you can create a dialog box with your own class name.
  2. How you can use FindWindow to find your dialog box by its class name.

Although I use MFC in this sample, it is not limited to MFC programs only. You can do the same for ATL/WTL based programs as well as for generic Win32 applications.

Implementation

  1. Create a MFC dialog based project.
  2. Open the Resource Script file (.rc), find your main dialog template, and add the following line:
    CLASS "SINGLE_INSTANCE_APP"

    You should have something like this:

    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 our own Windows class "SINGLE_INSTANCE_APP" instead of the standard dialog class.

  3. Now, we 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);
  4. After this, we can find our main dialog box by its class name. A non-NULL pointer means that our program is already running. We will simply activate it and exit.
    CWnd* pWnd = CWnd::FindWindow(_T("SINGLE_INSTANCE_APP"), NULL); 
    if (pWnd) 
    {
         pWnd->ShowWindow(SW_SHOW); 
         pWnd->SetForegroundWindow(); 
         return FALSE; 
    }

Recommendations

Well, there are a few limitations with using FindWindow that you should know about. If you execute some initialization code (that takes a long time) after calling FindWindow but before creating your main dialog box (by calling CDialog::DoModal() or CDialog::Create()), then this technique may fail.

So, you should avoid executing any code that may take a long time between the FindWindow call and creating your main dialog box, and avoid using initialization code in the PreCreateWindow(), OnCreate(), etc. handlers. The OnInitDialog() is the best place for them.

Conclusion

I hope this article was useful for you. I have used this technique in different free and commercial software programs, and it works quite well for me.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
Questionhow to work in a generic Win32 application Pinmemberdaxue14-Oct-07 3:06 
QuestionExternal DLL ressource PinmemberVirginF25-Apr-06 3:11 
AnswerRe: External DLL ressource Pinmemberpkhach25-Apr-06 4:10 
GeneralProbably won't work. PinmemberRandomMonkey9-Mar-06 19:25 
GeneralRe: Probably won't work. Pinmemberpkhach9-Mar-06 22:34 
GeneralRe: Probably won't work. PinmemberRandomMonkey10-Mar-06 4:45 
GeneralRe: Probably won't work. Pinmemberpkhach10-Mar-06 7:35 
GeneralRe: Probably won't work. PinmemberRandomMonkey10-Mar-06 8:48 
GeneralRe: Probably won't work. Pinmemberpkhach10-Mar-06 21:47 
GeneralResource Editor won't keep your manual changes PinmemberJun Du9-Mar-06 6:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 9 Mar 2006
Article Copyright 2006 by pkhach
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid