this is a more-or-less complete implementation of the wmi class win32_pingstatus in vfp.

this class has some more platform restrictions as well as not being available in windows 2000 or 95 or 98:

starting with windows vista, win32_pingstatus can return data for computers that have both ipv4 addresses and ipv6 addresses.

windows server 2003 and windows xp win32_pingstatus only returns data for computers running ipv4.

it's useful to be able to ping as part of a multi-computer scripting operation as it lets you know if it's worth trying the much more expensive operation of actually trying to connect to the wmi service: if you can't ping, you won't be able to connect to the wmi service, and you find out a lot faster.

also, and potentially more useful, the statuscode property could help diagnose connection problems in other scenarios, notably applications and error logging - i.e. 'why are database connections failing - what happens if we ping the database server?" it's a bit easier than parsing the output of microsoft's ping command, for anything other than the basic outcome at least.

it's been sitting in my draft folder for three or four years so you'll have to excuse any code-rot.

clear

owmi = getobject("winmgmts:")

* specify address or computer name or url
* caddress = "192.168.1.125"
caddress = "apple.com" && works 21 june 2009 - resolves to 17.112.152.57
* caddress = "computer_name

if os() < "windows 5.01"
messagebox("this will only work in windows xp and later")
return
endif

opings = owmi.execquery([select * from win32_pingstatus where address='] + caddress + ['])

for each oping in opings
* this class does not actually support enumeration,
* there is only one ping object returned and this is how to access it.

? "status code: ", getstatuscode(oping.statuscode)
?
? "address: ", oping.address
? "buffer size", oping.buffersize
? "no fragmentation", oping.nofragmentation
? "primaryaddressresolutionstatus", oping.primaryaddressresolutionstatus
? "protocoladdress", oping.protocoladdress
? "protocoladdressresolved", oping.protocoladdressresolved
? "recordroute", oping.recordroute
? "replyinconsistency", oping.replyinconsistency
? "replysize", oping.replysize
? "resolveaddressnames", oping.resolveaddressnames
? "responsetime", oping.responsetime
? "responsetimetolive", oping.responsetimetolive
? "sourceroute", oping.sourceroute
? "sourceroutetype", getsourceroutetype(oping.sourceroutetype)
? "timeout", oping.timeout
? "timestamproute", oping.timestamproute
? "timetolive", oping.timetolive
? "typeofservice", gettypeofservice(oping.typeofservice)

if not isnull(oping.routerecord)
for each oitem in oping.routerecord
? oitem
next
endif

if not isnull(oping.routerecordresolved)
for each oitem in oping.routerecordresolved
? oitem
next
endif

if not isnull(oping.routerecordresolved)
for each oitem in oping.timestamprecord
? oitem
next
endif

if not isnull(oping.timestamprecordaddress)
for each oitem in oping.timestamprecordaddress
? oitem
next
endif

if not isnull(oping.timestamprecordaddressresolved)
for each oitem in oping.timestamprecordaddressresolved
? oitem
next
endif

next

procedure getsourceroutetype
lparameters nsourceroutetype

local ctype

do case
case nsourceroutetype = 1
ctype = "loose source routing"
case nsourceroutetype = 2
ctype = "strict source routing"
otherwise
* default - 0 - or any other value.
ctype = "none"
endcase

return ctype

endproc

procedure gettypeofservice
lparameters nservicetype

local ctype as string

do case
case nservicetype = 2
ctype = "minimize monetary cost"
case nservicetype = 4
ctype = "maximize reliability"
case nservicetype = 8
ctype = "maximize throughput"
case nservicetype = 16
ctype = "minimize delay"

otherwise
* default - 0 - or any other value.
ctype = "normal"

endcase

return ctype

endproc

procedure getstatuscode (ccode as string) as string

local cstatus

do case
case ccode = 0
cstatus = "success"
case ccode = 11001
cstatus = "buffer too small"
case ccode = 11002
cstatus = "destination net unreachable"
case ccode = 11003
cstatus = "destination host unreachable"
case ccode = 11004
cstatus = "destination protocol unreachable"
case ccode = 11005
cstatus = "destination port unreachable"
case ccode = 11006
cstatus = "no resources"
case ccode = 11007
cstatus = "bad option"
case ccode = 11008
cstatus = "hardware error"
case ccode = 11009
cstatus = "packet too big"
case ccode = 11010
cstatus = "request timed out"
case ccode = 11011
cstatus = "bad request"
case ccode = 11012
cstatus = "bad route"
case ccode = 11013
cstatus = "timetolive expired transit"
case ccode = 11014
cstatus = "timetolive expired reassembly"
case ccode = 11015
cstatus = "parameter problem"
case ccode = 11016
cstatus = "source quench"
case ccode = 11017
cstatus = "option too big"
case ccode = 11018
cstatus = "bad destination"
case ccode = 11032
cstatus = "negotiating ipsec"
case ccode = 11050
cstatus = "general failure"

otherwise
cstatus = "unknown"
endcase

return cstatus

endproc

2 Responses to Found WMI Snippets #1 – WIN32_PingStatus

  • noel says:

    Will the  above code run in Linux-Wine-VFP?

  • stuartd says:

    > Will the  above code run in Linux-Wine-VFP

    I would expect it not to work, there’s not much WMI support in Wine: I don’t have Wine on my Linux, so I can’t try it. I think there’s a native Ubuntu WMI-Client implementation in 8.04 and later, check your distro..

    –stuart

Leave a Reply

Your email address will not be published. Required fields are marked *