I have a problem with the WindowsAPI Command "CreateProcess" under WinNT 4.0
and Delphi 5.0!
If I start my application with CreateProcess and wait for Termination with
"WaitForSingleObject" the application hangs and I must kill it with the
TaskManager.
After i'd killed it my Delphi runs on without any errors.
Here' s the code I used:
function ExecAndWait(const aCmdLine: String; aHide, aWait: Boolean):
Boolean;
var
StartupInfo : TStartupInfo;
ProcessInfo : TProcessInformation;
begin
FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
with StartupInfo do
begin
cb:= SizeOf(TStartupInfo);
dwFlags:= STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
if aHide then wShowWindow:= SW_HIDE
else wShowWindow:= SW_SHOWNORMAL;
end;
Result := CreateProcess(nil,PChar(aCmdLine), nil, nil, False,
NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo);
if aWait then
if Result then
begin
WaitForInputIdle(ProcessInfo.hProcess, INFINITE);
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
end;
end;
Michael Riester
function WinExecAndWait32(FileName: String;
Visibility: integer): DWORD;
var
zAppName:array[0..512] of char;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
hd,hdProc : THandle;
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.hThread,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Result);
CloseHandle( ProcessInfo.hProcess );
CloseHandle( ProcessInfo.hThread );
end;
end;
exemple:
WinExecAndWait32 (Paths.SourcePath + FILE_COMPILATION_BATCH_FILE,
SW_SHOW); // wait for dos box to end
prototype
function WinExecAndWait32(Filename: String; Visibility :integer):
Cardinal;
function WinExecAndWait32(Filename: string; Visibility :integer): Cardinal;
var
zAppName: array[0..512] of char;
zProjDir: array[0..512] of char;
zCurDir: array[0..255] of char;
WorkDir :string;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
ProjectDir: String;
begin
ProjectDir := ExtractFilePath(Filename);
StrPCopy(zProjDir, ProjectDir);
StrPCopy(zAppname, FileName);
GetDir(0,WorkDir);
StrPCopy(zCurdir,Workdir);
FillChar(StartupInfo, Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(
zAppname,
nil,
nil,
nil,
false,
CREATE_NEW_CONSOLE or
HIGH_PRIORITY_CLASS ,
nil,
zProjDir,
StartupInfo,
ProcessInfo) then Result := 0
else
begin
repeat
sleep(50);
Application.ProcessMessages;
GetExitCodeProcess(ProcessInfo.hProcess,Result);
until (Result <> STILL_ACTIVE) or Application.Terminated;
end;
end;
"Michael Riester" <webm...@michaelriester.de> wrote in message
news:3987335a_1@dnews...