icmpsendecho2的vb6实现

时间:2015-08-29 22:27:15

标签: vb6 ping

我试图在vb6中实现一个适用于IPv4和DSP的Ping程序。 IPv6地址。 IPv4实现使用IcmpSendEcho,它工作正常,但IPv6实现使用Icmp6SendEcho2,我很难让它工作。

函数调用正常,它不会出错,但返回值始终为0,GetLastError返回0表示没有错误发生

https://msdn.microsoft.com/en-us/library/windows/desktop/aa366041(v=vs.85).aspx

我遵循的流程如下:

  • 通过调用WSAStartup
  • 加载Windows套接字dll
  • 通过调用Icmp6CreateHandle
  • 打开端口句柄
  • 通过调用Icmp6SendEcho2发送ICMP ECHO消息并分析响应
  • 通过调用IcmpCloseHandle
  • 关闭端口句柄
  • 通过调用WSACleanup
  • 卸载dll

Icmp6SendEcho2的定义如下

Private Declare Function Icmp6SendEcho2 Lib "Iphlpapi.dll" _
    (ByVal IcmpHandle As Long, _
    ByVal EventtoRaise As Long, _
    ByVal ApcRoutine As Long, _
    ByVal ApcContext As Long, _
    ByVal SourceAddressPointer As Long, _
    ByVal DestinationAddressPointer As Long, _
    ByVal RequestData As String, _
    ByVal RequestSize As Long, _
    ByVal RequestOptions As Long, _
    ReplyBuffer As ICMPV6_ECHO_REPLY, _
    ByVal ReplySize As Long, _
    ByVal timeOut As Long) As Long

ICMPV6_ECHO_REPLY的定义如下

Private Type ICMPV6_ECHO_REPLY
    Address As IPV6_ADDRESS
    Status As Long
    RoundTripTime As Long
End Type

Private Type IPV6_ADDRESS
    sin6_port As Integer
    sin6_flowinfo As Long
    sin6_addr(1 To 8) As Integer
    sin6_scope_id As Long
End Type

对Icmp6SendEcho2的调用如下:

lPingResponse = Icmp6SendEcho2(lPortHandle, 0, 0, 0, VarPtr(SourceAddress), VarPtr(DestinationAddress), sMessage, Len(sMessage), 0, Reply, Len(Reply), timeOut)

我使用以下链接实现了Ping for IPv4 https://support.microsoft.com/en-us/kb/300197

任何帮助都很棒

尼什

1 个答案:

答案 0 :(得分:0)

问题与没有足够的缓冲区来获得响应有关。将响应结构更改为类似这样的技巧

Private Type ICMPV6_ECHO_REPLY
  Address As IPV6_ADDRESS
  Status As Long
  RoundTripTime As Long
  data(0 To 1023) As Byte
End Type