// // // #include #include #include #include "SingleApplication.h" HANDLE SingleApplication::_uniq = NULL; std::string SingleApplication::_appid; SingleApplication::SingleApplication(const char* appid) { assert(appid != NULL); _appid = appid; } SingleApplication::~SingleApplication() { if (_uniq) CloseHandle(_uniq); } bool SingleApplication::IsFirstInstance() { if (_uniq) return true; else { _uniq = CreateMutex(NULL, TRUE, _appid.c_str()); if (!_uniq) throw InitFailed(); else { if (GetLastError() == ERROR_ALREADY_EXISTS) { InitSecondInstanceSafely(); ReleaseMutex(_uniq); CloseHandle(_uniq); _uniq = NULL; return false; } else { InitFirstInstanceSafely(); ReleaseMutex(_uniq); return true; } } } }