Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. run single instance of app

run single instance of app

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elephantstar
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups