Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Dialog based single instance applications

0.00/5 (No votes)
8 Mar 2006 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here