Hello Rob,

It's not clear that your application is a console app. But if it is
here is some code that I use to capture the output to a stringlist. I
hope that it's what you're looking for. It won't compile straight out
of the box but you get the idea.


function RunConsoleAppWaitAndCapture( const cConsoleApp, cParameters, 
cWorkingDir :string; aResults :TStringList ) :DWORD;
var
  SA :TSecurityAttributes;
  SI :TStartupInfo;
  PI :TProcessInformation;
  hStdOut, hAppProcess, hAppThread :THandle;
  cTemp, cTempFile :string;
  aBuffer :array[0..255] of Char;
const
  FUNC_NAME = 'RunConsoleAppWaitAndCapture';
begin
  // do the business
  result := 0;

  try


    hStdOut      := 0;
    hAppProcess  := 0;
    hAppThread   := 0;
    result       := 0;
    aResults.Clear;

    GetTempPath( 255, aBuffer );
    cTemp        := StrPas( aBuffer );
    cTempFile    := AddName2Path( cTemp, 'stdout.tmp' );
    if FileExists( cTempFile ) then
      SysUtils.DeleteFile( cTempFile );


    // Initialize output file security attributes
    FillChar( SA, SizeOf(SA), #0 );
    SA.nLength              := SizeOf(SA);
    SA.lpSecurityDescriptor := nil;
    SA.bInheritHandle       := true;


    // Create Output File
    hStdOut := CreateFile( PChar(cTempFile),
                           GENERIC_READ or GENERIC_WRITE,
                           FILE_SHARE_READ or FILE_SHARE_WRITE,
                           @SA,
                           CREATE_ALWAYS, // Always create it
                           FILE_ATTRIBUTE_TEMPORARY or // Will cache in memory 
if possible
                           FILE_FLAG_WRITE_THROUGH,
                           0 );


    // Check Output Handle
    if hStdOut = INVALID_HANDLE_VALUE then
      begin
        result := ERR_STDOUT_FILE;
        Raise Exception.CreateFmt( 'Function %s() failed!'#10#13'Command line = 
%s', [FUNC_NAME, cConsoleApp] );
      end;


    // Initialize Startup Info
    FillChar( SI, SizeOf(SI), #0 );
    with SI do
      begin
        cb          := SizeOf(SI);
        dwFlags     := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
        wShowWindow := SW_HIDE;
        hStdInput   := GetStdHandle(STD_INPUT_HANDLE);
        hStdError   := hStdOut;
        hStdOutput  := hStdOut;
      end;


    // Create the process
    cTemp := cConsoleApp + ' ' + cParameters;
    if CreateProcess( nil,
                      PChar( cTemp ),
                      nil,
                      nil,
                      true,
                      0,
                      nil,
                      PChar(cWorkingDir),
                      SI,
                      PI
                     ) then
      begin
        WaitforSingleObject( PI.hProcess, INFINITE );
        hAppProcess := PI.hProcess;
        hAppThread  := PI.hThread;
        GetExitCodeProcess( hAppProcess, result );
      end
    else
      begin
        result := ERR_CREATE_PROCESS;
        Raise Exception.CreateFmt( 'CreateProcess() in function %s() 
failed!'#10#13'Command line = %s', [FUNC_NAME, cConsoleApp] );
      end;

    CloseHandle( hStdOut );
    hStdOut := 0;

    if hAppProcess <> 0 then
      CloseHandle(hAppProcess);
    if hAppThread <> 0 then
      CloseHandle(hAppThread);
      
    if FileExists( cTempFile ) then
      begin
        try
          aResults.LoadFromFile( cTempFile );
        except
          on e:Exception do
            showmessage( e.Message );
        end;
        //showmessage( inttostr(aresults.count) );
        SysUtils.DeleteFile( cTempFile );
      end;

  finally

    if hAppProcess <> 0 then
      CloseHandle(hAppProcess);
    if hAppThread <> 0 then
      CloseHandle(hAppThread);

  end;
end;


-- 
Best regards,
 Stephen                            mailto:[EMAIL PROTECTED]


_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to