// Copyright (c) // All rights reserved. // //Describe : Only can run single program. //Author : Qing //Date : 2014-08-14 #ifndef __SINGLE_PROGRAM_H__ #define __SINGLE_PROGRAM_H__ #include <string> #include <Windows.h> namespace Qing { class SingleProgram { public: SingleProgram(void) : m_Mutex(NULL) { } ~SingleProgram(void) { MandatoryDestoryInstance(); } bool CreateSingleInstance(const std::string &ProgramName) { m_Mutex = ::CreateMutexA(NULL, TRUE, ProgramName.c_str()); DWORD LastError =::GetLastError(); if(m_Mutex != NULL) { ::ReleaseMutex(m_Mutex); } if(LastError == ERROR_ALREADY_EXISTS) { m_Mutex = NULL; return false; } return true; } void MandatoryDestoryInstance() { if(m_Mutex != NULL) { ::CloseHandle(m_Mutex); m_Mutex = NULL; } } private: SingleProgram(const SingleProgram&); SingleProgram& operator=(const SingleProgram&); private: HANDLE m_Mutex; }; } #endif