Click here to Skip to main content
15,884,237 members
Articles / Programming Languages / C++

Avoid Multiple Instances of Windows Application with SingleInstanceGuard

Rate me:
Please Sign up or sign in to vote.
3.92/5 (20 votes)
21 Oct 2010CPOL16 min read 53.7K   629   27  
A simple utility class that helps maintain one instance of your application and allows other instances to transfer the data processing to it before they close
// 
// SingleInstanceGuard
//
// This is a simple utility class that enables Windows applications to maintain just one 
// running instance. The other instances can transfer their data to the running instance.
// before closing.
//
// Designed and Coded by Vinay MS aka bleedingfingers
//
// Oct 2 2010
//
// Version 1.0
//
////////////////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <string>

class SingleInstanceGuard
{
public:
	class Exception
	{
	public:
		enum REASON 
		{ 
			DIFFERENT_GUARDS, 
			UNABLE_TO_CHECK,
			UNABLE_TO_READ,
			UNABLE_TO_WRITE,
			UNABLE_TO_ALERT,
			SHARING_WITH_SELF, 
			DATA_TOO_BIG, 
			OPERATION_NOT_SUPPORTED
		};
		REASON m_r;
		Exception(REASON r):m_r(r){}
	};

	// This is the alert message that would be sent to the running instance by the other instances
	static UINT SIGM_ALERT;

	SingleInstanceGuard(const char *pID, long nSharedDataSize=-1);
	~SingleInstanceGuard();	

	bool AlreadyRunning();
	template<class DATA> SingleInstanceGuard& operator << (const DATA &d){return Write((void*)&d, sizeof(d));}
	SingleInstanceGuard& operator << (const std::string &s);
	template<class DATA> bool operator >> (DATA &d){return Read((void*)&d, sizeof(d));}	
	bool operator >> (std::string &s);
	void AlertTheRunningInstance();
	
private:
	SingleInstanceGuard& Write(void *pData, int nize);
	bool Read(void *pData, int nize);
};
// These are unsupported and must not be used.and hence implemented to explicitly disallow
void operator<<(SingleInstanceGuard &t, char *p);
void operator>>(SingleInstanceGuard &t, char *p);

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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



Comments and Discussions