0

These two days I start to write some codes for ping some other devices using Delphi XE5. To my pleasure, I find there's a component named IcmpClient can be used for ping. Here's the code, mostly from some web resource.

unit main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Samples.Spin,
  Vcl.ExtCtrls, IdBaseComponent, IdComponent, IdRawBase, IdRawClient,
  IdIcmpClient;

type
  TfrmPing = class(TForm)
    edtHost: TEdit;
    btnPing: TButton;
    ICMP: TIdIcmpClient;
    Label1: TLabel;
    Panel1: TPanel;
    spnPing: TSpinEdit;
    lstReplies: TListBox;
    procedure btnPingClick(Sender: TObject);
    procedure ICMPReply(ASender: TComponent; const ReplyStatus: TReplyStatus);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPing: TfrmPing;

implementation

{$R *.dfm}

procedure TfrmPing.btnPingClick(Sender: TObject);
var
  i: integer;
begin
  ICMP.OnReply := ICMPReply;
  ICMP.ReceiveTimeout := 1000;
  btnPing.Enabled := False; try
    ICMP.Host := edtHost.Text;
    for i := 1 to spnPing.Value do begin
      ICMP.Ping;
      Application.ProcessMessages;
    end;
  finally btnPing.Enabled := True; end;
end;


procedure TfrmPing.ICMPReply(ASender: TComponent; const ReplyStatus: TReplyStatus);
var
  sTime: string;
begin
  // TODO: check for error on ping reply (ReplyStatus.MsgType?)
  if (ReplyStatus.MsRoundTripTime = 0) then
    sTime := '<1'
  else
    sTime := '=';

  lstReplies.Items.Add(Format('%d bytes from %s: icmp_seq=%d ttl=%d time%s%d ms',
    [ReplyStatus.BytesReceived,
    ReplyStatus.FromIpAddress,
    ReplyStatus.SequenceId,
    ReplyStatus.TimeToLive,
    sTime,
    ReplyStatus.MsRoundTripTime]));
end;

end.

After solving the problem of Error #10013 (by giving the administrator privilege ), I met with the second error #10040.

According to one post here, some people said that happens after upgrading from xe3 to xe4, and Remy Lebeau said it was on fixing. But after a few months, we are still meeting with the errors with XE5.

Should I just abandon Indy 10 and look for some other way to PING, or waiting for the fixes?

Puzzlist
  • 1
  • 1
  • 3
  • OT: I'm using code [`based on this`](https://code.google.com/p/projects-stackoverflow-tlama/source/browse/trunk/17084031/PingUnit.pas) unit because it doesn't require elevation. Just if you'd decide it to use, be sure to change the type `POINTER_32` defined there to `Pointer`. I've [`discussed it here`](http://stackoverflow.com/q/17084031/960757), but although the accepted answer is correct, the code fails when you don't do so. – TLama Jan 31 '14 at 11:33
  • `TIdIcmpClient` fixes are still on my TODO list. I have not had any free time lately to get to them yet. I know I am falling behind. I will try to get to them soon. – Remy Lebeau Jan 31 '14 at 16:32
  • Thanks for your hard work, Remy. Although Delphi is not as popular as before, Indy and it are still good choices for programmers like me. – Puzzlist Feb 01 '14 at 05:53

0 Answers0