To make the application run only one instance, a simple method is to use mutex in the application class, which can be the GUIDGEN under VC. EXE program generation. GUIDGEN. EXE is located in the CommonTools directory of the VC installation directory.
Instance
1: Create a project ex1 Based on the dialog box and use the default settings.
2: Use GUIDGEN. EXE to generate a global flag, # define one "generated global flag"
The statements in this example are as follows: # define one "0xbe8e2ce1, 0xdab6, 0x11d6, 0xad, 0xd0, 0x0, 0xe0, 0x4c, 0x53, 0xf6, 0xe6"
3: In the application class CEx1App: InitInstance (), use the CreateMutex function to create a mutex, and then call the GetLastError () function ()
If the result is equal to ERROR_ALREADY_EXISTS, it indicates that an instance is running and FALSE is returned. BOOL CEx1App: InitInstance () {handle =: CreateMutex (NULL, FALSE, one); // handle is the declared global variable of the HANDLE type if (GetLastError () = ERROR_ALREADY_EXISTS)
{
AfxMessageBox ("the application is already running ");
Return FALSE;
}
}
4: In CEx1App: ExitInstance (), delete the mutex int CEx1App: ExitInstance (){
CloseHandle (handle );
Return CWinApp: ExitInstance ();
}
& Nbs