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

Tagged as

Go to top

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
{
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)

Share

About the Author


Comments and Discussions

 
General@Aescleal: Thanks. The throwing constructor did cross my min... Pinmemberbleedingfingers28-Sep-10 20:54 
GeneralReason for my vote of 5 I'd prefer a throwing constructor (w... PinmemberAescleal27-Sep-10 5:16 
General@DaveAuld: That is what I meant when I said there are severa... Pinmemberbleedingfingers27-Sep-10 2:15 
General@BF: look here as an example; http://bcbjournal.org/articles... PinmentorDaveAuld27-Sep-10 0:57 
General@DaveAuld. I am not sure to what you are referring. Could yo... Pinmemberbleedingfingers26-Sep-10 21:04 
GeneralI thought one of the preferred methods in c++ (i might be wr... PinmentorDaveAuld26-Sep-10 0:57 
GeneralUsing Global in Windows 7 environment Pinmembergaryt7-Feb-11 5:31 
GeneralFew points PinmemberAjay Vijayvargiya26-Sep-10 7:57 
GeneralRe: Few points Pinmemberbleedingfingers26-Sep-10 21:05 

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
Web03 | 2.8.140922.1 | Last Updated 27 Sep 2010
Article Copyright 2010 by bleedingfingers
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid