/* 二重起動を禁止します。 以下のように使ってください。 (1) CMutex mu("SAMPLE WINDOW"); if(mu.IsMutex()) return;//終了 (2) CMutex mu; mu.Set("SAMPLE WINDOW"); if(mu!=0) return;//終了 */ class CMutex { HANDLE hMutex; int flag; public: CMutex() { hMutex=NULL; Set("DefaultMutex"); } CMutex(char* name) { hMutex=NULL; Set(name); } void Set(char *name) { Delete(); hMutex = CreateMutex(NULL, true, name); if(GetLastError() == ERROR_ALREADY_EXISTS ) flag=1;//既に作成していた else flag=0; } operator int(void) { return flag; } int IsMutex(){ return flag; } ~CMutex() { Delete(); } private: void Delete() { if(hMutex){ ReleaseMutex(hMutex); CloseHandle(hMutex); } } };