AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Netzwerke Delphi [HILFE] GetConsoleOutput variable IP
Thema durchsuchen
Ansicht
Themen-Optionen

[HILFE] GetConsoleOutput variable IP

Ein Thema von JackTheRipper · begonnen am 2. Nov 2006 · letzter Beitrag vom 6. Nov 2006
Antwort Antwort
JackTheRipper

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

[HILFE] GetConsoleOutput variable IP

  Alt 2. Nov 2006, 20: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
Benutzerbild von TheMiller
TheMiller

Registriert seit: 19. Mai 2003
Ort: Gründau
2.480 Beiträge
 
Delphi XE7 Architect
 
#2

Re: [HILFE] GetConsoleOutput variable IP

  Alt 2. Nov 2006, 20:15
Wenn Du den Quelltext per Copy & Paste übernommen hast, dann hast du das "+" Symbol zwischen Ping und der IP NACH dem String vergessen...
  Mit Zitat antworten Zitat
JackTheRipper

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

Re: [HILFE] GetConsoleOutput variable IP

  Alt 2. Nov 2006, 20:23
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
  Mit Zitat antworten Zitat
Benutzerbild von TheMiller
TheMiller

Registriert seit: 19. Mai 2003
Ort: Gründau
2.480 Beiträge
 
Delphi XE7 Architect
 
#4

Re: [HILFE] GetConsoleOutput variable IP

  Alt 2. Nov 2006, 20:25
Kein Problem,

aber dafür, dass du "neu in Delphi" bist, sieht dein Code ziemlich professionell aus....
  Mit Zitat antworten Zitat
Benutzerbild von Zacherl
Zacherl

Registriert seit: 3. Sep 2004
4.629 Beiträge
 
Delphi 10.2 Tokyo Starter
 
#5

Re: [HILFE] GetConsoleOutput variable IP

  Alt 2. Nov 2006, 20:54
Ich denke mal die GetConsoleOutput Funktion kam aus der CodeLib
Projekte:
- GitHub (Profil, zyantific)
- zYan Disassembler Engine ( Zydis Online, Zydis GitHub)
  Mit Zitat antworten Zitat
Benutzerbild von TheMiller
TheMiller

Registriert seit: 19. Mai 2003
Ort: Gründau
2.480 Beiträge
 
Delphi XE7 Architect
 
#6

Re: [HILFE] GetConsoleOutput variable IP

  Alt 2. Nov 2006, 20:55
Scharf kombiniert *fg*
  Mit Zitat antworten Zitat
Benutzerbild von Zacherl
Zacherl

Registriert seit: 3. Sep 2004
4.629 Beiträge
 
Delphi 10.2 Tokyo Starter
 
#7

Re: [HILFE] GetConsoleOutput variable IP

  Alt 2. Nov 2006, 22:04
[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]
Projekte:
- GitHub (Profil, zyantific)
- zYan Disassembler Engine ( Zydis Online, Zydis GitHub)
  Mit Zitat antworten Zitat
JackTheRipper

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

Re: [HILFE] GetConsoleOutput variable IP

  Alt 6. Nov 2006, 14:29
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
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:02 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz