Welcome

Wheresoever you go, go with all your heart. (Confucius)

10/11/2011

Single instance in MFC

Sometimes you want to make your application only with a single instance.
Unfortunately MFC don't provide any APIs. However, there is the simplest method by MUTEX.

First, you create the globally unique identifier (GUID) to create MUTEX.
MFC provides creating GUID: Tools-Create GUID.
after creating it, just copy and paste in your code.

The following code located in InitInstance() of MFC.

CString stringGUID = L"........";
HANDLE hMutex = CreateMutex(NULL, TRUE, stringGUID);
if( GetLastError() == ERROR_ALREADY_EXISTS )
{
    CloseHandle(hMutex);
    return FALSE;
}

No comments: