Autor Beitrag
kuhlthomas
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 35



BeitragVerfasst: Sa 15.03.03 14:24 
ich habe ein programm geschrieben mit dem man net send nachrichten verschicken kann, nun will ich, dass das was durch 'net view' in der dos eingabeaufforderung ausgegeben wird in meinem delphi programm z.B. in einer ListBox ausgegeben wird ich hoffe mir ist zu helfen...
Andreas Pfau
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 997



BeitragVerfasst: Sa 15.03.03 15:00 
Also, ich habe einen Code. Den habe ich mal irgendwo runtergeladen und die Syntaqx ein bisschen an meinen Stil angepasst. Ich hoffe, ich verstoße gegen kein Copyright, wenn ich das hier post, denn ich babe die URL vergessen:
ausblenden volle Höhe 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;


Parameter: Der Konsolenbefehl (Muss den Interpreter enthalten, also nicht "dir" sondern "cmd.exe /c dir" oder "command.com dir")
Dann eine Stringliste mit dem Output und eine für Fehlermeldungen.

_________________
Life is a bad adventure, but the graphic is really good!
kuhlthomas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 35



BeitragVerfasst: Sa 15.03.03 15:53 
oh backe ich bin fast umgefallen als ich das gelesen habe, ich habe gedacht das wäre in drei zeilen zu erledigen. aber ich habe trotzdem noch ein problem, ich weiss nämlich nicht wie man den rückgabewert dieser function zweckmäßig verarbeiten soll
Andreas Pfau
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 997



BeitragVerfasst: Sa 15.03.03 16:02 
Tja, wie du villeicht schon selber festgestellt hast, man muss nur den Code reinkopieren, die Bedung ist extrem leicht.

Wei man die Rückgabe verarbeitet, ist dein Problem :wink: . Denn jedes Programm gibt andere Werte zurück. Du musst eine Routine schreiben, dir für ein bestimmtes Programm die Strings analysiert und auswertet. Anders geht's nicht. So was wurde erst mit Windows eingeführt, also z.B. Mechanismen wie DDE oder das versenden von Messages. DOS war für SingleTsking ausgelegt, also war es unwichtig, sich darum Gedanken zu machen, dass die Daten in einem Software-Auswertbarem Format eusgegeben wurden. Es gab ja keine anderen Prozesse. Es sei denn, dein Programm erstellt eine Log-File. Die könntest du evtl. einfahcer auswerten.

Bitte korrigiert mich, wenn das so nicht stimmt!

_________________
Life is a bad adventure, but the graphic is really good!
naleh
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23



BeitragVerfasst: Sa 15.03.03 16:27 
hi,
such doch mal nach "runcaptured" im forum, da wirste auch noch paar sachen finden!
MfG naleh
kuhlthomas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 35



BeitragVerfasst: Sa 15.03.03 18:12 
jetzt ist mir auf jeden fall einiges klarer, nur leider bekomme ich beim kompilieren immer eine fehlermeldung die sich auf den command parameter der function bezieht und das sogar wenn ich es so mache im im beispiel empfolen

gruß thomas
kuhlthomas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 35



BeitragVerfasst: Sa 15.03.03 18:16 
ja 'runcaptured' hats gebracht ich bin da auf ne echt gute function von Luckie gestoßen...
naleh
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 23



BeitragVerfasst: Sa 15.03.03 19:41 
manchmal fehlen einem einfach nur die richtigen worte :D