Einzelnen Beitrag anzeigen

JackTheRipper

Registriert seit: 18. Sep 2006
10 Beiträge
 
#1

[HILFE] GetConsoleOutput variable IP

  Alt 2. Nov 2006, 21:11
Hi,

Mein Problem ist das ich eine Variable IP pingen will mit CMD und es im Memo ausgibt. *Nicht so der begabteste mit Fachwörtern*
Eine IP die ich dann in der Zeile.
Delphi-Quellcode:
 
if GetConsoleOutput('cmd /c ping *HIER DIE IP*', output, errors) then
Manuell eingebe macht er doch ich will das er es in etwa so macht.
Delphi-Quellcode:

ip:=Edit1.Text;
  output:=TStringList.Create;
 try
    errors:=TStringList.Create;
    if GetConsoleOutput('cmd /c ping 'ip, output, errors) then
      Memo1.Lines.AddStrings(output);
  finally
    output.free;
    errors.free;
  end;
Der gesammte Quell Code :

Delphi-Quellcode:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Edit2: TEdit;
    Panel1: TPanel;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

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;



procedure TForm1.Button1Click(Sender: TObject);
var output, errors: TStringList;
var ip : string;
begin
showmessage('Ein Moment');
ip:=Edit1.Text;
  output:=TStringList.Create;
 try
    errors:=TStringList.Create;
    if GetConsoleOutput('cmd /c ping ', output, errors) then
      Memo1.Lines.AddStrings(output);
  finally
    output.free;
    errors.free;
  end;



end;
end.
Und die GUI

Zitat:

object Form1: TForm1
Left = 192
Top = 114
Width = 624
Height = 750
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 184
Top = 120
Width = 27
Height = 20
Caption = 'Ip.:'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -16
Font.Name = 'MS Sans Serif'
Font.Style = [fsBold]
ParentFont = False
end
object Label2: TLabel
Left = 8
Top = 160
Width = 214
Height = 20
Caption = 'Wiederholungsrate in Min.:'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -16
Font.Name = 'MS Sans Serif'
Font.Style = [fsBold]
ParentFont = False
end
object Button1: TButton
Left = 416
Top = 120
Width = 75
Height = 25
Caption = 'Manuell'
TabOrder = 0
OnClick = Button1Click
end
object Memo1: TMemo
Left = 16
Top = 240
Width = 561
Height = 353
Lines.Strings = (
'Memo1')
TabOrder = 1
end
object Button2: TButton
Left = 48
Top = 616
Width = 75
Height = 25
Caption = 'Save'
TabOrder = 2
end
object Button3: TButton
Left = 200
Top = 616
Width = 75
Height = 25
Caption = 'Ende'
TabOrder = 3
end
object Button4: TButton
Left = 360
Top = 616
Width = 75
Height = 25
Caption = 'Autostart'
TabOrder = 4
end
object Edit1: TEdit
Left = 256
Top = 120
Width = 121
Height = 21
TabOrder = 5
end
object Edit2: TEdit
Left = 256
Top = 160
Width = 121
Height = 21
TabOrder = 6
end
object Panel1: TPanel
Left = 32
Top = 8
Width = 561
Height = 73
Caption = 'SuperPing'
Font.Charset = ANSI_CHARSET
Font.Color = clWindowText
Font.Height = -35
Font.Name = 'Morpheus'
Font.Style = []
ParentFont = False
TabOrder = 7
end
end
  Mit Zitat antworten Zitat