Autor Beitrag
Marekventur
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 72



BeitragVerfasst: Sa 16.04.05 19:25 
Hallo!!!

Ich möchte ein wenig mit dem Free Pascal Compiler arbeiten, da ich dasd leicht auch Linu portieren kann.... :roll:
Da das kompilieren in einer Konsolenanwendung stattfindet, sieht man nicht viel. Das Fenster geht kurz auf und direkt wieder zu. So kann ich auch die Fehlermeldungen nicht sehen. Ich habe es nicht geschafft, das Fenster solange offenzubehalten, das ich die Fehler lesen kann.....

Nun meine Frage: Kann ich die Ausgabe einer Konsolenanwendung mit einem Programm (welche natürlich erst diese Anwendung aufruft) abfangen. Also sozusagen ein ShellExecute mit String-Rückgabe? Dann würde ich nähmlich mein Delphi-Programm aufrufen, welches den Compiler aufruft und dann die Meldungen (und Fehler) zurückgibt.

Vielleicht hat jemand ja auch eine ganz andere Idee....

Also, vielen Dank

Marek Ventur
demo88
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 160

Ubuntu 6.04, Win XP
Delphi 7
BeitragVerfasst: Sa 16.04.05 20:51 
Command ist der Befehl
z.B.: 'dir c:\'
Output ist eine Stringlist in dem halt die Ausgabe erfolgt, falls ein Error auftrit wird er in Errors gespeichert

ausblenden volle Höhe Delphi-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:
function GetConsoleOutput(const Command: String; var Output, Errors: TStringList): Boolean;
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  SecurityAttr: TSecurityAttributes;
  PipeOutputRead: THandle;
  PipeOutputWrite: THandle;
  PipeErrorsRead: THandle;
  PipeErrorsWrite: THandle;
  Succeed: Boolean;
  Buffer: array [0..255] of Char;
  NumberOfBytesRead: DWORD;
  Stream: TMemoryStream;
begin
  //Initialisierung ProcessInfo
  FillChar(ProcessInfo, SizeOf(TProcessInformation), 0);

  //Initialisierung SecurityAttr
  FillChar(SecurityAttr, SizeOf(TSecurityAttributes), 0);
  SecurityAttr.nLength := SizeOf(SecurityAttr);
  SecurityAttr.bInheritHandle := true;
  SecurityAttr.lpSecurityDescriptor := nil;

  //Pipes erzeugen
  CreatePipe(PipeOutputRead, PipeOutputWrite, @SecurityAttr, 0);
  CreatePipe(PipeErrorsRead, PipeErrorsWrite, @SecurityAttr, 0);

  //Initialisierung StartupInfo
  FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  StartupInfo.cb:=SizeOf(StartupInfo);
  StartupInfo.hStdInput := 0;
  StartupInfo.hStdOutput := PipeOutputWrite;
  StartupInfo.hStdError := PipeErrorsWrite;
  StartupInfo.wShowWindow := sw_Hide;
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;

  if  CreateProcess(nil, PChar(command), nil, nil, true,
  CREATE_DEFAULT_ERROR_MODE or CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil,
  StartupInfo, ProcessInfo) then begin
    result:=true;
    //Write-Pipes schließen
    CloseHandle(PipeOutputWrite);
    CloseHandle(PipeErrorsWrite);

    //Ausgabe Read-Pipe auslesen
    Stream := TMemoryStream.Create;
    try
      while true do begin
        succeed := ReadFile(PipeOutputRead, Buffer, 255, NumberOfBytesRead, nil);
        if not succeed then break;
        Stream.Write(Buffer, NumberOfBytesRead);
      end;
      Stream.Position := 0;
      Output.LoadFromStream(Stream);
    finally
      Stream.Free;
    end;
    CloseHandle(PipeOutputRead);

    //Fehler Read-Pipe auslesen
    Stream := TMemoryStream.Create;
    try
      while true do begin
        succeed := ReadFile(PipeErrorsRead, Buffer, 255, NumberOfBytesRead, nil);
        if not succeed then break;
        Stream.Write(Buffer, NumberOfBytesRead);
      end;
      Stream.Position := 0;
      Errors.LoadFromStream(Stream);
    finally
      Stream.Free;
    end;
    CloseHandle(PipeErrorsRead);

    WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
    CloseHandle(ProcessInfo.hProcess);
  end
  else begin
    result:=false;
    CloseHandle(PipeOutputRead);
    CloseHandle(PipeOutputWrite);
    CloseHandle(PipeErrorsRead);
    CloseHandle(PipeErrorsWrite);
  end;
end;

_________________
"Das ist kein Bug, das ist ein Feature..."
prote
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 17



BeitragVerfasst: Sa 16.04.05 21:06 
Verwendest Du eine IDE, die für Dich auch kompiliert? Wenn ja, dann dürfte ein Aufruf des Compilers von der Konsole aus weiterhelfen, da der mit Sicherheit dort auch seine Meldungen hinterläßt ...
Marekventur Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 72



BeitragVerfasst: So 17.04.05 18:52 
Danke, das funktioniert perfekt!

Nein, ich benutze nicht die IDE, ein Editor mit Synatax-Higlighting find ich besser.....