Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Delphi [HILFE] GetConsoleOutput variable IP (https://www.delphipraxis.net/80089-%5Bhilfe%5D-getconsoleoutput-variable-ip.html)

JackTheRipper 2. Nov 2006 20:11


[HILFE] GetConsoleOutput variable IP
 
Hi,

Mein Problem ist das ich eine Variable IP pingen will mit CMD und es im Memo ausgibt. :gruebel: :gruebel: *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

TheMiller 2. Nov 2006 20:15

Re: [HILFE] GetConsoleOutput variable IP
 
Wenn Du den Quelltext per Copy & Paste übernommen hast, dann hast du das "+" Symbol zwischen Ping und der IP NACH dem String vergessen...

JackTheRipper 2. Nov 2006 20:23

Re: [HILFE] GetConsoleOutput variable IP
 
Danke war schon die ganze zeit am grübeln. Bin neu in Delphi. Und wuste net das das eigentlich so simpel ist nur ein + ^^. THX :cyclops:

TheMiller 2. Nov 2006 20:25

Re: [HILFE] GetConsoleOutput variable IP
 
Kein Problem,

aber dafür, dass du "neu in Delphi" bist, sieht dein Code ziemlich professionell aus....

Zacherl 2. Nov 2006 20:54

Re: [HILFE] GetConsoleOutput variable IP
 
Ich denke mal die GetConsoleOutput Funktion kam aus der CodeLib :mrgreen:

TheMiller 2. Nov 2006 20:55

Re: [HILFE] GetConsoleOutput variable IP
 
Scharf kombiniert *fg* :???:

Zacherl 2. Nov 2006 22:04

Re: [HILFE] GetConsoleOutput variable IP
 
[OT]Naja und so richtig proffessionell eingerückt ist der Code in der OnClick Methode ja nicht ganz ;)
Aber das erwartet ja auch keiner von einem Anfänger. :) [/OT]

JackTheRipper 6. Nov 2006 14:29

Re: [HILFE] GetConsoleOutput variable IP
 
Solange es Funzt setz ich mich doch net mit ein Buch vor den Pc und grübel über sowas nach wozu gibt es Google oder besser euch Dephi-Praxis. Ihr seid die besten. Will auch ma so krass werden wie ihr ;) :dance:


Alle Zeitangaben in WEZ +1. Es ist jetzt 00:46 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz