Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The below code captures console outputs and sent in realime to the Memo parameter, so the end user may not think the application is hanging while program performing the command.

Code Block
languagedelphi
type
  TArg<T> = reference to procedure(const Arg: T);
.
.
.
procedure TMainFrm.CaptureConsoleOutput(const ACommand, AParameters: String; AMemoCallBack: TMemoTArg<PAnsiChar>);
 const
   CReadBuffer = 2400;
 var
   saSecurity: TSecurityAttributes;
   hRead: THandle;
   hWrite: THandle;
   suiStartup: TStartupInfo;
   piProcess: TProcessInformation;
   pBuffer: array [0 .. CReadBuffer] of CharAnsiChar;
  dBuffer: array [0 .. CReadBuffer] of AnsiChar;
  dRead: DWordDWORD;
   dRunning: DWordDWORD;
  dAvailable: DWORD;
begin
   saSecurity.nLength := SizeOf(TSecurityAttributes);
   saSecurity.bInheritHandle := Truetrue;  
   saSecurity.lpSecurityDescriptor := nil; 
 
   if CreatePipe(hRead, hWrite, @saSecurity, 0) then
   begin   try
 
     FillChar(suiStartup, SizeOf(TStartupInfo), #0);
      suiStartup.cb := SizeOf(TStartupInfo);
      suiStartup.hStdInput := hRead;
      suiStartup.hStdOutput := hWrite;
      suiStartup.hStdError := hWrite;
      suiStartup.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;  
  
     suiStartup.wShowWindow := SW_HIDE; 
 
     if CreateProcess(nil, PChar(ACommand + ' ' + AParameters), @saSecurity,
       @saSecurity, Truetrue, NORMAL_PRIORITY_CLASS, nil, nil, suiStartup,
  piProcess)
      piProcess) then
     begin   try
          repeat
            dRunning  := WaitForSingleObject(piProcess.hProcess, 100);
            PeekNamedPipe(hRead, nil, 0, nil, @dAvailable, nil);
            if Application.ProcessMessages(); (dAvailable > 0) then
              repeat
                dRead := 0;
                ReadFile(hRead, pBuffer[0], CReadBuffer, dRead, nil); 
         
           pBuffer[dRead] := #0;
  
   
           OemToAnsiOemToCharA(pBuffer, pBufferdBuffer);
              AMemo.Lines.Add(String(pBuffer));
  CallBack(dBuffer);
              until (dRead < CReadBuffer);
            Application.ProcessMessages;
          until (dRunning <> WAIT_TIMEOUT);
        finally
          CloseHandle(piProcess.hProcess);
          CloseHandle(piProcess.hThread);
    
     end;
    finally
 
     CloseHandle(hRead);
      CloseHandle(hWrite);
    end;
end;