Sometimes you don’t want to enable the option of running more than one instance of an executable at a time. This can be achieved by using this fragment of code:
void main(){ HANDLE hMutex = CreateMutex(NULL, FALSE, "programName"); // if Create mutex succeeded if (hMutex) { // if we already have mutex with that name it means another instance isrunning if (GetLastError() == ERROR_ALREADY_EXISTS) { cout << "another instance of this program is running" << endl; return; } }}