分类: C/C++
2008-05-27 20:40:31
有时候为了避免不必要的错误,应防止同一应用程序被打开两个实例
以下一个函数可以达到此项目的,挺有用的!
BOOL C××App::AlreadyRunning()
{
BOOL bFound = FALSE;
// Try to create a mutex with the app's name
HANDLE hMutexOneInstance = ::CreateMutex(NULL,TRUE,_T(AfxGetAppName()));
// Already there...means that we are already running an instance
if(::GetLastError() == ERROR_ALREADY_EXISTS)
bFound = TRUE;
// Release the mutex
if(hMutexOneInstance)
::ReleaseMutex(hMutexOneInstance);
return(bFound);
}
只要在
BOOL C**App::InitInstance()
{
// Is it already running?
if(AlreadyRunning())
{
// Yep...get out now
AfxMessageBox(IDS_ALREADY_RUNNING,MB_ICONWARNING);
return(FALSE);
}
。。。。。
}