Autor Beitrag
Minime
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Mo 04.09.06 16:17 
Hi ihr coolen Delphi Freaks ;-)!

ich versuche mich gerade an einem projeckt (kleines remote shell programm) und hab die socket arschitektur sogut wie fertig

nur noch eins fehlt : das herz stück -> die Cosole Capture Function ;-)

ich hab mich zwar belesen und so vieles an quellcode über pipes durchgelesen aber ich hab trotzdem null peilung


Die ganzen threads über dieses thema behandeln nur das fangen von "eigenen" programmen, ich aber will am besten einen direkten zugriff auf cmd.exe mit zb dir c:\ oder so


TDosCommand ist auch keine alternative, das das programm sehr größensensitiv ist (sprich keine forms,sysutils etc) und wiederum man batsch dateien etc angeben/erstellen muss

wer kann mir helen?
tgr
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 29

WIN 7, XP
D2010 Ent
BeitragVerfasst: Mo 04.09.06 22:42 
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:
86:
87:
88:
89:
function ExecConsole(const Command:string;Output,Errors:TStringList):Boolean;
//Consolenkommando (Command) ausführen und Ergebnis/Fehler zurückliefern
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
        begin
          Break;
        end;
        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
        begin
          Break;
        end;
        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; //ExecConsole


TGR
Minime Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Mo 04.09.06 23:00 
waaaaas

danke :-)
Minime Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Di 05.09.06 15:54 
Titel: Tribute
Hier nochmal das ganze ohne Classes ;-)
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:
function ExecConsole(const Command:String):string;
//Consolenkommando (Command) ausführen und Ergebnis/Fehler zurückliefern
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  SecurityAttr: TSecurityAttributes;
  PipeOutputRead: THandle;
  PipeOutputWrite: THandle;
  PipeErrorsRead: THandle;
  PipeErrorsWrite: THandle;
  Succeed: Boolean;
  Buffer: array [0..255] of Char;
  OutBuffer :string;
  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
    //Write-Pipes schließen
    CloseHandle(PipeOutputWrite);
    CloseHandle(PipeErrorsWrite);
    //Ausgabe Read-Pipe auslesen
    try
      while True do
      begin
        Succeed:=ReadFile(PipeOutputRead,Buffer,255,NumberOfBytesRead,nil);
        if not Succeed then
        begin
          Break;
        end;
       OutBuffer := OutBuffer + Buffer;
      end;
      Result := OutBuffer;
    finally
    Buffer := '';
    OutBuffer := '';
    end;
    CloseHandle(PipeOutputRead);
    CloseHandle(PipeErrorsRead);
    WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
    CloseHandle(ProcessInfo.hProcess);
  end
  else
  begin
    CloseHandle(PipeOutputRead);
    CloseHandle(PipeOutputWrite);
    CloseHandle(PipeErrorsRead);
    CloseHandle(PipeErrorsWrite);
  end;
end;
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 05.09.06 21:47 
Moin!

Ich habe dazu auch einen Vorschlag in die DL eingestellt, der nicht so speicherintensiv aus den Pipes liest.

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Minime Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 32



BeitragVerfasst: Mi 06.09.06 14:54 
jeah very nice ;-)

100x besser als meine version