Page 1 of 1

The problem is with the IcmpSendEcho API function.

Posted: Sat Sep 14, 2024 11:18 am
by Rudines
Hi guys, help me solve the problem.
There is a program:

Code: Select all

Procedure.w Ping(lngDAddress.l, paramTimeOut=1000, paramCountPing=4)
Define ECHO.ICMP_ECHO_REPLY 
Define lngHPort.l, lngResult.l
Define *buffer, CountPing;
Define strMessage.s = "Ping, ping, ping!!!"
Define CountMillSec.w=0, ColGoodPing.w = 0
Define MsgLen.b = Len(strMessage)

lngHPort = IcmpCreateFile_() 

*buffer=AllocateMemory(SizeOf(ICMP_ECHO_REPLY)+MsgLen) 

For CountPing = 1 To paramCountPing
	lngResult = IcmpSendEcho_(lngHPort, lngDAddress, @strMessage, MsgLen , #Null,*buffer, SizeOf(ICMP_ECHO_REPLY)+MsgLen, paramTimeOut) 
	
	CopyMemory(*buffer,@ECHO,SizeOf(ICMP_ECHO_REPLY)) 
	
	If ECHO\RoundTripTime=0 And ECHO\Options\Ttl>0 And ECHO\Status=0
    		CountMillSec+1
    		ColGoodPing+1
    	ElseIf ECHO\RoundTripTime>0 And ECHO\Options\Ttl>0 And ECHO\Status=0
      		CountMillSec + ECHO\RoundTripTime 
      		ColGoodPing + 1
    	EndIf
    	Debug "CountPing="+CountPing+"   Address="+IPString(lngDAddress)+"   RoundTripTime="+ECHO\RoundTripTime+"   Status="+ECHO\Status+"   TTL="+ECHO\Options\Ttl
	Delay(1) 
Next  CountPing
  
If CountMillSec>0
  	PingResult = CountMillSec/ColGoodPing ;CountPing
Else
	PingResult = -1
EndIf

FreeMemory(*buffer) 
lngResult = IcmpCloseHandle_(lngHPort) 

ProcedureReturn PingResult 

EndProcedure 

Debug Ping(MakeIPAddress(8,8,8,8))
In version 6.04 this program worked, but in subsequent versions it stopped working.
Debugger version 6.11 and version 6.12 give an error:

[15:09:03] Waiting for executable to start...
[15:09:03] Executable type: Windows - x64 (64bit, Unicode, Thread, Purifier)
[15:09:03] Executable started.
[15:09:06] The debugged executable quit unexpectedly.

What could it be?

Re: The problem is with the IcmpSendEcho API function.

Posted: Sat Sep 14, 2024 1:27 pm
by User_Russian
This is not a PB bug.
This is a bug in your code. Incorrect variable type.

Code: Select all

Procedure.w Ping(lngDAddress.l, paramTimeOut=1000, paramCountPing=4)
Define ECHO.ICMP_ECHO_REPLY 
Define lngHPort, lngResult
Define *buffer, CountPing
Define strMessage.s = "Ping, ping, ping!!!"
Define CountMillSec.w=0, ColGoodPing.w = 0
Define MsgLen.b = Len(strMessage)

lngHPort = IcmpCreateFile_() 

*buffer=AllocateMemory(SizeOf(ICMP_ECHO_REPLY)+MsgLen) 

For CountPing = 1 To paramCountPing
	lngResult = IcmpSendEcho_(lngHPort, lngDAddress, @strMessage, MsgLen , #Null,*buffer, SizeOf(ICMP_ECHO_REPLY)+MsgLen, paramTimeOut) 
	
	CopyMemory(*buffer,@ECHO,SizeOf(ICMP_ECHO_REPLY)) 
	
	If ECHO\RoundTripTime=0 And ECHO\Options\Ttl>0 And ECHO\Status=0
    		CountMillSec+1
    		ColGoodPing+1
    	ElseIf ECHO\RoundTripTime>0 And ECHO\Options\Ttl>0 And ECHO\Status=0
      		CountMillSec + ECHO\RoundTripTime 
      		ColGoodPing + 1
    	EndIf
    	Debug "CountPing="+CountPing+"   Address="+IPString(lngDAddress)+"   RoundTripTime="+ECHO\RoundTripTime+"   Status="+ECHO\Status+"   TTL="+ECHO\Options\Ttl
	Delay(1) 
Next  CountPing
  
If CountMillSec>0
  	PingResult = CountMillSec/ColGoodPing ;CountPing
Else
	PingResult = -1
EndIf

FreeMemory(*buffer) 
lngResult = IcmpCloseHandle_(lngHPort) 

ProcedureReturn PingResult 

EndProcedure 

Debug Ping(MakeIPAddress(8,8,8,8)) 

Re: The problem is with the IcmpSendEcho API function.

Posted: Sat Sep 14, 2024 1:47 pm
by spikey
A handle needs to be a 64-bit integer on 64-bit systems not a 32-bit long. There was a discussion of this recently, but I can't find the thread at the moment.

Re: The problem is with the IcmpSendEcho API function.

Posted: Sat Sep 14, 2024 2:03 pm
by Quin
spikey wrote: Sat Sep 14, 2024 1:47 pm A handle needs to be an 64-bit integer on 64-bit systems not a 32-bit long. There was a discussion of this recently, but I can't find the thread at the moment.
viewtopic.php?p=617359
viewtopic.php?p=615678
It was even talked about in the release topic: viewtopic.php?p=613828

Re: The problem is with the IcmpSendEcho API function.

Posted: Sat Sep 14, 2024 6:03 pm
by Rudines
This code was taken from this forum and worked well on PB 6.04 (x64).
Now I will not use LONG type variables on 64-bit systems.
Thank you all very much!

Re: The problem is with the IcmpSendEcho API function.

Posted: Sat Sep 14, 2024 9:45 pm
by jacdelad
It was also discussed several time why it worked on 6.04 and not later...