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

    技术2022-05-11  93

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

      //首先在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; }

     

    最新回复(0)