New Tip - ...run a console application and get its output ?

  • From: "SwissDelphiCenter.ch" <webmaster@xxxxxxxxxxxxxxxxxxxx>
  • To: sdcnewtip@xxxxxxxxxxxxx
  • Date: Sat, 2 Feb 2002 13:50:23 +0100

* SwissDelphiCenter new Tip newsletter
* www.swissdelphicenter.ch
*
* To unsubscribe send a mail to sdcnewtip-request@xxxxxxxxxxxxx with the
* subject unsubscribe


Hallo,

Folgender neuer Tip ist neu auf SwissDelphiCenter verfügbar:

Besuchen Sie die Programmier Tips unter 
http://www.swissdelphicenter.ch/de/tipsindex.php


Bewerten Sie diesen Tip nach Schwierigkeitsgrad, Nützlichkeit und Gesamthaft auf
http://www.swissdelphicenter.ch/de/showcode.php?id=990



Autor:
Superhausi <superhausi@xxxxxxxxxxxxx>
http://www.superhausi.ch


-------------
...ein Konsolen Programm ausführen und die Ausgaben erfassen ?
-------------
Kategorie: System



{
Diese Funktion führt ein Programm (Konsole oder Batch-File) aus und fügt die 
Ausgaben zu Memo1 hinzu
}


{
This function runs a program (console or batch) and adds its output to Memo1
}

{....}
  private
    function RunCaptured(const _dirName, _exeName, _cmdLine: string): Boolean;
    
{....}

function TForm1.RunCaptured(const _dirName, _exeName, _cmdLine: string): 
Boolean;
var
  start: TStartupInfo;
  procInfo: TProcessInformation;
  tmpName: string;
  tmp: Windows.THandle;
  tmpSec: TSecurityAttributes;
  res: TStringList;
  return: Cardinal;
begin
  Result := False;
  try
    { Setze ein Temporäres File }
    { Set a temporary file }
    tmpName := 'Test.tmp';
    FillChar(tmpSec, SizeOf(tmpSec), #0);
    tmpSec.nLength := SizeOf(tmpSec);
    tmpSec.bInheritHandle := True;
    tmp := Windows.CreateFile(PChar(tmpName),
           Generic_Write, File_Share_Write,
           @tmpSec, Create_Always, File_Attribute_Normal, 0);
    try
      FillChar(start, SizeOf(start), #0);
      start.cb          := SizeOf(start);
      start.hStdOutput  := tmp;
      start.dwFlags     := StartF_UseStdHandles or StartF_UseShowWindow;
      start.wShowWindow := SW_Minimize;
      { Starte das Programm }
      { Start the program }
      if CreateProcess(nil, PChar(_exeName + ' ' + _cmdLine), nil, nil, True,
                       0, nil, PChar(_dirName), start, procInfo) then
      begin
        SetPriorityClass(procInfo.hProcess, Idle_Priority_Class);
        WaitForSingleObject(procInfo.hProcess, Infinite);
        GetExitCodeProcess(procInfo.hProcess, return);
        Result := (return = 0);
        CloseHandle(procInfo.hThread);
        CloseHandle(procInfo.hProcess);
        Windows.CloseHandle(tmp);
        { Die Ausgaben hinzufügen }
        { Add the output }
        res := TStringList.Create;
        try
          res.LoadFromFile(tmpName);
          Memo1.Lines.AddStrings(res);
        finally
          res.Free;
        end;
        Windows.DeleteFile(PChar(tmpName));
      end
      else
      begin
        Application.MessageBox(PChar(SysErrorMessage(GetLastError())),
          'RunCaptured Error', MB_OK);
      end;
    except
      Windows.CloseHandle(tmp);
      Windows.DeleteFile(PChar(tmpName));
      raise;
    end;
  finally
  end;
end;


// Beispiel:
// Example:

procedure TForm1.Button1Click(Sender: TObject);
begin
  RunCaptured('C:\', 'cmd.exe', '/c dir');
end;




Best Regards
SwissDelphiCenter Team
www.swissdelphicenter.ch
[automatisch generierte EMail]


----------------------------------------------------
ENGLISH NEWSLETTER
----------------------------------------------------

Hi,

This new tip is new available on SwissDelphiCenter.ch:

Visit the programming tips at http://www.swissdelphicenter.ch/en/tipsindex.php


Rate this tip after skill, useful and overall at
http://www.swissdelphicenter.ch/en/showcode.php?id=990



Author:
Superhausi <superhausi@xxxxxxxxxxxxx>
http://www.superhausi.ch


-------------
...run a console application and get its output ?
-------------
Category: System



{
Diese Funktion führt ein Programm (Konsole oder Batch-File) aus und fügt die 
Ausgaben zu Memo1 hinzu
}


{
This function runs a program (console or batch) and adds its output to Memo1
}

{....}
  private
    function RunCaptured(const _dirName, _exeName, _cmdLine: string): Boolean;
    
{....}

function TForm1.RunCaptured(const _dirName, _exeName, _cmdLine: string): 
Boolean;
var
  start: TStartupInfo;
  procInfo: TProcessInformation;
  tmpName: string;
  tmp: Windows.THandle;
  tmpSec: TSecurityAttributes;
  res: TStringList;
  return: Cardinal;
begin
  Result := False;
  try
    { Setze ein Temporäres File }
    { Set a temporary file }
    tmpName := 'Test.tmp';
    FillChar(tmpSec, SizeOf(tmpSec), #0);
    tmpSec.nLength := SizeOf(tmpSec);
    tmpSec.bInheritHandle := True;
    tmp := Windows.CreateFile(PChar(tmpName),
           Generic_Write, File_Share_Write,
           @tmpSec, Create_Always, File_Attribute_Normal, 0);
    try
      FillChar(start, SizeOf(start), #0);
      start.cb          := SizeOf(start);
      start.hStdOutput  := tmp;
      start.dwFlags     := StartF_UseStdHandles or StartF_UseShowWindow;
      start.wShowWindow := SW_Minimize;
      { Starte das Programm }
      { Start the program }
      if CreateProcess(nil, PChar(_exeName + ' ' + _cmdLine), nil, nil, True,
                       0, nil, PChar(_dirName), start, procInfo) then
      begin
        SetPriorityClass(procInfo.hProcess, Idle_Priority_Class);
        WaitForSingleObject(procInfo.hProcess, Infinite);
        GetExitCodeProcess(procInfo.hProcess, return);
        Result := (return = 0);
        CloseHandle(procInfo.hThread);
        CloseHandle(procInfo.hProcess);
        Windows.CloseHandle(tmp);
        { Die Ausgaben hinzufügen }
        { Add the output }
        res := TStringList.Create;
        try
          res.LoadFromFile(tmpName);
          Memo1.Lines.AddStrings(res);
        finally
          res.Free;
        end;
        Windows.DeleteFile(PChar(tmpName));
      end
      else
      begin
        Application.MessageBox(PChar(SysErrorMessage(GetLastError())),
          'RunCaptured Error', MB_OK);
      end;
    except
      Windows.CloseHandle(tmp);
      Windows.DeleteFile(PChar(tmpName));
      raise;
    end;
  finally
  end;
end;


// Beispiel:
// Example:

procedure TForm1.Button1Click(Sender: TObject);
begin
  RunCaptured('C:\', 'cmd.exe', '/c dir');
end;



Best Regards
SwissDelphiCenter Team
www.swissdelphicenter.ch
[automatic generated EMail]

Other related posts:

  • » New Tip - ...run a console application and get its output ?