Board index » cppbuilder » Activating the first instance of a program when it's started multiple times

Activating the first instance of a program when it's started multiple times


2005-04-22 01:15:56 PM
cppbuilder52
Hello,
I have a program that I want it to act like Outlook Express in the sense
that when the program is minimized or in the tray, and when user clicks on
the desktop icon, instead of relaunching a second instance, it should bring
the first instance to screen (window should be visible).
I know this is possible and I would appreciate if someone could shed some
light on my problem.
Best Regards,
SZ
 
 

Re:Activating the first instance of a program when it's started multiple times

On Fri, 22 Apr 2005 08:15:56 +0300, SubZero wrote:
Quote
I know this is possible and I would appreciate if someone could shed some
light on my problem.
Google is your friend:
www.google.com/search
snipurl.com/e68m
--
Good luck,
liz
 

Re:Activating the first instance of a program when it's started multiple times

Ok then how will activate and bring the old process's main form to front?
Just like OE.
Best Regards,
SZ
"Liz Albin" < XXXX@XXXXX.COM >wrote in message
Quote
On Fri, 22 Apr 2005 08:15:56 +0300, SubZero wrote:

>I know this is possible and I would appreciate if someone could shed some
>light on my problem.

Google is your friend:

www.google.com/search

snipurl.com/e68m


--
Good luck,

liz
 

{smallsort}

Re:Activating the first instance of a program when it's started multiple times

SubZero wrote:
Quote
Ok then how will activate and bring the old process's main form to front?
Use FindWindow() seaching the text in the Title of the mainwindow
of that app. Then use ::BringWindowToTop(). (untested).
Hans.
 

Re:Activating the first instance of a program when it's started multiple times

But the title of the main window changes during run-time.
Regards,
SZ
"Hans Galema" < XXXX@XXXXX.COM >wrote in message
Quote
SubZero wrote:
>Ok then how will activate and bring the old process's main form to front?

Use FindWindow() seaching the text in the Title of the mainwindow
of that app. Then use ::BringWindowToTop(). (untested).

Hans.
 

Re:Activating the first instance of a program when it's started multiple times

SubZero wrote:
Quote
But the title of the main window changes during run-time.
Does the first part stay the same maybe ? Or could you implement that ?
Hans.
 

Re:Activating the first instance of a program when it's started multiple times

HANDLE mutex;
WINAPI WinMain(HINSTANCE hCurInstance, HINSTANCE hPrevInstance, LPSTR
lpCmdLine, int nCmdShow)
{
try
{
mutex = ::CreateMutex(&sa, true, "sirconv.mutex");
if (::GetLastError() == ERROR_ALREADY_EXISTS)
{
// Find the HWND of the hidden application window of the previous
// instance. Note that we are not looking for the HWND of the main
// form. This code fails miserably if you pass it the main form
// HWND instead of the Application's HWND
Application->Title = "";
HWND hPrevApp = ::FindWindow("TSirconvMainForm", NULL);
if(hPrevApp)
{
::SendMessage(hPrevApp, WM_SYSCOMMAND, SC_RESTORE, 0);
::SetForegroundWindow(hPrevApp);
}
}
else
{
Application->Initialize();
Application->CreateForm(__classid(TSirconvMainForm),
&SirconvMainForm);
Application->Run();
}
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
if(mutex)
::CloseHandle(mutex);
return 0;
}
 

Re:Activating the first instance of a program when it's started multiple times

"SubZero" < XXXX@XXXXX.COM >wrote:
Quote
I have a program that I want it to act like Outlook Express in the sense
that when the program is minimized or in the tray, and when user clicks on
the desktop icon, instead of relaunching a second instance, it should bring
the first instance to screen (window should be visible).
A possibility.
When your program starts, use RegisterWindowMessage() to generate a
special unique-to-you Windows message ID.
Then attempt to create a mutex. If it is created successfully, then
you're the original copy, so run as normal. (Don't forget to release it
at the end.)
If you failed, you're the copy. So use EnumWindows to find all top-level
windows in turn, and send the message to each of them. And then
terminate. Or send it to HWND_BROADCAST (simpler).
Meanwhile, when the original receives the message, it knows that it
should pop up and show itself.
Note - I've not explicitly done this.
Alan Bellingham
--
ACCU Conference 2005 - 20-23 April, Randolph Hotel, Oxford, UK
 

Re:Activating the first instance of a program when it's started multiple times

"SubZero" < XXXX@XXXXX.COM >wrote in message
Quote
But the title of the main window changes during run-time.
The class name should not change, though. FindWindow() can look for class
names as well as window titles. Just make sure to give your form class a
unique name.
Alternative, you can register a custom message via RegisterWindowMessage()
and then broadcast it to all windows. Only your application will know how
to respond to the message, and can then activate itself rather than having
the requesting instance do the activating (which in itself actually has
known problems, so it is better for the first instance to activate itself
anyway).
Gambit
 

Re:Activating the first instance of a program when it's started multiple times

Remy Lebeau (TeamB) wrote:
Quote
The class name should not change, though. FindWindow() can look for class
names as well as window titles. Just make sure to give your form class a
unique name.
But there is still a problem then. The second instance has the same
ClassName(). So how would you distinghuish between the first and
the last ?
When searching for an Application->Title the trick was to temporally
give the Application no Title. How for a ClassName() ?
Hans.
 

Re:Activating the first instance of a program when it's started multiple times

"Hans Galema" < XXXX@XXXXX.COM >wrote in message
Quote
But there is still a problem then. The second instance has the same
ClassName(). So how would you distinghuish between the first and
the last ?
If you look for a form rather than the TApplication window, then you can
call FindWindow() before the second instance ever created any forms
instances. Thus only the first instance's forms will be available to be
found.
Quote
When searching for an Application->Title the trick was to
temporally give the Application no Title. How for a ClassName() ?
The class name of TApplication will always be "TApplication" for any VCL
project. So you don't look for TApplication to begin with. Look for TForm
instance instances instead, since their names can be unique per project.
Gambit
 

Re:Activating the first instance of a program when it's started multiple times

"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
If you look for a form rather than the TApplication window, then you
can call FindWindow() before the second instance ever created any
forms instances. Thus only the first instance's forms will be available
to be found.
If that is not an option, then you cxan alternative give the second
instance's window a unique title and then use FindWindowEx() in a loop until
you find a window that does not have the same unique title.
Gambit
 

Re:Activating the first instance of a program when it's started multiple times

"PaoloItaly" < XXXX@XXXXX.COM >wrote:
Quote

[...]
::SendMessage(hPrevApp, WM_SYSCOMMAND, SC_RESTORE, 0);
This code is unreliable. See my reply to Hans in the thread:
Avoiding multiple programinstances and restore the firstone
~ JD
 

Re:Activating the first instance of a program when it's started multiple times

"SubZero" < XXXX@XXXXX.COM >wrote:
Quote

[...] instead of relaunching a second instance, it should
bring the first instance to screen (window should be
visible).
See my reply to Hans in the thread:
Avoiding multiple programinstances and restore the firstone
~ JD