BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Switch to another running App from my Delphi program

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (Desktop)
View previous topic :: View next topic  
Author Message
fiaola
Guest





PostPosted: Mon Apr 12, 2004 8:06 pm    Post subject: Switch to another running App from my Delphi program Reply with quote



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





PostPosted: Tue Apr 13, 2004 12:45 am    Post subject: Re: Switch to another running App from my Delphi program Reply with 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
fiaola
Guest





PostPosted: Tue Apr 13, 2004 3:18 am    Post subject: Re: Switch to another running App from my Delphi program Reply with quote



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





PostPosted: Tue Apr 13, 2004 2:14 pm    Post subject: Re: Switch to another running App from my Delphi program Reply with 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)
Back to top
Jan Ferguson
Guest





PostPosted: Wed Apr 14, 2004 3:32 am    Post subject: Re: Switch to another running App from my Delphi program Reply with quote

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





PostPosted: Mon Apr 19, 2004 11:44 pm    Post subject: Re: Switch to another running App from my Delphi program Reply with quote

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





PostPosted: Tue Apr 20, 2004 10:35 am    Post subject: Re: Switch to another running App from my Delphi program Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (Desktop) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.