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

//首先在InitInstance()函数中创建一个同步对象,如果(GetLastError()返回ERROR_ALREADY_EXISTS就表明存在
  //一个应用实例了,返回false取消
  // 创建同步对象


//By cryfish
 hMutexOneInstantance=CreateMutex(NULL,TRUE,_T("PreventSecondInstance"));
 if(GetLastError()==ERROR_ALREADY_EXISTS)
  bFound=TRUE;
 if(hMutexOneInstantance)
  ReleaseMutex(hMutexOneInstantance);
 if (bFound==TRUE)
 { ::AfxMessageBox("You have Run one");
 return false;
 }

//首先在InitInstance()函数中创建一个同步对象,如果(GetLastError()返回ERROR_ALREADY_EXISTS就表明存在
  //一个应用实例了,返回false取消
  // 创建同步对象


//By cryfish
 hMutexOneInstantance=CreateMutex(NULL,TRUE,_T("PreventSecondInstance"));
 if(GetLastError()==ERROR_ALREADY_EXISTS)
  bFound=TRUE;
 if(hMutexOneInstantance)
  ReleaseMutex(hMutexOneInstantance);
 if (bFound==TRUE)
 { ::AfxMessageBox("You have Run one");
 return false;
 }

原文地址:https://www.cnblogs.com/tianlangshu/p/1989509.html