View Full Version : How to launch an app from a window app
jeanl
13th June 2005, 01:53
Guys,
I've been using the C/C++ ShellExecute() to launch external apps from a windows app, but I'm looking for a version that would block until the app returns (this way I can tell that the app is done processing). Any hint? (must be C/C++)
Thanks!
Jeanl
stax76
13th June 2005, 04:13
usually you do this by calling CreateProcess and WaitForSingleObject
jeanl
13th June 2005, 07:30
Yes, I just found out! Thanks for the pointer though...
This is what I'm doing, in case anybody is interested...
PROCESS_INFORMATION ProcInfo;
STARTUPINFO StrtupInfo;
GetStartupInfo(&StartupInfo);
CreateProcess(m_PathToApp,Arguments, 0, 0, 1, NORMAL_PRIORITY_CLASS, 0, 0, &StartupInfo, &ProcInfo);
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
jeanl
esby
13th June 2005, 14:55
I think You could have used shellExecEx() directly.
esby
jeanl
13th June 2005, 18:25
I think you're right, but you still need to pass a structure to have access to the process handle to use WaitForSingleObject() (shellExecEx returns immediately).
jeanl
esby
14th June 2005, 09:54
It is delphi code, but it was directly transposed from c.
var ProgramName,
Parameters:string;
ShowCmd: Integer ;
SEInfo: TShellExecuteInfo;
begin
ShowCmd := SW_SHOWNORMAL;
ProgramName := '"' + executable_path +'"'; // call to an object property
Parameters := generateCommandOption; // executable + options are put by this method
FillChar(SEInfo, SizeOf(SEInfo), 0);
SEInfo.cbSize := SizeOf(TShellExecuteInfo);
with SEInfo do begin
fMask := SEE_MASK_NOCLOSEPROCESS ;
Wnd := Application.Handle;
lpFile := PChar(ProgramName);
lpParameters := PChar(Parameters);
nShow := ShowCmd;
end;
ShellExecuteEx(@SEInfo);
repeat
Application.ProcessMessages;
GetExitCodeProcess(SEInfo.hProcess, ExitCode);
until (ExitCode <> STILL_ACTIVE) or Application.Terminated;
if ExitCode = STILL_ACTIVE then writeLog('Exit Code found');
This way you can check that what you launched with shellExecEx finished or not.
esby
Enots_
14th June 2005, 10:03
As a rule of thumb on when to use either CreateProcess() or ShellExecute[Ex]() the CreateProcess is the lower-level api which will provide you with more flexibility and control - the ShellExecute() is the higher level api which will give you a user experience closer to that of Start->Run.... When shelling out to do a mux or mpeg encode or something with no user feedback my choice would always be CreateProcess - where I to start an internet explorer I'd go with ShellExecute()...
DarkDudae
14th June 2005, 11:18
As a rule of thumb on when to use either CreateProcess() or ShellExecute[Ex]() the CreateProcess is the lower-level api which will provide you with more flexibility and control - the ShellExecute() is the higher level api which will give you a user experience closer to that of Start->Run.... When shelling out to do a mux or mpeg encode or something with no user feedback my choice would always be CreateProcess - where I to start an internet explorer I'd go with ShellExecute()...
I am agree.
jeanl
14th June 2005, 17:53
thanks for all these clarifications guys!
jeanl
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.