I am trying to launch a program using ShellExecute, then wait to execute
the next code until the launched program finishes. Any ideas? I tried
using WaitForSingleObject, but I can't figure out the syntax, and I
don't know what to test for.
Thanks.
Jesse Strachman
TIE Commerce
--
**********************************************
TIE COMMERCE
Jesse Strachman
Lead Development Software Engineer
6 New England Executive Park
Burlington, MA 01803-5080
Phone (781) 203-4452
Fax (781) 270-5485
E-mail Address: jstra...@tiecommerce.com
Internet Home Page: http://www.tiecommerce.com
**********************************************
Heres the code...
Hope it helps,
Paul Gent
function WinExecAndWait(FileName, CommandLine : string;
Visibility : Integer) : Integer;
// Name : WinExecAndWait
// Description : Runs / Executes a program and waits for it to finish
// before returning from function
// Inputs : Filename - Name of file to execute
// Returns : -1 if the Exec failed, otherwise returns the processes
// exit code when the process terminates
var
cmdline:string;
CurDir:array[0..255] of char;
WorkDir:ShortString;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
Temp : DWORD;
{!!.D4}
begin
if filename='' then
begin
result:=-1;
exit;
end;
cmdline:=filename+' ';
if length(commandline) > 0 then
if commandline[1] = ' ' then
cmdline:=cmdline+copy(commandline,2,length(commandline))
else
cmdline:=cmdline+commandline;
GetDir(0, WorkDir);
StrPCopy(CurDir, WorkDir);
FillChar(StartupInfo, Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(nil, pchar(cmdline), nil, nil, false,
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,
nil, nil, StartupInfo, ProcessInfo) then
Result := -1 // error condition
else
begin
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Temp);
{!!.D4}
Result := Integer(Temp);
{!!.D4}
end;
end;
Jesse Strachman schrieb:
>
> Greetings.
>
> I am trying to launch a program using ShellExecute, then wait to execute
> the next code until the launched program finishes. Any ideas? I tried
> using WaitForSingleObject, but I can't figure out the syntax, and I
> don't know what to test for.
--
Robert Marquardt (Team JEDI) http://delphi-jedi.org
function WinExecAndWait32(FileName:String; Visibility:integer):DWORD;
var { by Pat Ritchey }
zAppName:array[0..512] of char;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
StrPCopy(zAppName,FileName);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(nil,
zAppName, { pointer to command line string }
nil, { pointer to process security
attributes}
nil, { pointer to thread security
attributes }
false, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block
}
nil, { pointer to current directory name
}
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) { pointer to PROCESS_INF }
then Result := DWORD(-1)
else
begin
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Result);
CloseHandle( ProcessInfo.hProcess );
CloseHandle( ProcessInfo.hThread );
end;
end;
--
Charles Hacker
Lecturer in Electronics and Computing
School of Engineering
Griffith University - Gold Coast
>I am trying to launch a program using ShellExecute, then wait to execute
>the next code until the launched program finishes. Any ideas?
ShellExecuteEx
It accepts a pointer to a data structure containing the handle to the
process called and can be used with WaitForSingleObject.
--
Andrea Laforgia (UIN 87230455)
----------------------------------------------------------
Non scrivetemi per e-mail, se non richiesto espressamente.
No replies in private e-mail, unless explicitly requested.
----------------------------------------------------------