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





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
Question:
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.
Answer:
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);
::SetForegroundWindow(FirsthWnd);
FirstChildhWnd = ::GetLastActivePopup(FirsthWnd);
if (FirsthWnd != FirstChildhWnd)
::BringWindowToTop(FirstChildhWnd); // A pop-up is active so bring it to the top too.
}
else
{
Application->Initialize();
Application->Title = "YourApplicationTitle";
Application->CreateForm(__classid(TYourAppMainForm),&YourAppMainForm);
}
12/30/99

Last Modified: 01-SEP-99