Try this one. I have just wrapped a version of "WinExecAndWait32". This one DEFINITELY
waits correctly.
HTH
Chris
procedure ExecuteProgram(sPath: string; bWait: Boolean);
function WinExecAndWait32(Path: PChar; Visibility: Word;
Timeout: DWORD): integer;
var
WaitResult: integer;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
with StartupInfo do
begin
cb := SizeOf(TStartupInfo);
dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
{ you could pass sw_show or sw_hide as parameter: }
wShowWindow := visibility;
end;
if CreateProcess(nil, path, nil, nil, False,
NORMAL_PRIORITY_CLASS, nil, nil,
StartupInfo, ProcessInfo) then
begin
WaitResult := WaitForSingleObject(ProcessInfo.hProcess, timeout);
{ timeout is in miliseconds or INFINITE if
you want to wait forever }
result := WaitResult;
end
else
{ error occurs during CreateProcess see help for details }
result := GetLastError;
end;
begin
SetCurrentDir(ExtractFilePath(Application.ExeName)); {Set this so relative paths
will work}
if not FileExists(sPath) then begin
DlgError('Unable to run "' + sPath + '"'+NL+'The program cannot be found.');
end else begin
if bWait then begin
WinExecAndWait32(PChar(sPath), SW_SHOW, INFINITE);
end else begin
WinExecAndWait32(PChar(sPath), SW_SHOW, 0);
end;
end;
end;
> -----Original Message-----
> From: vss [mailto:vss@;vss.co.nz]
> Sent: Monday, 11 November 2002 3:08 p.m.
> To: Multiple recipients of list delphi
> Subject: [DUG]: Execute and wait
>
>
> Hi All.
> I have a bit of code that does an execute and waits for the app. to
> close.
> What I want to do is execute a game and know when its
> finished, BUT when
> I use my code, it starts to execute the app, but then it stops and
> returns an error code of zero which means its finished...BUT it never
> started.
> This is teh code I use. anyone got anything better?
>
> Jeremy
>
>
> function TfrmMain.WinExecAndWait32(Path: PChar; Visibility: Word):
> integer;
> var Msg: TMsg;
> lpExitCode: cardinal;
> StartupInfo: TStartupInfo;
> ProcessInfo: TProcessInformation;
> begin
> FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
> with StartupInfo do
> begin
> cb := SizeOf(TStartupInfo);
> dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
> wShowWindow := visibility; {you could pass sw_show or sw_hide as
> parameter}
> end;
>
> if CreateProcess(nil, path, nil, nil, False, NORMAL_PRIORITY_CLASS,
> nil, nil, StartupInfo,
> ProcessInfo) then
> begin
> repeat
> while PeekMessage(Msg, 0, 0, 0, pm_Remove) do
> begin
> if Msg.Message = wm_Quit then Halt(Msg.WParam);
> TranslateMessage(Msg);
> DispatchMessage(Msg);
> end;
> GetExitCodeProcess(ProcessInfo.hProcess,lpExitCode);
> until lpExitCode <> Still_Active;
>
> with ProcessInfo do {not sure this is necessary but seen
> in in some
> code elsewhere}
> begin
> CloseHandle(hThread);
> CloseHandle(hProcess);
> end;
> Result := 0; {success}
> end else Result := GetLastError;
> end;
>
> --------------------------------------------------------------
> -------------
> New Zealand Delphi Users group - Delphi List -
> [EMAIL PROTECTED]
> Website: http://www.delphi.org.nz
> To UnSub, send email to: [EMAIL PROTECTED]
> with body of "unsubscribe delphi"
> Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
>
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/