Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problems with CreateProcess() and WaitForSingleObject() under D5!

902 views
Skip to first unread message

Michael Riester

unread,
Aug 1, 2000, 3:00:00 AM8/1/00
to
Hello!

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

Michael Riester

unread,
Aug 1, 2000, 3:00:00 AM8/1/00
to

Maynard Philbrook

unread,
Aug 2, 2000, 3:00:00 AM8/2/00
to Michael Riester
I have worked with the API call.
Take my word for it., it does not do what the API help claim it does.
95,98, and 98se all have a different effect.
in 95 it seems to work more like it should, in 98 it does not work at all in
some cases..
it also depends of the app you are waiting for. not all apps process the message
system the same way.
so if your waiting for an app to terminate you would be better off just poling
for it's existance every
1 second or so.,
that is what i did to solve the problem of obtaining a window handle of the
newly started app.
use the create process first.
then i poled the windowList matching the threadid's every .5 seconds until i
found the App that had the
main window and matching threadid..
this at this point i had the Window Handle..
then in your case if your waiting for it's termination you could then scan the
list until it's gone.

Marius Bunescu

unread,
Aug 2, 2000, 3:00:00 AM8/2/00
to
I used this and it works

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;

Steve Guerin

unread,
Aug 18, 2000, 3:00:00 AM8/18/00
to
Here's a function that launch and wait for the end of execution of your
process and also process any incomming event

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...

0 new messages