|
//
// 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.