Private Declare Function GetIpForwardTable Lib "iphlpapi" (ByRef pIpForwardTable As Any, ByRef pdwSize As Long, ByVal bOrder As Long) As Long 如果用它得到路由表信息?
就是不知道怎么调用啊..哈哈.. 我这有个代码..有点乱..调不出来. Private Type MIB_IPFORWARDROW dwForwardDest As Long dwForwardMask As Long dwForwardPolicy As Long dwForwardNextHop As Long dwForwardIfIndex As Long dwForwardType As Long dwForwardProto As Long dwForwardAge As Long dwForwardNextHopAS As Long dwForwardMetric1 As Long dwForwardMetric2 As Long dwForwardMetric3 As Long dwForwardMetric4 As Long dwForwardMetric5 As Long End Type Private Type MIB_IPFORWARDTABLE dwNumEntries As Long ANY_SIZE() As MIB_IPFORWARDROW End TypePrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dst As Any, src As Any, ByVal bcount As Long) Private Declare Function GetIpForwardTable Lib "iphlpapi" (ByRef pIpForwardTable As Any, ByRef pdwSize As Long, ByVal bOrder As Long) As Long Private Declare Function inet_ntoa Lib "wsock32" (ByVal addr As Long) As Long Private Declare Function lstrcpyA Lib "kernel32" (ByVal RetVal As String, ByVal Ptr As Long) As Long Private Declare Function lstrlenA Lib "kernel32" (ByVal Ptr As Any) As LongSub test() Dim row As MIB_IPFORWARDROW Dim buflen As Long, ret As Long Dim nRows As Long, nStructSize Dim i As Integer, octet(3) As Byte'Get Buffer length only this api pass ret = GetIpForwardTable(0&, buflen, 0)ReDim rtable(buflen) As Byte routetable.Text = GetStrFromPtrA(inet_ntoa(row.dwForwardProto)) '& " " & GetStrFromPtrA(inet_ntoa(row.dwForwardMask)) & "->" & GetStrFromPtrA(inet_ntoa(row.dwForwardNextHopAS)) 'get table data on second api pass ret = GetIpForwardTable(rtable(0), buflen, 0)'get number entries 'first 4 bytes is a long indicating the 'number of entries in 'the table CopyMemory nRows, rtable(0), 4 nStructSize = LenB(row) For i = 1 To nRows 'Move a 'row' from the table into the MIB_IPFORWARDROW row CopyMemory row, rtable(4 + (i - 1) * nStructSize), nStructSize 'This is the long to byte array stuff (now unused) CopyMemory octet(0), '//row.dwForwardDest , 4 'octet array now holds the octets (what else?) 'This is the VBNet implimentation with dots Debug.Print GetStrFromPtrA (inet_ntoa(row.dwForwardDest)) & " / ": GetStrFromPtrA (inet_ntoa(row.dwForwardMask)) 'routetable.Text = GetIpForwardTable Next' End SubPublic Function GetStrFromPtrA(ByVal lpszA As Long) As String GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0) Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA) End Function Private Sub Command1_Click() Call test Dim row As MIB_IPFORWARDROW 'routetable.Text = GetIpForwardTable(All, All, All) 'MsgBox "hi" End Sub
现在好像差这句话无法理解了,或者不知道怎么写才对. 'the table CopyMemory nRows, rtable(0), 4 nStructSize = LenB(row)
这是两句话, 第一句的意思是路由表头4个字节是Long型,nRows代表表结构数组总数 第二句的意思是nStructSize为表结构数组中每个结构的长度,就是LenB(row) 注意:因为是在内存中Copy,所以不能用Len(row)取长度。 虽然这里因结构中只有Long元素,类型很单一,两种取法得到的值相同,但道理不同,所以不要只看结果。我帮你改了一下原代码,在与原作者的一些观点不同之处,加入了自己的一点想法,你可参考。以下代码在WinXP下调试通过Option ExplicitPrivate Type MIB_IPFORWARDROW dwForwardDest As Long dwForwardMask As Long dwForwardPolicy As Long dwForwardNextHop As Long dwForwardIfIndex As Long dwForwardType As Long dwForwardProto As Long dwForwardAge As Long dwForwardNextHopAS As Long dwForwardMetric1 As Long dwForwardMetric2 As Long dwForwardMetric3 As Long dwForwardMetric4 As Long dwForwardMetric5 As Long End Type Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dst As Any, src As Any, ByVal bcount As Long) Private Declare Function GetIpForwardTable Lib "iphlpapi" (ByRef pIpForwardTable As Any, ByRef pdwSize As Long, ByVal bOrder As Long) As LongPrivate Function LngToAddress(lngAddr As Long) As String '这是我自己写的Long转换成IP字符串的函数,比API转换要安全与高效; Dim myByte(3) As Byte, s(3) As String Dim i As Long CopyMemory myByte(0), lngAddr, 4 For i = 0 To 3 s(i) = CStr(myByte(i)) Next LngToAddress = Join(s, ".") End FunctionPrivate Sub Test() Dim i As Long, nStructSize As Long Dim nRows As Long, bBytes() As Byte, buflen As Long Dim row As MIB_IPFORWARDROW On Error GoTo Fail nStructSize = LenB(row) GetIpForwardTable ByVal 0&, buflen, 0 '第一次调用取得数据长度 If buflen <= 0 Then Exit Sub ReDim bBytes(0 To buflen - 1) As Byte '按数据长度定义Byte数组 GetIpForwardTable bBytes(0), buflen, 0 '第二次调用取路由表数据 CopyMemory nRows, bBytes(0), 4 '路由表头4个字节是Long型,代表表结构数组总数nRows For i = 0 To nRows - 1 CopyMemory row, bBytes(4 + (i * nStructSize)), nStructSize '按结构长度复制数据 Debug.Print LngToAddress(row.dwForwardDest) & " / " & LngToAddress(row.dwForwardMask) '输出 Next Fail: End SubPrivate Sub Command1_Click() Test End Sub
我这有个代码..有点乱..调不出来.
Private Type MIB_IPFORWARDROW
dwForwardDest As Long
dwForwardMask As Long
dwForwardPolicy As Long
dwForwardNextHop As Long
dwForwardIfIndex As Long
dwForwardType As Long
dwForwardProto As Long
dwForwardAge As Long
dwForwardNextHopAS As Long
dwForwardMetric1 As Long
dwForwardMetric2 As Long
dwForwardMetric3 As Long
dwForwardMetric4 As Long
dwForwardMetric5 As Long
End Type
Private Type MIB_IPFORWARDTABLE
dwNumEntries As Long
ANY_SIZE() As MIB_IPFORWARDROW
End TypePrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dst As Any, src As Any, ByVal bcount As Long)
Private Declare Function GetIpForwardTable Lib "iphlpapi" (ByRef pIpForwardTable As Any, ByRef pdwSize As Long, ByVal bOrder As Long) As Long
Private Declare Function inet_ntoa Lib "wsock32" (ByVal addr As Long) As Long
Private Declare Function lstrcpyA Lib "kernel32" (ByVal RetVal As String, ByVal Ptr As Long) As Long
Private Declare Function lstrlenA Lib "kernel32" (ByVal Ptr As Any) As LongSub test()
Dim row As MIB_IPFORWARDROW
Dim buflen As Long, ret As Long
Dim nRows As Long, nStructSize
Dim i As Integer, octet(3) As Byte'Get Buffer length only this api pass ret = GetIpForwardTable(0&, buflen, 0)ReDim rtable(buflen) As Byte
routetable.Text = GetStrFromPtrA(inet_ntoa(row.dwForwardProto))
'& " " & GetStrFromPtrA(inet_ntoa(row.dwForwardMask)) & "->" & GetStrFromPtrA(inet_ntoa(row.dwForwardNextHopAS))
'get table data on second api pass ret = GetIpForwardTable(rtable(0), buflen, 0)'get number entries 'first 4 bytes is a long indicating the 'number of entries in
'the table CopyMemory nRows, rtable(0), 4 nStructSize = LenB(row)
For i = 1 To nRows 'Move a 'row' from the table into the MIB_IPFORWARDROW row
CopyMemory row, rtable(4 + (i - 1) * nStructSize), nStructSize
'This is the long to byte array stuff (now unused) CopyMemory octet(0),
'//row.dwForwardDest , 4 'octet array now holds the octets (what else?)
'This is the VBNet implimentation with dots Debug.Print
GetStrFromPtrA (inet_ntoa(row.dwForwardDest)) & " / ": GetStrFromPtrA (inet_ntoa(row.dwForwardMask))
'routetable.Text = GetIpForwardTable
Next'
End SubPublic Function GetStrFromPtrA(ByVal lpszA As Long) As String
GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0)
Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA)
End Function
Private Sub Command1_Click()
Call test
Dim row As MIB_IPFORWARDROW
'routetable.Text = GetIpForwardTable(All, All, All)
'MsgBox "hi"
End Sub
没弄过这东西,但看起来不难。不过,“iphlpapi”与系统(主要是IE)有很大关系,GetIpForwardTable在Win9X下不一定能正常使用。
取路由表需调用GetIpForwardTable两次,第一次取回表长度,第二次才取实际数据
取回的数据,前四个字节为表结构数组元素个数,然后按个数用循环从数据中Copy出表结构就行了。按这个思路去做,你就会成功的!
'the table CopyMemory nRows, rtable(0), 4 nStructSize = LenB(row)
第一句的意思是路由表头4个字节是Long型,nRows代表表结构数组总数
第二句的意思是nStructSize为表结构数组中每个结构的长度,就是LenB(row)
注意:因为是在内存中Copy,所以不能用Len(row)取长度。
虽然这里因结构中只有Long元素,类型很单一,两种取法得到的值相同,但道理不同,所以不要只看结果。我帮你改了一下原代码,在与原作者的一些观点不同之处,加入了自己的一点想法,你可参考。以下代码在WinXP下调试通过Option ExplicitPrivate Type MIB_IPFORWARDROW
dwForwardDest As Long
dwForwardMask As Long
dwForwardPolicy As Long
dwForwardNextHop As Long
dwForwardIfIndex As Long
dwForwardType As Long
dwForwardProto As Long
dwForwardAge As Long
dwForwardNextHopAS As Long
dwForwardMetric1 As Long
dwForwardMetric2 As Long
dwForwardMetric3 As Long
dwForwardMetric4 As Long
dwForwardMetric5 As Long
End Type
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dst As Any, src As Any, ByVal bcount As Long)
Private Declare Function GetIpForwardTable Lib "iphlpapi" (ByRef pIpForwardTable As Any, ByRef pdwSize As Long, ByVal bOrder As Long) As LongPrivate Function LngToAddress(lngAddr As Long) As String
'这是我自己写的Long转换成IP字符串的函数,比API转换要安全与高效;
Dim myByte(3) As Byte, s(3) As String
Dim i As Long
CopyMemory myByte(0), lngAddr, 4
For i = 0 To 3
s(i) = CStr(myByte(i))
Next
LngToAddress = Join(s, ".")
End FunctionPrivate Sub Test()
Dim i As Long, nStructSize As Long
Dim nRows As Long, bBytes() As Byte, buflen As Long
Dim row As MIB_IPFORWARDROW
On Error GoTo Fail
nStructSize = LenB(row)
GetIpForwardTable ByVal 0&, buflen, 0 '第一次调用取得数据长度
If buflen <= 0 Then Exit Sub
ReDim bBytes(0 To buflen - 1) As Byte '按数据长度定义Byte数组
GetIpForwardTable bBytes(0), buflen, 0 '第二次调用取路由表数据
CopyMemory nRows, bBytes(0), 4 '路由表头4个字节是Long型,代表表结构数组总数nRows
For i = 0 To nRows - 1
CopyMemory row, bBytes(4 + (i * nStructSize)), nStructSize '按结构长度复制数据
Debug.Print LngToAddress(row.dwForwardDest) & " / " & LngToAddress(row.dwForwardMask) '输出
Next
Fail:
End SubPrivate Sub Command1_Click()
Test
End Sub