Load the following function at the beginning of the program, such as in the initialization of the MFC program
//////////////////////////////////////////////////////////////////////////
Function Name: Program_mutex
Function: Ensure that only instances of the program are unique
Return value: 0-normal; 1-there is already a running instance;-1-Failed to create mutex object
//////////////////////////////////////////////////////////////////////////
int Program_mutex ()
{
HANDLE Hmutex = NULL;
TCHAR * lpszName = L "Appmutex";
int nret = 0;
Do
{
Hmutex =:: CreateMutex (Null,false,lpszname);
DWORD dwret =:: GetLastError ();
switch (dwret)
{
Case
0:
{
Break
;
}
case error_already_exists:
{
MessageBox (null,l" Notification: The application is already open.) \ n ", L" notice ", MB_OK);
nret = 1;
break;
}
default:
{
MessageBox (null,l" Hint: The application failed to create a mutex object. \ n ", L" error ", MB_OK);
nret =-1;
break;
}
}
} while (FALSE);
return nret;
}
C + + ensures that the program has only unique instances