The Road to Delphi

Delphi – Free Pascal – Oxygene

Making a PING with Delphi and the WMI


Typically we use the IcmpSendEcho function or a component like TIdIcmpClient to make a ping request from Delphi. Today I will show you another way to do this using the WMI (Windows Management Instrumentation).

The WMI class which allow you to make a ping request is Win32_PingStatus, to use this class you only need to pass the parameter Address value in your WQL sentence , the form of the Address parameter can be either the computer name (ACCOUNT-PC), IPv4 address (, or IPv6 address (2010:836B:4179::836B:4179).

SELECT * FROM Win32_PingStatus where Address=''

Some of the advantages of use this class to make a ping is which supports IPv4 addresses and IPv6 addresses (Starting with Windows Vista) , and you can set the ping parameters in a single WQL sentence.

For example if you want send a Buffer of 64 bytes (instead of the 32 default size) and resolve the address of the host server you only need to write a sentence like this :

SELECT * FROM Win32_PingStatus where Address='' AND BufferSize=64 AND ResolveAddressNames=TRUE

Now check this sample console application.

program WMIPing;



function GetStatusCodeStr(statusCode:integer) : string;
  case statusCode of
    0     : Result:='Success';
    11001 : Result:='Buffer Too Small';
    11002 : Result:='Destination Net Unreachable';
    11003 : Result:='Destination Host Unreachable';
    11004 : Result:='Destination Protocol Unreachable';
    11005 : Result:='Destination Port Unreachable';
    11006 : Result:='No Resources';
    11007 : Result:='Bad Option';
    11008 : Result:='Hardware Error';
    11009 : Result:='Packet Too Big';
    11010 : Result:='Request Timed Out';
    11011 : Result:='Bad Request';
    11012 : Result:='Bad Route';
    11013 : Result:='TimeToLive Expired Transit';
    11014 : Result:='TimeToLive Expired Reassembly';
    11015 : Result:='Parameter Problem';
    11016 : Result:='Source Quench';
    11017 : Result:='Option Too Big';
    11018 : Result:='Bad Destination';
    11032 : Result:='Negotiating IPSEC';
    11050 : Result:='General Failure'

//The form of the Address parameter can be either the computer name (wxyz1234), IPv4 address (, or IPv6 address (2010:836B:4179::836B:4179).
procedure  Ping(const Address:string;Retries,BufferSize:Word);
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
  i             : Integer;

  PacketsReceived : Integer;
  Minimum         : Integer;
  Maximum         : Integer;
  Average         : Integer;
  Minimum        :=0;
  Maximum        :=0;
  Average        :=0;
  Writeln(Format('Pinging %s with %d bytes of data:',[Address,BufferSize]));
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', '');
  //FWMIService   := FSWbemLocator.ConnectServer('', 'root\CIMV2', 'user', 'password');
  for i := 0 to Retries-1 do
    FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM Win32_PingStatus where Address=%s AND BufferSize=%d',[QuotedStr(Address),BufferSize]),'WQL',0);
    oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
    if oEnum.Next(1, FWbemObject, iValue) = 0 then
      if FWbemObject.StatusCode=0 then
        if FWbemObject.ResponseTime>0 then
          Writeln(Format('Reply from %s: bytes=%s time=%sms TTL=%s',[FWbemObject.ProtocolAddress,FWbemObject.ReplySize,FWbemObject.ResponseTime,FWbemObject.TimeToLive]))
          Writeln(Format('Reply from %s: bytes=%s time=<1ms TTL=%s',[FWbemObject.ProtocolAddress,FWbemObject.ReplySize,FWbemObject.TimeToLive]));


        if FWbemObject.ResponseTime>Maximum then

        if Minimum=0 then

        if FWbemObject.ResponseTime<Minimum then

      if not VarIsNull(FWbemObject.StatusCode) then
        Writeln(Format('Reply from %s: %s',[FWbemObject.ProtocolAddress,GetStatusCodeStr(FWbemObject.StatusCode)]))
        Writeln(Format('Reply from %s: %s',[Address,'Error processing request']));

  Writeln(Format('Ping statistics for %s:',[Address]));
  Writeln(Format('    Packets: Sent = %d, Received = %d, Lost = %d (%d%% loss),',[Retries,PacketsReceived,Retries-PacketsReceived,Round((Retries-PacketsReceived)*100/Retries)]));
  if PacketsReceived>0 then
   Writeln('Approximate round trip times in milli-seconds:');
   Writeln(Format('    Minimum = %dms, Maximum = %dms, Average = %dms',[Minimum,Maximum,Round(Average/PacketsReceived)]));

    on E:Exception do
        Writeln(E.Classname, ':', E.Message);

And the output

Author: Rodrigo

Just another Delphi guy.

34 thoughts on “Making a PING with Delphi and the WMI

  1. Pingback: Making a PING with Delphi and the WMI

  2. Nice post, but the code has broken lines.
    Please don’t use tags, use tags, like this :

     if FWbemObject.ResponseTime>Maximum then




  3. Hi Rodrigo,

    As usual exceptional examples with very useful information! On this example I could suggest you to change
    “FWMIService := FSWbemLocator.ConnectServer(‘localhost’, ‘root\CIMV2’, ”, ”);” with
    ” FWMIService := FSWbemLocator.ConnectServer(”, ”, ”, ”);”
    due to fact that SWbemLocator.ConnectServer method takes by default the system parameters, if you don’t want to put them explicit. And you have several comments there, even an IP adress :)

    Please keep up the good work, it’s always interesting to read your blog!

    Best regards,

    • Radu, thanks for your comments, about your suggestion is always recommendable pass the namespace and the machine name when you use the ConnectServer function. about the commented line is just for illustrate which you can make a ping from a remote machine using the WMI.

  4. Muy bueno el código !!!


  5. Hello

    There is a problem with your code. If you try to ping a invalid address like ‘aaaa’ you get this error message:

    Project WMIPing.exe raised exception class EVariantTypeCastError with message ‘Could not convert variant of type (Null) into type (Integer).

    To reproduce this error, simple change:




    in this case, FWbemObject.StatusCode is getting NULL instead of the error code.

    • Rafael the problem is due (as you say) which the StatusCode property return a null value which cannot be translated to a valid status message, the code was modified to handle this issue. try now and let me know the results of your tests.

      • OK .. the problem is gone now (i had already did what you just did). But it seems to me that the Status code is always null when an address is invalid. This way, you will never get the error status when you use GetStatusCodeStr. Every invalid address i have tried gets a null status code.

        • Rafael. try checking the value of the PrimaryAddressResolutionStatus property this value always return a valid code (not null) even if the address passed is invalid, you can translate the value to an string using the GetStatusCodeStr function.

  6. Just had to use this today. (:
    Gold as always.
    Do you know any drawback of using this comparing to usual IcmpSendEcho approach?


    • There is not drawbacks, you can get the same results using the IcmpSendEcho function or the Win32_PingStatus wmi class.

      • Thanks! (:
        the problem with IcmpSendEcho is that we should not statically link to the library. Instead, call LoadLibrary and then GetProccess, and if fails, try again.
        Sometimes, I just wanna a simpler code that work in all target windows…

  7. Pingback: WMI Tasks using Delphi – Networking « The Road to Delphi – a Blog about programming

  8. Hi Rodrigo, i would like of handle the timeout of the ping request . Could you give me some tips in how implement it using your source code?

  9. Hi Rodrigo
    Thank for good example. The ICMP of Indy on W7 64 not work, and your example work for all target windows.

  10. Muchas gracias por este trabajo!

  11. Hi. thank you from your good example.
    I have a question. How to change the port that is uses to ping via this method?
    Thank you very much.

  12. Como usaria este codigo en una aplicacion Win32 usando Delphi XE ???

    • Hola Jorge, el codigo de ejemplo es de una aplicacion de consola, si deseas usarla en una aplicaicon VCL solo debes adaptar el codigo reemplazando las llamadas al procedimiento Writeln.

  13. This code is really nice. However if the pinged computer is offline it takes ages to get a response. Can this be made quicker? Any suggestions?

  14. Beautiful solution… pulled my hair out on making IcmpSendEcho work… basically would return 0 even when I could ping the host via command line. This worked flawlessly… thank you so much Rodrigo… you are a rock star my friend!

  15. Great!really appreciate this, was struggling with the IcmpSendEcho.

  16. Hello rodrigo, many thanks for this example.
    I use the response time for calculations. With fast local conections the time is 1ms. Also i used more ping bytes. Do you know a easy :-) way to show the response time for example in micro seconds?
    Many many thanks

  17. Hi there I tried to use ICMP to ping a device on windows server 2012 and it takes 3 seconds to process Client.Ping; when client is offline. When its done on a windows server 2003 client.Ping; takes less than a second to inform the client is offline.
    Do you know how long does it takes to process a ping through your code WMI if client is offline?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s