🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.delphi.nativeapi
Article #172

Re: Problem with CreateProcess

#172
From: Bryan Ashby
Date: Mon, 26 Jan 2004 22:41
177 lines
6176 bytes
Or perhaps not, somehow I managed to post the wrong code!

If you can't find it on google, feel free to email me for a code piece so I
don't have to post a big chunk here again :)

Bryan

"Bryan Ashby" <bryanashby@hotNOSPAM.PLEASEmail.com> wrote in message
news:40159637@newsgroups.borland.com...
> Perhaps this will help:
>
> type
>   TCreateNewProcessPriorityClass        = (
>     pcInheritCurrent,
>     pcAboveNormal,
>     pcBelowNormal,
>     pcHigh,
>     pcIdle,
>     pcNormal,
>     pcRealTime
>   );
>
>   TCreateNewProcessWait = (
>     pwNoWait,
>     pwWaitUntilEnd,
>     pwWaitUntilInputIdle
>   );
>
> procedure __WaitForProcessTermination (Pid : LongWord);
> var
>   Msg   : TMsg;
>   r     : LongWord;
> begin
>   repeat
>     r := MsgWaitForMultipleObjects (1, Pid, FALSE, INFINITE, QS_PAINT or
>       QS_SENDMESSAGE);
>     if r = WAIT_FAILED then
>       Exit;
>     if r = (WAIT_OBJECT_0 + 1) then
>       while PeekMessage(Msg, 0, WM_PAINT, WM_PAINT, PM_REMOVE) do
>         DispatchMessage (Msg);
>   until (r = WAIT_OBJECT_0);
> end;
>
>
//--------------------------------------------------------------------------
> ----
>
> //
> // Code adapted from WinExecAndWaitV2 by Peter Below from Team B on Delphi
> ng's
> //
> function CreateNewProcess (const CommandLine : string;
>   const ShowWindow : Word; const Wait : TCreateNewProcessWait;
>   const Priority : TCreateNewProcessPriorityClass;
>   const WaitIdleTimeoutMs : LongWord; Pid, ThreadId : PLongWord) :
LongWord;
> var
>   StartInfo     : TStartupInfo;
>   ProcInfo      : TProcessInformation;
>   Cf            : LongWord;
> begin
>   Result := LongWord(-1);
>
>   FillChar (StartInfo, SizeOf(StartInfo), 0);
>   FillChar (ProcInfo, SizeOf(ProcInfo), 0);
>   StartInfo.cb := SizeOf(StartInfo);
>
>   if ShowWindow <> 0 then
>   begin
>     StartInfo.dwFlags := STARTF_USESHOWWINDOW;
>     StartInfo.wShowWindow := ShowWindow;
>   end;
>
>   case Priority of
>     pcInheritCurrent    : Cf := GetPriorityClass (GetCurrentProcess);
>     pcAboveNormal       : Cf := ABOVE_NORMAL_PRIORITY_CLASS;
>     pcBelowNormal       : Cf := BELOW_NORMAL_PRIORITY_CLASS;
>     pcHigh              : Cf := HIGH_PRIORITY_CLASS;
>     pcIdle              : Cf := IDLE_PRIORITY_CLASS;
>     pcNormal            : Cf := NORMAL_PRIORITY_CLASS;
>     pcRealTime          : Cf := REALTIME_PRIORITY_CLASS;
>     else
>       Cf := 0;
>   end;
>
>   Cf := Cf or CREATE_NEW_CONSOLE;
>
>   if CreateProcess(nil, PChar(CommandLine), nil, nil, FALSE, Cf, nil, nil,
>     StartInfo, ProcInfo) then
>   begin
>     case Wait of
>       pwNoWait  :
>       begin
>         if Assigned(Pid) then
>           Pid^ := ProcInfo.hProcess;
>         if Assigned(ThreadId) then
>           ThreadId^ := ProcInfo.hThread;
>
>         Result := ProcInfo.hProcess;
>       end;
>
>       pwWaitUntilEnd    :
>       begin
>         __WaitForProcessTermination (ProcInfo.hProcess);
>         GetExitCodeProcess (ProcInfo.hProcess, Result);
>         CloseHandle (ProcInfo.hProcess);
>         CloseHandle (ProcInfo.hThread);
>       end;
>
>       pwWaitUntilInputIdle      :
>       begin
>         WaitForInputIdle (ProcInfo.hProcess, WaitIdleTimeoutMs);
>         if Assigned(Pid) then
>           Pid^ := ProcInfo.hProcess;
>         if Assigned(ThreadId) then
>           ThreadId^ := ProcInfo.hThread;
>
>         Result := ProcInfo.hProcess;
>       end;
>     end;        // case Wait
>   end;
> end;
>
> "Mike Fieger" <mike.fieger@aon.at> wrote in message
> news:401592fe@newsgroups.borland.com...
> > Hello!
> >
> > I need to start a DOS-program and redirect its output to a file from
> within
> > a Delphi application.
> > After some trying I created the following code.
> > Everything works fine... except for the fact that it stops working as
soon
> > as I try to actually redirect the output to a file...   :-(
> >
> > Can anybody help me with that?
> >
> > Mike
> >
> >
> >
> >
> > procedure TForm1.StartDosProg;
> > var
> >   StartupInfo: TStartupInfo;         // for CreateProcess
> >   ProcessInfo: TProcessInformation;  // for CreateProcess
> > begin
> >   // Initialize StartupInfo
> >   FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
> >   StartupInfo.cb := Sizeof(TStartupInfo);
> >   StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
> >   StartupInfo.wShowWindow := SW_SHOWNORMAL;
> >   // Start DOS-prog
> >   if CreateProcess(nil, PChar('fc'), nil, nil, false,
> NORMAL_PRIORITY_CLASS,
> > nil, nil, StartupInfo, ProcessInfo) then   // this works
> >   //if CreateProcess(nil, PChar('fc >fc.log'), nil, nil, false,
> > NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then  // this
> > works not :-(   why?
> >   begin
> >     // Program started: Wait till finished...
> >     WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
> >     CloseHandle(ProcessInfo.hProcess);
> >   end
> >   else
> >   begin
> >     // Error starting program
> >     //Application.MessageBox(PChar(sGetErrorString(GetLastError)),
> 'Error',
> > MB_OK);
> >   end;
> > end;
> >
> >
>
>
Technical Details
Message-ID: <40159784$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.frii.net!newsfeed.frii.net!news.compaq.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <401592fe@newsgroups.borland.com> <40159637@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f94dd,7d1432a8153d8a6
X-Google-Attributes: gidf94dd,public
X-Google-ArrivalTime: 2004-01-26 14:53:31 PST
From: "Bryan Ashby" <bryanashby@hotNOSPAM.PLEASEmail.com>
Newsgroups: borland.public.delphi.nativeapi
References: <401592fe@newsgroups.borland.com> <40159637@newsgroups.borland.com>
Subject: Re: Problem with CreateProcess
Date: Mon, 26 Jan 2004 15:41:08 -0700
Lines: 179
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 64.90.206.254
Message-ID: <40159784$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1075156868 64.90.206.254 (26 Jan 2004 14:41:08 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.frii.net!newsfeed.frii.net!news.compaq.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail