How to ping computer?
;==========================================================================================================================================
;
; How to use 'ping' to check if a computer is available on the network?
;
; How to 'ping' with WinBatch?
;
;
; (c)Detlev Dalitz.20110126.
;
;==========================================================================================================================================

DirChange (DirScript ())

; Computer names for testing.
strComputer1 = "www.windowware.com"
strComputer2 = "google.com"
strComputer3 = "www.www"

strListComputers = strComputer1 : @TAB : strComputer2 : @TAB : strComputer3

intCount = ItemCount (strListComputers, @TAB)
For intI = 1 To intCount

   strComputer = ItemExtract (intI, strListComputers, @TAB)

   ;------------------------------------------------------------------------------------------------------------------------------------------
   :Example1

   strWql = "Select * from Win32_PingStatus Where Address = '" : strComputer : "'"

   objWMIService = GetObject ("winmgmts:\\.\root\cimv2")
   objItems = objWMIService.ExecQuery(strWql)
   ForEach objItem In objItems
      Message ("Win32_PingStatus", strComputer : @LF : "StatusCode = " : objItem.StatusCode : @LF : "Response time = " : objItem.ResponseTime)
   Next

   objItems = 0
   objItem = 0
   objWMIService = 0
   ;------------------------------------------------------------------------------------------------------------------------------------------

   ;------------------------------------------------------------------------------------------------------------------------------------------
   :Example2

   AddExtender ("WWINT44I.DLL")
   intResult = iPing (strComputer, 10)
   intLastError = iGetLastError ()

   strMsgTitle = "WinInet iPing"
   strMsgText1 = strComputer : @LF : "successful" : @LF : "Result = " : intResult : @LF : "Last Error = " : intLastError
   strMsgText2 = strComputer : @LF : "not successful" : @LF : "Result = " : intResult : @LF : "Last Error = " : intLastError

   If intLastError == 0
      Message (strMsgTitle, strMsgtext1)
   Else
      Message (strMsgTitle, strMsgtext2)
   EndIf
   ;------------------------------------------------------------------------------------------------------------------------------------------

   ;------------------------------------------------------------------------------------------------------------------------------------------
   :Example3

   AddExtender ("WWIPG44I.DLL")
   intResult = ipPing (strComputer, 10)
   intLastError = ipGetLastErr ()

   strMsgTitle = "IPGrabber ipPing"
   strMsgText1 = strComputer : @LF : "successful" : @LF : "Result = " : intResult : @LF : "Last Error = " : intLastError
   strMsgText2 = strComputer : @LF : "not successful" : @LF : "Result = " : intResult : @LF : "Last Error = " : intLastError

   If intLastError == 0
      Message (strMsgTitle, strMsgtext1)
   Else
      Message (strMsgTitle, strMsgtext2)
   EndIf
   ;------------------------------------------------------------------------------------------------------------------------------------------

   ;------------------------------------------------------------------------------------------------------------------------------------------
   :Example4

   objShell = ObjectCreate ("WScript.Shell")
   conShell = ObjectConstantsGet (objShell)

   ; ping.exe, where "-n 1" means one try and "-w 10000" means 10 seconds wait.
   objExec = objShell.Exec(: "%%comspec%% /c ping -n 1 -w 10000 " : strComputer)

   strStdOut = objExec.StdOut.ReadAll(); There are @CRLF and @CR.
   intExitCode = objExec.ExitCode

   strStdOut = StrReplace (strStdOut, @CRLF, @LF)
   strStdOut = StrReplace (strStdOut, @CRLF, @LF)
   IntControl (63, 100, 100, 900, 600) ; Window position.
   IntControl (28, 1, 0, 0, 0) ; Font fixed pitch.
   AskItemlist ("Result", strStdOut, @LF, @UNSORTED, @SINGLE)

   strMsgTitle = "PING.EXE"
   strMsgText1 = strComputer : @LF : "successful" : @LF : "Exit Code = " : intExitCode
   strMsgText2 = strComputer : @LF : "not successful" : @LF : "Exit Code = " : intExitCode

   If intExitCode == 0
      Message (strMsgTitle, strMsgText1)
   Else
      Message (strMsgTitle, strMsgText2)
   EndIf

   objExec = 0
   conShell = 0
   objShell = 0
   ;------------------------------------------------------------------------------------------------------------------------------------------

Next

:CANCEL
Exit