This is a common test case, proposed by most of the Test Engineers for MFC applications. Yes, really its a good scenario. 🙂

By default MFC provide no APIs or methods to satisfy this. But, by using MUTEX, the work is so simple.

For this we need a GUID (Globally Unique Identifier). You can create this by using the MFC application

GUIDGEN.EXE.

So the steps is as follows.

1. Create a GUID

2. Implement one method for eg:

BOOL CThreadTestApp::SingleTest (LPSTR szName)
{
        HANDLE hMutex = CreateMutex (NULL, TRUE, szName);
        if (GetLastError() == ERROR_ALREADY_EXISTS)
        {
           CloseHandle(hMutex);
           return FALSE;
        }
return TRUE;
}

3. Call the method in InitInstance() like

if (SingleTest (_T("SingleTest_48C56927-A0DB-4e31-8C32-FE15FBA45043")))
{
}
else
{
   AfxMessageBox(_T("Error: application is already running!"));
   return FALSE;
}

Ok na?

🙂

Advertisements