AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Server Ping per cmd

Ein Thema von Tobiax · begonnen am 16. Apr 2008 · letzter Beitrag vom 16. Apr 2008
Antwort Antwort
Benutzerbild von Tobiax
Tobiax

Registriert seit: 13. Jul 2006
83 Beiträge
 
Delphi 2005 Architect
 
#1

Server Ping per cmd

  Alt 16. Apr 2008, 15:45
Moin, ich möchte mir gerne ein ping script schreiben ich habe das jetzt so gemacht mit einen cmd befehl ind delphi ausgeführt ping localhost dann kommt das raus

Ping wird ausgeführt für Zer0q12-PC [::1] von ::1 mit 32 Bytes Daten:
Antwort von ::1: Zeit<1ms
Antwort von ::1: Zeit<1ms
Antwort von ::1: Zeit<1ms
Antwort von ::1: Zeit<1ms

Diese antwort möchte ich mit delphi abfangen und zb mit if abfragen ob es eine antwort gibt oder nicht.
Wie kann ich das realisieren?


mfg
Anfänger ONAir
  Mit Zitat antworten Zitat
mashutu

Registriert seit: 15. Nov 2007
195 Beiträge
 
#2

Re: Server Ping per cmd

  Alt 16. Apr 2008, 16:03
Delphi-Quellcode:
function RunModulePiped(const sCommandLine : string; slStrings : TStrings):boolean;
type
    TCharBuffer = array[0..MaxInt - 1] of Char;
const
    MaxBufSize = 1024;
var
    I : Longword;
    SI : TStartupInfo;
    PI : TProcessInformation;
    _SA : PSecurityAttributes;
    _pSD : PSECURITY_DESCRIPTOR;
    NewStdIn : THandle;
    NewStdOut : THandle;
    ReadStdOut : THandle;
    WriteStdIn : THandle;
    Buffer : ^TCharBuffer;
    BufferSize : Cardinal;
    Last : WideString;
    Str : WideString;
    ExitCode : DWORD;
    Bread : DWORD;
    Avail : DWORD;
begin
    result:=true;
    GetMem(_SA, SizeOf(TSecurityAttributes));
        case Win32Platform of
            VER_PLATFORM_WIN32_NT :
                begin
                    GetMem(_pSD, SizeOf(SECURITY_DESCRIPTOR));
                    SysUtils.Win32Check(InitializeSecurityDescriptor(_pSD, SECURITY_DESCRIPTOR_REVISION));
                    SysUtils.Win32Check(SetSecurityDescriptorDacl(_pSD, True, nil, False));
                    _SA.lpSecurityDescriptor := _pSD;
                end; {end VER_PLATFORM_WIN32_NT}
        else //case
            _SA.lpSecurityDescriptor := nil;
        end; {end case}
        _SA.nLength := SizeOf(SECURITY_ATTRIBUTES);
        _SA.bInheritHandle := True;

    SysUtils.Win32Check(CreatePipe(NewStdIn, WriteStdIn, _SA, 0));
    if not CreatePipe(ReadStdOut, NewStdOut, _SA, 0) then
    begin
        CloseHandle(NewStdIn);
        CloseHandle(WriteStdIn);
        RaiseLastWin32Error;
    end; {end if}
    GetStartupInfo(SI);
    SI.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
    SI.wShowWindow := {SW_SHOWNORMAL} SW_HIDE;
    SI.hStdOutput := NewStdOut;
    SI.hStdError := NewStdOut;
    SI.hStdInput := NewStdIn;
    if not CreateProcess(nil, PChar(sCommandLine), nil, nil, True,
                            CREATE_NEW_CONSOLE, nil, nil, SI, PI) then
    
    begin
        CloseHandle(NewStdIn);
        CloseHandle(NewStdOut);
        CloseHandle(ReadStdOut);
        CloseHandle(WriteStdIn);
        result:=false;
        //SysUtils.RaiseLastWin32Error;
        RaiseLastWin32Error;

    end; {end if}
    Last := '';
    BufferSize := MaxBufSize;
    Buffer := AllocMem(BufferSize);
    try
        repeat
            SysUtils.Win32Check(GetExitCodeProcess(PI.hProcess, ExitCode));
            PeekNamedPipe(ReadStdOut, Buffer, BufferSize, @Bread, @Avail, nil);
            if (Bread <> 0) then
            begin
                if (BufferSize < Avail) then
                begin
                    BufferSize := Avail;
                    ReallocMem(Buffer, BufferSize);
                end; {end if}
                FillChar(Buffer^, BufferSize, #0);
                ReadFile(ReadStdOut, Buffer^, BufferSize, Bread, nil);

                Str := Last;
                I := 0;
                while (I < Bread) do
                begin
                    case Buffer^[I] of
                        #0 : inc(I);
                        #10 :
                            begin
                                inc(I);
                                slStrings.Add(Str);
                                Str := '';
                            end; {end #10}
                        #13 :
                            begin
                                inc(I);
                                if (I < Bread) and (Buffer^[I] = #10) then
                                    inc(I);
                                slStrings.Add(Str);
                                Str := '';
                            end; {end #13}
                    else//case
                        begin
                            Str := Str + Buffer^[I];
                            inc(I);
                        end; {end else}
                    end; {end case}
                end; {end while}
                Last := Str;
            end; {end if}
            Sleep(1);
            Application.ProcessMessages;
        until (ExitCode <> STILL_ACTIVE);
        if Last <> 'then
            slStrings.Add(Last);
    finally
        FreeMem(Buffer);
    end; {end try/finally}
    CloseHandle(PI.hThread);
    CloseHandle(PI.hProcess);
    CloseHandle(NewStdIn);
    CloseHandle(NewStdOut);
    CloseHandle(ReadStdOut);
    CloseHandle(WriteStdIn);
    if _SA.lpSecurityDescriptor <> nil then
            freeMem(_pSD, SizeOf(SECURITY_DESCRIPTOR));
    
    freeMem(_SA, SizeOf(TSecurityAttributes)); //***hy freigeben des allozierten Speichers
end; {end procedure}
utu

if it was hard to write it should be hard to read
  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 09:05 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