Results 1 to 2 of 2

Thread: IcmpSendEcho2 doesn't fill buffer

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    2

    IcmpSendEcho2 doesn't fill buffer

    Hi,

    I've got a tricky problem and am already working hours on it to solve it. Maybe I'm blind already, but I just can't find out what the problem is. I've got a nice piece of Excel Workbook with which I can scan and inventory devices on the network. to see what is on the network I currently use IcmpSendEcho, but depending on how big the network is it takes a while until the Pings run into a timeout. So instead of using IcmpSendEcho I tried to use IcmpSendEcho2. Basically the same funcitonality, but the function returns immediately and raises an event as soon as it has received the reply or run into time-out. So far so good - the only problem I have is that for whatever reason it doesn't fill the buffer with data. My guess is that this is because I haven't declared the API functions correctly and therefore the API writes the data to the wrong memory location. I don't think that the function itself has a bug. I didn't find a lot of pages on the internet relating to IcmpSendEcho2, but the ones that have used it seems not to have any problem.

    Here is a test- code for both - btw, the functions are a quite nice solution for pings...


    And just to clarify in advance - the IcmpSendEcho works perfectly - I only inlcuded it here in the test file so that you can compare the results.

    IP Addresses are random ones - you need to change them if you want to test it.


    VB Code:
    1. Private Const PING_TIMEOUT As Long = 2000 'Wait 2 secs
    2.  
    3. Private Type ICMP_OPTIONS
    4.     ttl             As Byte
    5.     Tos             As Byte
    6.     Flags           As Byte
    7.     OptionsSize     As Byte
    8.     OptionsData     As Long
    9. End Type
    10.  
    11. Private Type ICMP_ECHO_REPLY
    12.     Address         As Long
    13.     Status          As Long
    14.     RoundTripTime   As Long
    15.     datasize        As Long
    16.     DataPointer     As Long
    17.     Options         As ICMP_OPTIONS
    18.     Data            As String * 250
    19. End Type
    20.  
    21. Public Type SECURITY_ATTRIBUTES
    22.     nLength As Long
    23.     lpSecurityDescriptor As Long
    24.     bInheritHandle As Long
    25. End Type
    26.  
    27. Public Declare Function CreateEvent Lib "kernel32" Alias "CreateEventA" (lpEventAttributes As SECURITY_ATTRIBUTES, ByVal bManualReset As Long, ByVal bInitialState As Long, ByVal lpName As String) As Long
    28. Public Declare Function WaitForMultipleObjects Lib "kernel32" (ByVal nCount As Long, lpHandles As Long, ByVal bWaitAll As Long, ByVal dwMilliseconds As Long) As Long
    29. Public Declare Function ResetEvent Lib "kernel32" (ByVal hEvent As Long) As Long
    30. Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    31.  
    32. Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long
    33. Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal IcmpHandle As Long) As Long
    34. Private Declare Function IcmpSendEcho Lib "icmp.dll" (ByVal IcmpHandle As Long, ByVal DestinationAddress As Long, _
    35. ByVal RequestData As String, ByVal RequestSize As Long, ByVal RequestOptions As Long, ReplyBuffer As ICMP_ECHO_REPLY, _
    36. ByVal ReplySize As Long, ByVal Timeout As Long) As Long
    37. Private Declare Function IcmpSendEcho2 Lib "icmp.dll" (ByVal IcmpHandle As Long, ByVal hEvent As Long, ByVal ApcRoutine As Any, _
    38. ByVal ApcContext As Long, ByVal DestinationAddress As Long, ByVal RequestData As String, ByVal RequestSize As Long, _
    39. ByVal RequestOptions As Long, ReplyBuffer As ICMP_ECHO_REPLY, ByVal ReplySize As Long, ByVal Timeout As Long) As Long
    40.  
    41. Private Declare Function inet_addr Lib "wsock32.dll" (ByVal s As String) As Long
    42. Private Declare Function inet_ntoa Lib "wsock32.dll" (ByVal addr As Long) As Long
    43.  
    44.  
    45. Sub test1()
    46.     Const sSendData As String = "TESTMESSAGE"
    47.     Dim buffer As ICMP_ECHO_REPLY
    48.     Dim lhwndPort As Long
    49.      
    50.     lhwndPort = IcmpCreateFile
    51.     Cells(1, 1) = "Pinging 10.10.11.1 using IcmpSendEcho"
    52.     If IcmpSendEcho(lhwndPort, inet_addr("10.10.11.1"), sSendData, Len(sSendData), _
    53.     0, buffer, Len(buffer), PING_TIMEOUT) <> 0 Then
    54.     Cells(1, 2) = buffer.Address
    55. Else
    56.     Cells(1, 2) = "Failed"
    57. End If
    58. End Sub
    59.  
    60.  
    61. Sub test2()
    62.     Const sSendData As String = "TESTMESSAGE"
    63.     Dim buffer As ICMP_ECHO_REPLY
    64.     Dim lhwndPort As Long
    65.     Dim hEvent As Long
    66.     Dim sd As SECURITY_ATTRIBUTES
    67.      
    68.     With sd
    69.         .nLength = Len(sd) 'we pass the length of sd
    70.         .lpSecurityDescriptor = 0
    71.         .bInheritHandle = 0
    72.     End With
    73.      
    74.     hEvent = CreateEvent(sd, True, False, "PING2")
    75.      
    76.     lhwndPort = IcmpCreateFile
    77.     Cells(2, 1) = "Pinging 10.10.11.1 using IcmpSendEcho2"
    78.     Call IcmpSendEcho2(lhwndPort, hEvent, 0&, 0, inet_addr("10.10.11.1"), sSendData, Len(sSendData), _
    79.     0, buffer, Len(buffer), PING_TIMEOUT)
    80.     If WaitForMultipleObjects(1, hEvent, True, 10000) = 0 Then
    81.         Cells(2, 2) = buffer.Address
    82.     Else
    83.         Cells(2, 2) = "Failed"
    84.     End If
    85. End Sub


    Hope somebody can help me...

    goppi.

  2. #2

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    2

    Re: IcmpSendEcho2 doesn't fill buffer

    Solved it alrady... - it had to do with the buffer pointer

    I changed the declaration to

    VB Code:
    1. Private Declare Function IcmpSendEcho2 Lib "icmp.dll" ( _
    2.   ByVal IcmpHandle As Long, _
    3.   ByVal hEvent As Long, _
    4.   ByVal ApcRoutine As Any, _
    5.   ByVal ApcContext As Long, _
    6.   ByVal DestinationAddress As Long, _
    7.   ByVal RequestData As String, _
    8.   ByVal RequestSize As Long, _
    9.   ByVal RequestOptions As Long, _
    10.   ByVal ReplyBuffer As Long, _
    11.   ByVal ReplySize As Long, _
    12.   ByVal Timeout As Long) As Long

    .. and the call to

    VB Code:
    1. IcmpSendEcho2(lhwndPort, hEvent, 0, 0, inet_addr("10.10.11.1"), sSendData, Len(sSendData), 0, VarPtr(buffer), Len(buffer), 10000)

    The trick is the undocumented VBA routine VarPtr.

    Goppi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width