Ping and CLR

Started by stanl, January 29, 2014, 09:39:03 AM

Previous topic - Next topic

stanl

Still Trying to Learn..... I realize WB has an excellent ping method. Just trying to incorporate an MSDN example into the WB CLR. The code below worked when I just did a send with no options and checked for success. Then I added the options....... and everything went South

Code (WINBATCH) Select

;Winbatch 2014A - CLR Sample Ping
;
;Stan Littlefield January 28, 2014
;===================================================================================
ObjectClrOption("use", "System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
oPing = ObjectClrNew("System.Net.NetworkInformation.Ping")
oOptions = ObjectClrNew("System.Net.NetworkInformation.PingOptions")
nTimeOut = ObjectType("I4",128)
buffer = ObjectType("I1",32)
reply = oPing.Send("www.winbatch.com",nTimeOut,buffer,oOptions)
Display(5,"Pinging...","www.winbatch.com")
If reply.Status == 11010
   cReply=""
   cReply=cReply:"Address: ":reply.Address.ToString():@CRLF
   cReply=cReply:"RoundTripTime: ":reply.RoundtripTime:@CRLF
   cReply=cReply:"Time To Live: ":reply.Options.Ttl:@CRLF
   cReply=cReply:"Buffer Length: ":reply.Buffer.Length
   Message("Reply",cReply)
Else
   Display(3,"Ping Failed",reply.Status)
Endif
oPing.Dispose()
Exit

td

There may be more issues but for starters
Code (winbatch) Select

;;;buffer = ObjectType("I1",32)  bad form.

; Per c# example = good form.
objEncoder = ObjectClrNew("System.Text.Encoding")
strdata = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
buffer = objEncoder.ASCII.GetBytes(strData)

and
Code (winbatch) Select

IpStatus = ObjectClrNew("System.Net.NetworkInformation.IPStatus")
reply = oPing.Send("www.winbatch.com",nTimeOut,buffer,oOptions)
Display(5,"Pinging...","www.winbatch.com")
If reply.Status == IpStatus.Success    ; = 0 ;; 11010


Note that our routers block pings.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Deana

The Ping Send method has multiple overloads. http://msdn.microsoft.com/en-us/library/System.Net.NetworkInformation.Ping.Send(v=vs.110).aspx

It appears the one you are trying to call requires a Byte array for the Third parameter
Send(String, Int32, Byte(), PingOptions)

Here is some code that contains a code sample of how to create a Byte array in WinBatch, so that you can pass it to this method.

Code (winbatch) Select
;***************************************************************************
;**   DotNet Ping
;**
;** Purpose:
;**        Attempts to send an Internet Control Message Protocol (ICMP) echo message with the specified data buffer to the specified computer,
;**        and receive a corresponding ICMP echo reply message from that computer. This overload allows you to specify a time-out value for the
;**        operation and control fragmentation and Time-to-Live values for the ICMP packet.
;**       
;**        Send(String, Int32, Byte(), PingOptions)
;**       
;** Inputs:  URL
;** Outputs: Results in a Message
;** Reference:
;**       REQUIRES WinBatch 2013A or newer
;**
;** Developer: Deana Falk 2014.01.29
;***************************************************************************
If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf

URL = 'www.google.com'
;Error Handler to insure the Dispose method gets called
IntControl(73,1,0,0,0)

ObjectClrOption("use","System.Net, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a")
;ObjectClrOption('use', 'System.Net, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
oPing = ObjectClrNew('System.Net.NetworkInformation.Ping')

;An Int32 value that specifies the maximum number of milliseconds (after sending the echo message) to wait for the ICMP echo reply message.
nTimeOut = 128

; Create a Byte array that contains data to be sent with the ICMP echo message and returned in the ICMP echo reply message. The array cannot contain more than 65,500 bytes.
aByte = ArrDimension(1)
aByte[0] = 32 ;Data to send
bytearray =  ObjectType('array|ui1',aByte)

; A PingOptions object used to control fragmentation and Time-to-Live values for the ICMP echo message packet.
oOptions = ObjectClrNew('System.Net.NetworkInformation.PingOptions')
oOptions.Ttl = 128 ;default is 128

;oReply = ObjectClrType( 'System.Net.NetworkInformation.PingReply', '' )
oReply = oPing.Send( URL, nTimeOut, ByteArray, oOptions)
status =  oReply.Status
;Pause( 'Status', status )

; Ping command status codes.
; 0 Success
; 11001 Buffer Too Small
; 11002 Destination Net Unreachable
; 11003 Destination Host Unreachable
; 11004 Destination Protocol Unreachable
; 11005 Destination Port Unreachable
; 11006 No Resources
; 11007 Bad Option
; 11008 Hardware Error
; 11009 Packet Too Big
; 11010 Request Timed Out
; 11011 Bad Request
; 11012 Bad Route
; 11013 TimeToLive Expired Transit
; 11014 TimeToLive Expired Reassembly
; 11015 Parameter Problem
; 11016 Source Quench
; 11017 Option Too Big
; 11018 Bad Destination
; 11032 Negotiating IPSEC
; 11050 General Failure


;If the value of Status is not Success, you should not use the values returned by the RoundtripTime, Options or Buffer properties. The RoundtripTime and Buffer properties will return zero, and the Options property will return Nothing.
If status == 0
   addr = oReply.Address.ToString()
   rtt =  oReply.RoundtripTime
   ttl = oReply.Options.Ttl
   strReply = 'Address: ':addr:@CRLF
   strReply = strReply:'RoundTripTime: ':rtt:@CRLF
   strReply = strReply:'Time To Live: ':ttl:@CRLF
   Pause( 'Reply', strReply )
ElseIf status == 11010
   Pause( 'Ping Error: ':status , 'Request Timed Out. Server may not support Ping. In most cases, a "Request Timed Out" message is caused by a firewall blocking the connectivity.' )
Else
   Pause( 'Ping Failed', status )
Endif

oPing.Dispose()
oPing = 0
Exit

:WBERRORHANDLER
lasterr = wberrorarray[0]
handlerline = wberrorarray[1]
textstring = wberrorarray[5]
linenumber = wberrorarray[8]
errstr = StrCat("Number: ",lasterr,@LF,"String: ",textstring,@LF,"Line (",linenumber,"): '",handlerline,"'")
Message("Error Information",errstr)
if isDefined(oPing) && oPing !=0
   oPing.Dispose()
Endif










Deana F.
Technical Support
Wilson WindowWare Inc.

stanl

Thanks.  So, I needed to create an IPStatus Object, and now 2 ways to create a byte array - Winbatch or via System.Text.Encoding...  Tucked away.

Deana

Quote from: stanl on January 29, 2014, 12:00:29 PM
Thanks.  So, I needed to create an IPStatus Object, and now 2 ways to create a byte array - Winbatch or via System.Text.Encoding...  Tucked away.

Here is yet another method to create a byte array:

Code (winbatch) Select
ByteArray = ObjectType( "ARRAY|UI1", Arrayize( 32, @tab ) )
Deana F.
Technical Support
Wilson WindowWare Inc.