vc++防止多次运行
#include<windows.h>
#include<stdio.h>
int main(int argc,char *argv[])
{
HANDLE hMutex;
DWORD dwErr;
hMutex=CreateMutex(NULL,//无安全描述
FALSE,//没有所有者
"HACK");//对象名
if(hMutex==NULL)//hMutex为NULL标示创建失败
{
MessageBox(NULL,"CreateMutex error!",NULL,MB_OK);
}
else
{
dwErr=GetLastError();
if(dwErr==ERROR_ALREADY_EXISTS)//互斥对象已经存在
{
MessageBox(NULL,"Application has already running",NULL,MB_OK);
ExitProcess(0);
}
}
getchar();
return 0;
}