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 (192.168.154.102), or IPv6 address (2010:836B:4179::836B:4179).
SELECT * FROM Win32_PingStatus where Address='www.google.com'
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='192.168.1.221' AND BufferSize=64 AND ResolveAddressNames=TRUE
Now check this sample console application.
program WMIPing; {$APPTYPE CONSOLE} uses SysUtils, ActiveX, ComObj, Variants; function GetStatusCodeStr(statusCode:integer) : string; begin 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' else result:='Unknow'; end; end; //The form of the Address parameter can be either the computer name (wxyz1234), IPv4 address (192.168.177.124), or IPv6 address (2010:836B:4179::836B:4179). procedure Ping(const Address:string;Retries,BufferSize:Word); var FSWbemLocator : OLEVariant; FWMIService : OLEVariant; FWbemObjectSet: OLEVariant; FWbemObject : OLEVariant; oEnum : IEnumvariant; iValue : LongWord; i : Integer; PacketsReceived : Integer; Minimum : Integer; Maximum : Integer; Average : Integer; begin; PacketsReceived:=0; Minimum :=0; Maximum :=0; Average :=0; Writeln(''); Writeln(Format('Pinging %s with %d bytes of data:',[Address,BufferSize])); FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', ''); //FWMIService := FSWbemLocator.ConnectServer('192.168.52.130', 'root\CIMV2', 'user', 'password'); for i := 0 to Retries-1 do begin 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 begin if FWbemObject.StatusCode=0 then begin if FWbemObject.ResponseTime>0 then Writeln(Format('Reply from %s: bytes=%s time=%sms TTL=%s',[FWbemObject.ProtocolAddress,FWbemObject.ReplySize,FWbemObject.ResponseTime,FWbemObject.TimeToLive])) else Writeln(Format('Reply from %s: bytes=%s time=<1ms TTL=%s',[FWbemObject.ProtocolAddress,FWbemObject.ReplySize,FWbemObject.TimeToLive])); Inc(PacketsReceived); if FWbemObject.ResponseTime>Maximum then Maximum:=FWbemObject.ResponseTime; if Minimum=0 then Minimum:=Maximum; if FWbemObject.ResponseTime<Minimum then Minimum:=FWbemObject.ResponseTime; Average:=Average+FWbemObject.ResponseTime; end else if not VarIsNull(FWbemObject.StatusCode) then Writeln(Format('Reply from %s: %s',[FWbemObject.ProtocolAddress,GetStatusCodeStr(FWbemObject.StatusCode)])) else Writeln(Format('Reply from %s: %s',[Address,'Error processing request'])); end; FWbemObject:=Unassigned; FWbemObjectSet:=Unassigned; //Sleep(500); end; Writeln(''); 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 begin Writeln('Approximate round trip times in milli-seconds:'); Writeln(Format(' Minimum = %dms, Maximum = %dms, Average = %dms',[Minimum,Maximum,Round(Average/PacketsReceived)])); end; end; begin try CoInitialize(nil); try //Ping('192.168.52.130',4,32); Ping('theroadtodelphi.wordpress.com',4,32); finally CoUninitialize; end; except on E:Exception do Writeln(E.Classname, ':', E.Message); end; Readln; end.
And the output
Pingback: Making a PING with Delphi and the WMI
February 2, 2011 at 8:48 am
Nice post, but the code has broken lines.
Please don’t use tags, use tags, like this :
.
Thanks.
Thanks.
February 2, 2011 at 2:37 pm
WanC , which browser are you using? the code looks good in Firefox and IE Explorer.
February 2, 2011 at 9:07 am
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
February 2, 2011 at 2:41 pm
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.
February 2, 2011 at 10:03 am
Muy bueno el código !!!
Gracias
February 2, 2011 at 2:41 pm
Domingo, gracias por tus comentarios.
April 5, 2011 at 10:53 am
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:
Ping(‘theroadtodelphi.wordpress.com’,4,32);
for
Ping(‘aaaaaa’,4,32);
in this case, FWbemObject.StatusCode is getting NULL instead of the error code.
April 5, 2011 at 11:17 am
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.
April 5, 2011 at 12:39 pm
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.
April 5, 2011 at 12:56 pm
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.
April 5, 2011 at 1:10 pm
OK. Now it works perfect. You should consider changing your source code also.
Thanks for you help and good job btw.
April 5, 2011 at 1:15 pm
ok, no problem, glad to help you.
August 29, 2011 at 3:01 pm
Just had to use this today. (:
Gold as always.
Do you know any drawback of using this comparing to usual IcmpSendEcho approach?
thanks!
EMB
August 29, 2011 at 3:48 pm
There is not drawbacks, you can get the same results using the IcmpSendEcho function or the Win32_PingStatus wmi class.
August 30, 2011 at 10:40 am
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…
Pingback: WMI Tasks using Delphi – Networking « The Road to Delphi – a Blog about programming
August 9, 2012 at 8:31 am
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?
February 7, 2013 at 12:22 am
Just use the TimeOut property like so
P.D : Sorry for the late response.
June 23, 2013 at 2:50 am
Hi Rodrigo
Thank for good example. The ICMP of Indy on W7 64 not work, and your example work for all target windows.
November 7, 2013 at 3:46 pm
Muchas gracias por este trabajo!
February 3, 2014 at 12:11 pm
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.
February 3, 2014 at 12:25 pm
The Win32_PingStatus WMI Class uses the ICMP protocol to make a echo request, and ICMP does not use ports, so you cannot set a port in a ping request.
February 3, 2014 at 1:08 pm
Thank you from your answer
April 30, 2014 at 11:13 am
Como usaria este codigo en una aplicacion Win32 usando Delphi XE ???
April 30, 2014 at 4:09 pm
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.
September 30, 2014 at 2:28 pm
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?
October 2, 2014 at 10:40 am
Try using the Timeout property if the Win32_PingStatus class.
November 1, 2014 at 7:43 pm
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!
January 28, 2015 at 3:55 am
Great!really appreciate this, was struggling with the IcmpSendEcho.
March 20, 2016 at 10:36 am
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
March 20, 2016 at 11:23 pm
The ping accuracy of the Win32_PingStatus class is milliseconds. I’m not aware of another class or function which provides a better accuracy.
June 22, 2016 at 10:36 am
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?
Thanks
June 22, 2016 at 5:09 pm
NO, I don’t made that test before, but you can try yourself.
October 24, 2016 at 10:59 am
Hi Rodrigo,
your code is the perfect option for my automatic backup system, after I had trouble working with the ICMP approach.
Thank you
October 24, 2016 at 11:07 am
You are welcome.