C++控制程序只运行一个实例
1 int APIENTRY WinMain(HINSTANCE hInstance, 2 HINSTANCE hPrevInstance, 3 LPSTR lpCmdLine, 4 int nCmdShow) 5 { 6 HANDLE hMutex = CreateMutex(NULL, false, "win32program"); 7 if (GetLastError() == ERROR_ALREADY_EXISTS) 8 { 9 CloseHandle(hMutex); 10 ::MessageBox(NULL, "程序已经在运行中,不能重复启动!", "提示", MB_OK +MB_ICONWARNING); 11 return 1; 12 } 13 do 14 { 15 Sleep(1000); 16 } while (true); 17 return 0; 18 }