Главная страница | назад

Article #18816: Preventing multiple instances with BCB

 Question and Answer Database

FAQ3816C.txt   Preventing multiple instances with BCB
Category   :Windows API
Platform    :Win95/NT
Product    :   All32Bit

I want to permit only one copy of my program to run at a time.  The Previous
Instance is always set to NULL, yet it is supposed to be NULL only the first
time you run the program.

This is a code example of using a Mutex to determine if another
instance is running.

::CreateMutex(NULL, FALSE, "YourMutexName");
    if ( GetlastError() == ERROR_ALREADY_EXISTS )
        // Found another running application with the same mutex
        // so one instance is already running.
        Application->Title="Testing";  // prohibits finding this instance
        HWND FirsthWnd, FirstChildhWnd;
        // see Application->Title or Options|Project|Application Tab|Title
        FirsthWnd = ::FindWindow("TApplication", "YourApplicationTitle"); 
        if (::IsIconic(FirsthWnd))
            ::ShowWindow(FirsthWnd, SW_SHOWDEFAULT);
        FirstChildhWnd = ::GetLastActivePopup(FirsthWnd);
        if (FirsthWnd != FirstChildhWnd)
            ::BringWindowToTop(FirstChildhWnd); // A pop-up is active so bring it to the top too.
        Application->Title = "YourApplicationTitle";


Last Modified: 01-SEP-99