Click here to Skip to main content
13,260,383 members (44,757 online)
Click here to Skip to main content
Add your own
alternative version


51 bookmarked
Posted 1 Apr 2002

There can be only one...

, 1 Apr 2002
Rate this:
Please Sign up or sign in to vote.
Limiting an application to a single instance.


When reading Joseph Newcomer's article about this matter, I thought there must be a way to do this simpler. So based on his excellent work the past few years I've spent all my time devoted on this theorem. In the mean time I never saw any daylight and I've lost all my friends (Just kidding' ;)...

But after this long research and keeping it secret for too long I finally bring out the goods just to make sure such an important discovery like this doesn't fade away in history... uhum...

So here we go:

  1. We need a registered message that our application will respond to. Make this one global in your application.
    static const UINT WM_CHECK_ITS_ME = RegisterWindowMessage("MY_UNIQUE_GUID");
  2. Now we create a mutex somewhere at the start of your program:
    HANDLE hMutexOneInstance = CreateMutex( NULL, FALSE, _T("MY_UNIQUE_GUID"));
  3. Check the last error to see what happened:
    bool AlreadyRunning = ( GetLastError() == ERROR_ALREADY_EXISTS || 
                               GetLastError() == ERROR_ACCESS_DENIED);
  4. Then we send out a message to all top level windows to make sure it pops up and becomes the active window. The trick here is to use HWND_BROADCAST, which is a little bit undocumented.
    if ( AlreadyRunning )
     DWORD result;
     LRESULT ok = SendMessageTimeout(HWND_BROADCAST,
                                           0, 0, 
                                           SMTO_BLOCK |
     return FALSE; // Here we quit this application
     // or exit(0);
     // or PostQuitMessage() ... etc...
  5. The already running application needs to respond the registered window message, so we add a message handler to the application.

    If you are using MFC edit your messagemap:
    or WTL:
    or plain Windows API:
    LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
      if (msg == WM_CHECK_ITS_ME)
        return 0;
      return ...;
  6. Now in the handler for this message we just make sure this application becomes the foreground and active one.
    LRESULT CMainFrame::OnCheckItsMe(...)
    // If we are using Chris Maunder's CSystemTray it's handy
    // or else you can call all sorts of restore/maximise functions:
     - ShowWindow(SW_RESTORE);
     - SetForegroundWindow();
     - SetWindowPlacement(m_MyWindowPlacement);
     return WM_CHECK_ITS_ME;
  7. That should work!


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


About the Author

Niek Albers
Web Developer
Netherlands Netherlands
Niek is the founder and programmer of and is working on many projects all the time. He makes a living by doing contractwork for others.

You may also be interested in...


Comments and Discussions

Questionwait for exit of previous app? Pin
hobnob26-Apr-13 0:49
memberhobnob26-Apr-13 0:49 
QuestionSome needed extensions Pin
KarstenK4-Jan-07 23:51
memberKarstenK4-Jan-07 23:51 
GeneralUsing event object will be simpler... Pin
opsoft-op6-Dec-05 0:59
memberopsoft-op6-Dec-05 0:59 
GeneralStill the same old problem! Pin
Daniel Lohmann19-Jul-02 8:07
memberDaniel Lohmann19-Jul-02 8:07 
GeneralRe: Still the same old problem! Pin
andy_kanaka17-Aug-02 18:23
sussandy_kanaka17-Aug-02 18:23 
GeneralRe: Still the same old problem! Pin
Daniel Lohmann17-Aug-02 22:52
memberDaniel Lohmann17-Aug-02 22:52 
GeneralAccess violation Pin
CapsuleDweller29-May-02 9:33
memberCapsuleDweller29-May-02 9:33 
GeneralRe: Access violation Pin
Anonymous29-May-02 9:32
memberAnonymous29-May-02 9:32 
GeneralRe: Access violation Pin
Carl Ferreira11-Jul-02 6:05
memberCarl Ferreira11-Jul-02 6:05 
GeneralRe: Access violation Pin
Jan Deinhard5-Sep-02 4:27
memberJan Deinhard5-Sep-02 4:27 
GeneralRe: Access violation Pin
xxxyyyzzz22-Dec-02 20:02
memberxxxyyyzzz22-Dec-02 20:02 
QuestionWhere to register the message?? Pin
cycosi9-Apr-02 6:17
membercycosi9-Apr-02 6:17 
AnswerRe: Where to register the message?? Pin
Trapper23-Apr-02 8:05
memberTrapper23-Apr-02 8:05 
GeneralOK but not perfect Pin
Philippe Lhoste8-Apr-02 4:47
memberPhilippe Lhoste8-Apr-02 4:47 
GeneralRe: OK but not perfect Pin
Andreas Saurwein14-May-02 22:26
memberAndreas Saurwein14-May-02 22:26 
QuestionIs this way good? Pin
Daniel Madden8-Apr-02 1:53
memberDaniel Madden8-Apr-02 1:53 
AnswerRe: Is this way good? Pin
Philippe Lhoste8-Apr-02 4:31
memberPhilippe Lhoste8-Apr-02 4:31 
GeneralRe: Is this way good? Pin
Daniel Madden8-Apr-02 9:23
memberDaniel Madden8-Apr-02 9:23 
GeneralThanks! Pin
Anonymous2-Apr-02 12:16
memberAnonymous2-Apr-02 12:16 
GeneralDialogs Pin
Atlantys2-Apr-02 3:35
memberAtlantys2-Apr-02 3:35 
GeneralRe: Dialogs Pin
Niek Albers2-Apr-02 4:11
memberNiek Albers2-Apr-02 4:11 
GeneralScary Pin
Chris Maunder2-Apr-02 1:23
adminChris Maunder2-Apr-02 1:23 
GeneralRe: Scary Pin
Josh Knox4-Apr-02 7:00
memberJosh Knox4-Apr-02 7:00 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 2 Apr 2002
Article Copyright 2002 by Niek Albers
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid