The problem is with the IcmpSendEcho API function.

Just starting out? Need help? Post your questions and find answers here.
Rudines
New User
New User
Posts: 2
Joined: Sat Jan 08, 2022 6:23 pm

The problem is with the IcmpSendEcho API function.

Post 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?
User_Russian
Addict
Addict
Posts: 1588
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: The problem is with the IcmpSendEcho API function.

Post 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)) 
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: The problem is with the IcmpSendEcho API function.

Post 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.
Last edited by spikey on Sat Sep 14, 2024 2:29 pm, edited 1 time in total.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: The problem is with the IcmpSendEcho API function.

Post 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
Rudines
New User
New User
Posts: 2
Joined: Sat Jan 08, 2022 6:23 pm

Re: The problem is with the IcmpSendEcho API function.

Post 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!
User avatar
jacdelad
Addict
Addict
Posts: 2031
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: The problem is with the IcmpSendEcho API function.

Post by jacdelad »

It was also discussed several time why it worked on 6.04 and not later...
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply