Page 1 of 1

[SOLVED] ping and unicode

Posted: Thu Sep 08, 2016 2:00 pm
by supercdfr
I have this code :

Code: Select all

InitNetwork()

Global EchoMessage.s = "MVPing make a Ping Test"

Procedure Ping(valeur)
  Protected sIPAddress.s
  Protected IPAddress.l
  Protected lngResult.l
  
  Repeat
    sIPAddress = GetGadgetText(3)
    
    ReplyBuffer.s = Space(SizeOf(ICMP_ECHO_REPLY) + Len(EchoMessage))
    hIcmpFile = IcmpCreateFile_()
    dwRetVal = IcmpSendEcho_(hIcmpFile, inet_addr_(sIPAddress), @EchoMessage, Len(EchoMessage), #Null, @ReplyBuffer, Len(ReplyBuffer) + SizeOf(ICMP_ECHO_REPLY), 1000)
    IcmpCloseHandle_(hFile)
    *Echo.ICMP_ECHO_REPLY = @ReplyBuffer
    
    If (*Echo\DataSize) > 0 ; ping ok
      SetGadgetColor(3,#PB_Gadget_BackColor,RGB(0,255,0))
    Else ; no ping
      SetGadgetColor(3,#PB_Gadget_BackColor,RGB(255,0,0))
      Delay(1000)
    EndIf
  ForEver
EndProcedure

If OpenWindow(0, 245, 73, 295, 175, "Wake on Lan", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
  StringGadget(3, 100, 10, 190, 20, "192.168.0.8")
EndIf

CreateThread(@ping(),1)

Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
It works fine under 5.42 non unicode, but not in 5.42 with unicode neither 5.50.
The problem is the unicode support, but don't see why.
this line is the problem :

Code: Select all

If (*Echo\DataSize) > 0
without unicode, it returned 0. With unicode, it returns 32 whatever there's ping or not.

Re: ping and unicode

Posted: Thu Sep 08, 2016 2:20 pm
by supercdfr
foind this solution :

Code: Select all

Procedure.q lngNewAddress(strAdd.s)
  Protected sDummy.s=strAdd
  Protected Position = FindString(sDummy, ".",1)
  If Position>0
    Protected a1=Val(Left(sDummy,Position-1))
    sDummy=Right(sDummy,Len(sDummy)-Position)
    Position = FindString(sDummy, ".",1)
    If Position>0
      Protected A2=Val(Left(sDummy,Position-1))
      sDummy=Right(sDummy,Len(sDummy)-Position)
      Position = FindString(sDummy, ".",1)
      If Position>0
        Protected A3=Val(Left(sDummy,Position-1))
        sDummy=Right(sDummy,Len(sDummy)-Position)
        Protected A4=Val(sDummy)
        Protected dummy.q=0
        PokeB(@dummy,a1)
        PokeB(@dummy+1,A2)
        PokeB(@dummy+2,A3)
        PokeB(@dummy+3,A4)
        ProcedureReturn dummy
      EndIf
    EndIf
  EndIf
EndProcedure
and make this changes :

Code: Select all

  Protected sIPAddress.q
.
.
    sIPAddress = lngNewAddress( GetGadgetText(3) )
.
.
   dwRetVal = IcmpSendEcho_(hIcmpFile, sIPAddress, @EchoMessage, Len(EchoMessage), #Null, @ReplyBuffer, Len(ReplyBuffer) + SizeOf(ICMP_ECHO_REPLY), 1000)
Is it basic or C++ ????

Re: [SOLVED] ping and unicode

Posted: Thu Sep 08, 2016 4:15 pm
by infratec
First of all:

Please use EnableExplicit, then you'll find already a few bugs.

Next:
You did not understand the problem.
The windows api function expects ASCII strings.
So simply convert them (mainly the IP address)

Which results in:

Code: Select all

EnableExplicit

InitNetwork()

Global EchoMessage.s = "MVPing make a Ping Test"

Procedure Ping(valeur)
  
  Protected sIPAddress.s
  Protected IPAddress.l
  Protected lngResult.l
  Protected ReplyBuffer.s
  Protected hIcmpFile.i, dwRetVal.i, *Echo.ICMP_ECHO_REPLY
  Protected *IPBuffer
  
  Repeat
    sIPAddress = GetGadgetText(3)
    
    *IPBuffer = AllocateMemory(StringByteLength(sIPAddress, #PB_Ascii) + SizeOf(Character))
    If *IPBuffer
      PokeS(*IPBuffer, sIPAddress, -1, #PB_Ascii)
    EndIf
    
    
    ReplyBuffer = Space(SizeOf(ICMP_ECHO_REPLY) + StringByteLength(EchoMessage))
    hIcmpFile = IcmpCreateFile_()
    dwRetVal = IcmpSendEcho_(hIcmpFile, inet_addr_(*IPBuffer), @EchoMessage, StringByteLength(EchoMessage), #Null, @ReplyBuffer, StringByteLength(ReplyBuffer) + SizeOf(ICMP_ECHO_REPLY), 1000)
    IcmpCloseHandle_(hIcmpFile)
    *Echo = @ReplyBuffer
    
    If (*Echo\DataSize) > 0 ; ping ok
      SetGadgetColor(3,#PB_Gadget_BackColor,RGB(0,255,0))
    Else ; no ping
      SetGadgetColor(3,#PB_Gadget_BackColor,RGB(255,0,0))
      Delay(1000)
    EndIf
  ForEver
  
EndProcedure


Define Event.i

If OpenWindow(0, 245, 73, 295, 175, "Wake on Lan", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
  StringGadget(3, 100, 10, 190, 20, "192.168.182.247")
  
  CreateThread(@ping(),1)
  
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf
Bernd