Click here to Skip to main content
15,887,485 members
Articles / Desktop Programming / Win32
Tip/Trick

How to avoid multiple instances of your Windows application

Rate me:
Please Sign up or sign in to vote.
4.90/5 (9 votes)
27 Sep 2010CPOL1 min read 29K   12   9
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
{
public:
	SingleInstanceGuard(const char *pID):m_pid(pID), m_h(NULL){}
	~SingleInstanceGuard(){CloseHandle(m_h);}
	bool AlreadyRunning()
	{
		if(m_h = OpenEvent(EVENT_ALL_ACCESS, FALSE, m_pid)) 
 		    return true;
		m_h = CreateEvent(NULL, FALSE, FALSE, m_pid);
		return false;
	}
private:
	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.

License

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



Comments and Discussions

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