 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
fiaola Guest
|
Posted: Mon Apr 12, 2004 8:06 pm Post subject: Switch to another running App from my Delphi program |
|
|
Hi,
I have an application that RELIES on another program for some data. When i
execute my program, i have to start this other application first and then
start my Delphi program.
From a procedure in Delphi, if a certain situation occurs, i would like to
automatically pop up this application so the user can do some checking.
Is this possible? I know of ShellExecute and have tried it but I dont get
the results i need.
Thanks in Advance.
|
|
| Back to top |
|
 |
Bill Todd (TeamB) Guest
|
Posted: Tue Apr 13, 2004 12:45 am Post subject: Re: Switch to another running App from my Delphi program |
|
|
You can use CreateProcess.
function LaunchApp(const AppName: String): Boolean;
var
ProcessInfo: TProcessInformation;
StartUpInfo: TStartupInfo;
begin
result := False;
try
FillMemory( @StartupInfo, sizeof(StartupInfo), 0);
StartupInfo.cb := sizeof(StartUpInfo);
result := CreateProcess(Nil, PChar(AppName),Nil, Nil, False,
NORMAL_PRIORITY_CLASS, Nil, Nil,StartUpInfo,ProcessInfo);
finally
CloseHandle (ProcessInfo.hProcess);
CloseHandle (ProcessInfo.hThread);
end;
end;
--
Bill (TeamB)
(TeamB cannot respond to questions received via email)
|
|
| Back to top |
|
 |
fiaola Guest
|
Posted: Tue Apr 13, 2004 3:18 am Post subject: Re: Switch to another running App from my Delphi program |
|
|
Thanks Bill. This opens the launching application multiple times. What i'm
trying to do here is to switch (change focus) to the OTHER application that
is already running, but NOT to OPEN another incident of the same
application.
Thanks for the reply.
"Bill Todd (TeamB)" <no (AT) no (DOT) com> wrote
| Quote: | You can use CreateProcess.
function LaunchApp(const AppName: String): Boolean;
var
ProcessInfo: TProcessInformation;
StartUpInfo: TStartupInfo;
begin
result := False;
try
FillMemory( @StartupInfo, sizeof(StartupInfo), 0);
StartupInfo.cb := sizeof(StartUpInfo);
result := CreateProcess(Nil, PChar(AppName),Nil, Nil, False,
NORMAL_PRIORITY_CLASS, Nil, Nil,StartUpInfo,ProcessInfo);
finally
CloseHandle (ProcessInfo.hProcess);
CloseHandle (ProcessInfo.hThread);
end;
end;
--
Bill (TeamB)
(TeamB cannot respond to questions received via email)
|
|
|
| Back to top |
|
 |
Bill Todd (TeamB) Guest
|
Posted: Tue Apr 13, 2004 2:14 pm Post subject: Re: Switch to another running App from my Delphi program |
|
|
There is a Windows API call you can use to get the handle of a running
app but I cannot remember it at the moment. Search the nativeapi
newsgroup or post a message there.
--
Bill (TeamB)
(TeamB cannot respond to questions received via email)
|
|
| Back to top |
|
 |
Jan Ferguson Guest
|
Posted: Wed Apr 14, 2004 3:32 am Post subject: Re: Switch to another running App from my Delphi program |
|
|
Bill Todd (TeamB) wrote:
| Quote: | There is a Windows API call you can use to get the handle of a running
app but I cannot remember it at the moment. Search the nativeapi
newsgroup or post a message there.
--
Bill (TeamB)
(TeamB cannot respond to questions received via email)
|
Bill,
Here's one way to accomplish it... This gets placed in the project file.
begin
HPrevInst := FindWindow('TFormMain', nil);
if HPrevInst = 0 then
begin
Application.Title := 'My Program';
Application.CreateForm(TFormMain, FormMain);
Application.Run;
end else
begin
MessageDlg('The program is already running!', mtError, [mbOK], 0);
Windows.SetFocus(HPrevInst);
Windows.SetForegroundWindow(HPrevInst);
end;
end.
--
Regards,
Jan Ferguson
|
|
| Back to top |
|
 |
Bill Todd (TeamB) Guest
|
Posted: Mon Apr 19, 2004 11:44 pm Post subject: Re: Switch to another running App from my Delphi program |
|
|
Look up FindWindow in the Windows SDK help file on the Delphi Help
menu. It finds the Window by title and class (the class is optional)
and returns the handle.
--
Bill (TeamB)
(TeamB cannot respond to questions received via email)
|
|
| Back to top |
|
 |
fiaola Guest
|
Posted: Tue Apr 20, 2004 10:35 am Post subject: Re: Switch to another running App from my Delphi program |
|
|
Thanks Jan and Bill.
Where do i get the information about the HANDLE of the running application?
Example, NotePad, what is the window handle for NotePad?
Thanks.
"Jan Ferguson" <janfergusonATdatasoftwareDOTnet> wrote
| Quote: | Bill Todd (TeamB) wrote:
There is a Windows API call you can use to get the handle of a running
app but I cannot remember it at the moment. Search the nativeapi
newsgroup or post a message there.
--
Bill (TeamB)
(TeamB cannot respond to questions received via email)
Bill,
Here's one way to accomplish it... This gets placed in the project file.
begin
HPrevInst := FindWindow('TFormMain', nil);
if HPrevInst = 0 then
begin
Application.Title := 'My Program';
Application.CreateForm(TFormMain, FormMain);
Application.Run;
end else
begin
MessageDlg('The program is already running!', mtError, [mbOK], 0);
Windows.SetFocus(HPrevInst);
Windows.SetForegroundWindow(HPrevInst);
end;
end.
--
Regards,
Jan Ferguson
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|