Click here to Skip to main content
13,513,652 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


12 bookmarked
Posted 27 Sep 2010

How to avoid multiple instances of your Windows application

, 27 Sep 2010
Rate this:
Please Sign up or sign in to vote.
I want to share a simple class I use for this purpose. The class operates on kernel objects.
I have seen this question asked quite a bit with different solutions provided. The ones involving registered window handles or registered messages seem very tedious and code bloat. I use something else that I find to be very simple and I want to share it. It involves a few set of functions, that operate on a kernel object, that I have rolled into a simple reusable class.

I searched and I didn't come across what I use. May be it isn't published. Anyway, it isn't a radically new idea.

Kernel objects are things that can be shared across different applications, and hence multiple instances of one application. The basic idea involves the very first instance of the application creating a kernel object and the others checking for it's existence and when found taking suitable actions.

There are several kinds of kernel objects available but the simplest one that has a very low overhead on the system resources is an event object. This is the one used for thread synchronization but here it serves a different purpose.

The following code is all that it takes.

#include <windows.h>
class SingleInstanceGuard
	SingleInstanceGuard(const char *pID):m_pid(pID), m_h(NULL){}
	bool AlreadyRunning()
		if(m_h = OpenEvent(EVENT_ALL_ACCESS, FALSE, m_pid)) 
 		    return true;
		m_h = CreateEvent(NULL, FALSE, FALSE, m_pid);
		return false;
	const char *m_pid;
	HANDLE m_h;

The function AlreadyRunning() can be implemented in another way too, as follows

bool AlreadyRunning()
    m_h = CreateEvent(NULL, FALSE, FALSE, m_pid);
    return ERROR_ALREADY_EXISTS == GetLastError();

The code listed below demonstrates the usage.

#define TAG "QWERTY-12345"

void main()
	SingleInstanceGuard sig(TAG);
	if(sig.AlreadyRunning()) return;
	MessageBox(NULL, "I am the only one of my kind", "Look", 0);

You may look up MSDN or the web for more details on the terms involved if you aren't familiar with them already.


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


About the Author

You may also be interested in...


Comments and Discussions

General@Aescleal: Thanks. The throwing constructor did cross my min... Pin
bleedingfingers28-Sep-10 20:54
memberbleedingfingers28-Sep-10 20:54 
GeneralReason for my vote of 5 I'd prefer a throwing constructor (w... Pin
Aescleal27-Sep-10 5:16
memberAescleal27-Sep-10 5:16 
General@DaveAuld: That is what I meant when I said there are severa... Pin
bleedingfingers27-Sep-10 2:15
memberbleedingfingers27-Sep-10 2:15 
General@BF: look here as an example; Pin
DaveAuld27-Sep-10 0:57
mentorDaveAuld27-Sep-10 0:57 
General@DaveAuld. I am not sure to what you are referring. Could yo... Pin
bleedingfingers26-Sep-10 21:04
memberbleedingfingers26-Sep-10 21:04 
GeneralI thought one of the preferred methods in c++ (i might be wr... Pin
DaveAuld26-Sep-10 0:57
mentorDaveAuld26-Sep-10 0:57 
GeneralUsing Global in Windows 7 environment Pin
garyt7-Feb-11 5:31
membergaryt7-Feb-11 5:31 
GeneralFew points Pin
Ajay Vijayvargiya26-Sep-10 7:57
memberAjay Vijayvargiya26-Sep-10 7:57 
GeneralRe: Few points Pin
bleedingfingers26-Sep-10 21:05
memberbleedingfingers26-Sep-10 21:05 

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
Web01 | 2.8.180417.1 | Last Updated 27 Sep 2010
Article Copyright 2010 by bleedingfingers
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid