![]() |
[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:
Manuell eingebe macht er doch ich will das er es in etwa so macht.if GetConsoleOutput('cmd /c ping *HIER DIE IP*', output, errors) then
Delphi-Quellcode:
Der gesammte Quell Code :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;
Delphi-Quellcode:
Und die GUIunit 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. Zitat:
|
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...
|
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:
|
Re: [HILFE] GetConsoleOutput variable IP
Kein Problem,
aber dafür, dass du "neu in Delphi" bist, sieht dein Code ziemlich professionell aus.... |
Re: [HILFE] GetConsoleOutput variable IP
Ich denke mal die GetConsoleOutput Funktion kam aus der CodeLib :mrgreen:
|
Re: [HILFE] GetConsoleOutput variable IP
Scharf kombiniert *fg* :???:
|
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] |
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