run single instance of app
-
I have an application (A) that will execute my application (B) from its menu. When the menu item is selected, a method within my app called Notify will be called to execute the program. It is a COM interface. The problem that I am having is when the menu item is selected, application B executes and creates a named mutex object. The application window does not appear but it proceeds to the Notify method where the application executes once again and tries to create the same mutex object name. It fails because the mutex object already exists. If the mutex was created in the first run, why didn't the application start up? Here is what I have below:
STDMETHODIMP CWrapper::Notify(DataNotificationType notifyType, VARIANT data)
{
STARTUPINFO stStartUpInfo;
PROCESS_INFORMATION *pProcessInfo = new PROCESS_INFORMATION;memset(&stStartUpInfo, 0, sizeof(STARTUPINFO));
stStartUpInfo.cb = sizeof(STARTUPINFO);
stStartUpInfo.dwFlags = STARTF_USESHOWWINDOW;
stStartUpInfo.wShowWindow = SW_SHOWDEFAULT;CreateProcess(NULL, "C:\\\\Program Files\\\\XXX\\\\zzz.exe", NULL, NULL, FALSE, NORMAL\_PRIORITY\_CLASS, NULL, NULL, &stStartUpInfo, pProcessInfo);
return S_OK;
}BOOL CABCApp::InitInstance()
{//*** Run only one instance
mutex = ::CreateMutex(NULL, TRUE, named_mutex);DWORD dwMutexErr = GetLastError(); if (dwMutexErr == ERROR\_ALREADY\_EXISTS) { CWnd \*pPrevWnd = CWnd::GetDesktopWindow()->GetWindow(GW\_CHILD); while (pPrevWnd) { if ( ::GetProp(pPrevWnd->GetSafeHwnd(), named\_mutex ) { if ( pPrevWnd->IsIconic() ) pPrevWnd->ShowWindow(SW\_RESTORE); pPrevWnd->SetForegroundWindow(); pPrevWnd->GetLastActivePopup()->SetForegroundWindow(); return FALSE; } pPrevWnd = pPrevWnd->GetWindow(GW\_HWNDNEXT); } return FALSE; }
Return TRUE;
}Thanks!