CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Guest

    Single Instance of an Application Running

    I have a C++ application which must have only one instance of it running at any time.
    If the application (.exe) is invoked when one instance is already running, then
    it must not start up the application.


  2. #2
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Single Instance of an Application Running

    In your InitInstance() method, check the m_hPrevInstance member of your app. It will be NULL if this is the first instance, or non-NULL if there is an instance already running. You should return FALSE from InitInstance() to terminate the app before it gets going. You could do something like this:

    ---
    BOOL CMyApp::InitInstance()
    {
    if (m_hPrevInstance != NULL)
    {
    // Here you might want to find the existing instance. You would do this by
    // calling the CWnd::FindWindow() function with the name of your app's
    // title bar (and if you can access it, its main window's class name), and
    // then activating that window with ShowWindow().

    return FALSE ; // Terminate this instance immediately.
    }

    // The usual garbage...

    return TRUE ;
    }


    ---

    Does this help?


    --
    Jason Teagle
    [email protected]

  3. #3
    Join Date
    Apr 1999
    Posts
    48

    Re: Single Instance of an Application Running

    I generally enforce this by using a mutex.

    const LPCTSTR MutexName=_T("amutexname");

    m_hApplicationMutex=CreateMutex(NULL,FALSE,MutexName);
    if(NULL!=m_hApplicationMutex)
    {
    // Connected up to the Mutex OK.
    // Can we get ownership of it ok?
    DWORD dwResult=WaitForSingleObject(m_hApplicationMutex,0);
    if(WAIT_TIMEOUT==dwResult)
    {
    // Couldn't get the Mutex, Another instance is running...

    You should be careful to use a statistically unique name for the mutex (e.g. one containing an ascii form of a CLSID).
    You should release the mutex at the end of the program, but the O.S. will actually do that for you anyway.


    You can also do it by using FindWindow to look for another window with the same title, but this fails if the window title can change within the same application. Also there's probably a unlocked out gap before the window is created at startup.

    I'd be interested if anyone knows better ways.


    Cheers,
    Roger



  4. #4
    Join Date
    Apr 1999
    Posts
    48

    Re: Single Instance of an Application Running

    I thought that m_hPrevInstance was always NULL in Win32? But I could be wrong...

    Roger


  5. #5
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Single Instance of an Application Running

    Oops, you are correct. I'm a die-hard 16-bit programmer still, so I was thinking from that point of view. It's a bit stupid for Windows to no longer use that parameter, that's what the bloody thing is for.

    My apologies to the original author.



    --
    Jason Teagle
    [email protected]

  6. #6
    Join Date
    Apr 1999
    Posts
    48

    Re: Single Instance of an Application Running

    : It's a a bit stupid for Windows to no longer use that parameter

    Absolutely, you'd think there'd be some way it could be connected up under the WIN32 regime. Maybe it's something to do with security, but can't immediately think what.




  7. #7
    Join Date
    Apr 1999
    Posts
    18

    Re: Single Instance of an Application Running

    Another way to enforce a single instance is to use the following method (more details can be found in the Codeguru site).

    In the app.cpp place the following code :

    #pragma data_seg("Shared")
    bool g_bIsLoaded=false;
    #pragma data_seg()
    #pragma comment(linker,"/section:Shared,rws")

    Then in the InitInstance function :

    if (g_bIsLoaded)
    {
    return NULL;
    }
    else
    g_bIsLoaded = true;

    Hope that this helps.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured