c++ Builder中如何让程序只运行一个实例
//包含此文件确保值创建一个实例
HANDLE __stdcall CrnGetValidMutex() {
HANDLE hMutex1 = ::CreateMutex(NULL, TRUE, TEXT("DataDigWuxuping"));
if (::GetLastError() == ERROR_ALREADY_EXISTS) {
if (hMutex1)
::CloseHandle(hMutex1);
return NULL;
}
else
return hMutex1;
}
//使用方法
---------------------------------------------------------------------------
//WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int) {
// HANDLE hMutex = NULL;
// try {
// hMutex = CrnGetValidMutex();
// if (!hMutex) {
// // ::MessageBox(NULL, TEXT("已经有1个实例运行了!"),
// // TEXT("提醒"), MB_OK | MB_ICONWARNING);
//
// return -1;
// }
//
// Application->Initialize();
// Application->MainFormOnTaskBar = true;
// Application->CreateForm(__classid(TForm1), &Form1);
// Application->Run();
// }
// catch (Exception &exception) {
// Application->ShowException(&exception);
// }
// catch (...) {
// try {
// throw Exception("");
// }
// catch (Exception &exception) {
// Application->ShowException(&exception);
// }
// }
// ::ReleaseMutex(hMutex);
// ::CloseHandle(hMutex);
// return 0;
//}
---------------------------------------------------------------------------
上面的比较简单,其它的方式都是浮云!