Entwickler-Ecke

Dateizugriff - DOS Output in Textdatei schreiben


Yacoon - Mi 26.02.03 14:37
Titel: DOS Output in Textdatei schreiben
Ich habe eine Win32 Applikation, diese ruft eine DOS Anwendung mit Parametern auf.
Wie kann ich den Output, also das was normal in der DOS Konsole stehen würde in eine Textdatei schreiben?
Ich habe schon gesehen, das soetwas geht, habe aber keine Ahnung wie.

Vielleicht kann mir jemand helfen. Danke


Delete - Mi 26.02.03 14:49

Hier mal eine Möglichkeit über den Umweg einer Datei:

Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
    function RunCaptured(const _dirName, _exeName, _cmdLine: string): String;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  s: String;

implementation

{$R *.dfm}

procedure Ascii2Ansi(var s: string);
var
  s0: string;
begin
  Setlength(s0,length(s));
  if (length(s0)>0) then
    OemToChar(Pchar(s),PChar(s0));
  s:=s0;
end;

function TForm1.RunCaptured(const _dirName, _exeName, _cmdLine: string): String;
var 
  start: TStartupInfo; 
  procInfo: TProcessInformation; 
  tmpName: string;
  tmp: Windows.THandle;
  tmpSec: TSecurityAttributes; 
  res: TStringList; 
  return: Cardinal; 
begin
  try
    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;
      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);
        CloseHandle(procInfo.hThread);
        CloseHandle(procInfo.hProcess); 
        Windows.CloseHandle(tmp);
        res := TStringList.Create;
        try
          res.LoadFromFile(tmpName);
          result := res.Text;
        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; 

procedure TForm1.FormCreate(Sender: TObject);
var
  sl: TStringlist;
  i: Integer;
begin
  s := Runcaptured('C:\', 'ipconfig.exe', '/all');
  s := Trim(s);
  sl := TStringList.Create;
  try
  begin
    sl.Text := s;
    { Leerzeilen entfernen }
    for i := sl.Count-1 downto 0 do
    begin
      if sl.Strings[i] = '' then
        sl.Delete(i);
    end;
    s := sl.Text;
  end;
  finally
    sl.Free;
  end;
  ASCII2Ansi(s);
  Memo1.Text := s;
end;

Eleganter ist es mit Pipes, aber da kenne ich mich nicht mit aus.


hschuh - Mi 26.02.03 15:37

über die DOS-Box geht dies auch:

Bsp:

c:\> help > xx.txt

dann steht alles was help auswirft in xx.txt

Weis nicht ob dir das Hilft!

Gruß
HS


Wolff68 - Sa 01.03.03 02:06

Ich hab bei Torry mal eine Kompo namens DosCommand gefunden (Unter Apps und Tasks)
Die macht genau das was Du willst (und auch noch Zeilenweise)
Ausserdem ist der SourceCode dabei.

Wenn Du aber nur am Ende das Ergebnis haben willst ist die Lösung von hschuh sicher am einfachsten.


KeinePanik - Sa 01.03.03 13:25

noch nen Hinweis zu HSCHUS's Beitrag (falls du es nicht weisst):

Wenn du so ne Art log-Datei haben willst musst du

c:\> help >> xx.txt

nehmen (also doppeltes ">>") ... dann wird die Ausgabe an die Datei angehängt ... bei einem einfachen ">" wird die Datei xx.txt überschrieben ...