让程序只运行一个实例的简单实用的方法

时间:2023-01-24 13:57:29
//
//只能运行一个实例
HANDLE hMutexOneInstantance=CreateMutex(NULL,TRUE,_T("PreventSecondInstance"));
BOOL bFound=FALSE;
if(GetLastError()==ERROR_ALREADY_EXISTS)
bFound=TRUE;
if(hMutexOneInstantance)
ReleaseMutex(hMutexOneInstantance);
if (bFound==TRUE)
{
//::AfxMessageBox("您已经运行了一个实例");
return false;
}