函数原型:
HANDLE CreateMutex(
LPSECURITY_ATTRIBUTES lpMutexAttributes,//必须为NULL
BOOL bInitialOwner, //表示mutex的范围是本线程还是全系统
LPCTSTR lpName //互斥量的名字
);
bool checkMyselfExist()//如果程序已经有一个在运行,则返回true
{
HANDLE hMutex = CreateMutex(NULL, FALSE, L"DevState");
if (hMutex && (GetLastError() == ERROR_ALREADY_EXISTS))
{
CloseHandle(hMutex);
hMutex = NULL;
return true;
}
else{
return false;
}
}
int main()
{
if (checkMyselfExist()){
cout << "program has been runnin" << endl;
}
else{
cout << "start running" << endl;
}
system("pause");
return 0;
}