c++ 或者 vc++中判断程序实例是否运行

在Main 函数中Create*函数,以便创建一个命名对象(创建的是什么对象无所谓)。当Create * 函数返回时,调用GetLastError函数。 如果GetLastError函数返回ERROR_ALREADY_EXISTS,那么你的应用程序的另一个实例正在运行,新实例可以退出。

int WINAPI WinMain(HINSTANCE hinstExe,HINSTANCE,PSTR pszCmdLine,int nCmdShow){

HANDLE h=CreateMutex(NULL,FALSE,"{FA53433-EEEEE-32332-323-3323-32233223}");

if(GetLastError()==ERROR_ALREADY_EXISTS){

return(0);

}

//this is the first instance of this appliction running.

...

CloseHandle(h);

return(0);

}

 

posted on 2010-03-31 22:11  袁婉轩  阅读(857)  评论(0编辑  收藏  举报

导航