New topic: ICMPSendEcho
<http://forums.realsoftware.com/viewtopic.php?t=36321> Page 1 of 1 [ 4 posts ] Previous topic | Next topic Author Message rebrandsoftware Post subject: ICMPSendEchoPosted: Sun Nov 14, 2010 12:07 am Joined: Wed Mar 25, 2009 4:30 pm Posts: 148 Location: Alexandria, VA, USA Hi Everyone, I hate having to ask for help with these crazy declares so frequently. I feel like I'm learning a lot due to timhare's help over the last week (thank you again, Tim!). Here is a function that should be able to send an ICMP Echo (Ping) on Windows. I'm converting it from VB. It complies and it runs, but returns zero responses and leaves my buffer empty. I know there is an ICMP Socket at http://www.boisseau.co.uk/code/, and if this doesn't work I will certainly take that approach, but I would really like to master these windows declares because the project I am converting is full of them. Here is what I have so far: Code:Function Ping(sIP as string) As Integer Declare Function WSAStartup Lib "wsock32" (wVersionRequired as integer, byref buffer as ptr) as integer Declare Function inet_addr Lib "wsock32" (s As WString) As integer Declare Function IcmpCreateFile Lib "icmp.dll" () As integer 'Declare Function IcmpCreateFile Lib "icmp.dll" () As ptr 'Declare Function IcmpCloseHandle Lib "icmp.dll" (IcmpHandle As ptr) As Integer Declare Function IcmpCloseHandle Lib "icmp.dll" (IcmpHandle As Integer) As Integer Declare Function WSACleanup Lib "wsock32" As Integer Declare Function GetLastError Lib "kernel32" as Integer Declare Function IcmpSendEcho Lib "icmp.dll" (IcmpHandle as Integer, DestinationAddress as Integer, RequestData as WString, RequestSize as Integer, RequestOptions as Integer, byRef ReplyBuffer as ptr, ReplySize as Integer, Timeout as Integer) as integer 'DWORD IcmpSendEcho( '__in HANDLE IcmpHandle, '__in IPAddr DestinationAddress, '__in LPVOID RequestData, '__in WORD RequestSize, '__in PIP_OPTION_INFORMATION RequestOptions, '__inout LPVOID ReplyBuffer, '__in DWORD ReplySize, '__in DWORD Timeout '); dim WSAbuffer as ptr dim mEchoBuffer as MemoryBlock dim pEchoBuffer as ptr dim iRet as Integer dim iAddress as Integer 'dim pHandle as ptr dim iPort as Integer dim sData as string = "1234567890abcdef" dim iRoundTrip as integer dim iStatus as integer dim iReplyCount as integer dim iError as integer iRet = WSAStartup(WS_VERSION_REQD, WSAbuffer) iAddress = inet_Addr(sIP) if iAddress <> INADDR_NONE then 'pHandle = ICMPCreateFile() 'iPort = pHandle.UInt32(0) 'before I was sending the actual port number instead of the handle of the port. Now I am sending the handle. iPort = ICMPCreateFile if iPort > 0 then iReplyCount = IcmpSendEcho(iPort, iAddress, sData, len(sData), 0, pEchoBuffer, 278, PING_TIMEOUT) 'size should be 278 to match an ICMP_ECHO_REPLY iError = GetLastError if iReplyCount > 0 then iStatus = pEchoBuffer.UInt32(4) 'offset 4 in ICMP_ECHO_REPLY iRoundTrip = pEchoBuffer.UInt32(8) 'offset 8 else if iError <> 0 then MsgBox "Error: " + cstr(iError) end if end if 'call IcmpCloseHandle(pHandle) call IcmpCloseHandle(iPort) end if end if call WSACleanup() Return iRoundTrip End Function Any ideas? -Mike Top timhare Post subject: Re: ICMPSendEchoPosted: Sun Nov 14, 2010 12:43 am Joined: Fri Jan 06, 2006 3:21 pm Posts: 8686 Location: Portland, OR USA One thing that jumps out right away is that a WORD is a 16 bit value, not an Integer. RequestSize as UInt16 Top timhare Post subject: Re: ICMPSendEchoPosted: Sun Nov 14, 2010 1:30 am Joined: Fri Jan 06, 2006 3:21 pm Posts: 8686 Location: Portland, OR USA It looks like you are responsible for allocating the return buffer. And the buffer needs to be big enough to include the string you sent (and will be echoed back). You should probably use a CString instead of WString for the send data. You don't want all those nulls in the data you send. It expects a narrow string, not a wide string. Code:mEchoBuffer = New MemoryBlock(278+Len(sData)) iReplyCount = IcmpSendEcho(iPort, iAddress, sData, len(sData), 0, mEchoBuffer, mEchoBuffer.Size, PING_TIMEOUT) You don't need pEchoBuffer. RB converts the memoryblock to ptr automatically. Top timhare Post subject: Re: ICMPSendEchoPosted: Sun Nov 14, 2010 1:39 am Joined: Fri Jan 06, 2006 3:21 pm Posts: 8686 Location: Portland, OR USA Where did you get 278 for the size of an ICMP_ECHO_REPLY block? It looks like 28 to me. Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 4 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
