CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 1999
    Posts
    5

    One Instance of App

    I have asked about this problem before, and was given information, but unfortunalty did not help. I need to only run a single instance of a application using MFC, it must be very accurate.

    Can anybody help???


  2. #2
    Join Date
    Jun 1999
    Posts
    319

    Re: One Instance of App

    In the InitInstance you should try to find a handle to the window of your app, if it is running (FindWindow("MyClassWnd", NULL)). If the returned handler is NULL that means that there is no application running. If not, this means that you already run your application so return FALSE from InitInstance.
    Second method. There is memeber in the CWinApp (m_hInstance). This memeber indicate if you have a previous instance. Again if this is not NULL return FALSE from InitInstance.
    Or you can use a share file (GlobaleHandler) to determine a previous instance.

    Let me know if this helps you.
    Best regards,
    Faby


  3. #3
    Guest

    Re: One Instance of App

    Unfortunately, the "FindWindow" method is NOT reliable...and only works IF the window is already created and visible...which does you no good if someone clicks on the icon to launch your app several times quickly....so here is what you need to do:

    In your InitInstance do this:

    // Check for existing opened application
    HANDLE hMutex = CreateMutex( NULL, FALSE, _T("AppNameGoesHere") );
    if ( hMutex && (GetLastError() == ERROR_ALREADY_EXISTS ))
    {
    CheckWindow();
    return FALSE;
    }

    Your CheckWindow function looks like this:

    void CYourApp::CheckWindow()
    {
    // Check for existing opened application
    HWND FirstHwnd;
    HWND FirstChildHwnd;

    if(FirstHwnd = FindWindow("WndClassName", NULL))
    {
    FirstChildHwnd = GetLastActivePopup( FirstHwnd );
    BringWindowToTop( FirstHwnd );
    ::ShowWindow( FirstHwnd, SW_RESTORE );

    if(FirstHwnd != FirstChildHwnd)
    {
    BringWindowToTop( FirstChildHwnd );
    }
    }
    return;
    }

    This way, if they click fast, the CreateMutex code prevents another copy from being run and once the window is actually visible, the CheckWindow code can do its job, RELIABLY. Plus, if it is already runing, it will be brought into the foreground.

    This method has worked 100% for us. Hope this helps.


  4. #4
    Join Date
    Jun 1999
    Posts
    5

    Re: One Instance of App

    Fabi,

    Thankyou for your reply to my problem!!

    I have tried this before, and it does not work, I am creating a Dialog Based App, compiling under NT4.0 if this helps.

    I have checked m_hInstance, this does not work also searched for the handle with FindWindow(), have you anymore suggestions.

    The only other answer is that I write to the registery and use this each time, but it could very dangerous, in the case of a system crash.

    I need a failsafe way to have a single instance running at one time.....

    Thankyou

    Shaun Garratt


  5. #5
    Join Date
    Jul 1999
    Location
    Moscow, Russia
    Posts
    6

    Why THIS did not help, if you tried?

    http://www.codeguru.com/misc/single_instance.shtml

    I use it absolutely successfully.
    If you have any trouble with this approach - ask, I'll try to help.

    Alex.


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