我正在尝试在 vb6 中实现一个适用于 IPv4 和 IPv6 地址的 Ping 程序。 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)
我使用以下链接来实现 IPv4 的 Ping https://support.microsoft.com/en-us/kb/300197
任何帮助都会很棒
尼什
最佳答案
该问题与没有足够的缓冲区来获取响应有关。将响应结构更改为类似的内容就可以了
Private Type ICMPV6_ECHO_REPLY
Address As IPV6_ADDRESS
Status As Long
RoundTripTime As Long
data(0 To 1023) As Byte
End Type
关于icmpsendecho2 的 vb6 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32291290/