HANDLE hMutex; hMutex = CreateMutex(NULL, FALSE, "QSoftObjName"); if (GetLastError() == ERROR_ALREADY_EXISTS) { MessageBox(NULL, "You can open only one instance of <app name>!", "<app name>", MB_OK); CloseHandle(hMutex);
// The original code in the line above was "ReleaseMutex"
// which is wrong because this thread didn't create this mutex.
// Thanks again Torando...15/01/03 return(0); }
ReleaseMutex(hMutex); // Ofcourse, don't forget to clean up, thanks Tornado for reminding me :) //BTW- There are a few other ways to achieve this goal...But this is the easiest one IMHO. |