Const $IPHLPAPI = DllOpen("iphlpapi.dll")
Const $tagMIB_IPFORWARDROW = "long Destination;long NetworkMask;long Policy;long NextHop;long IfIndex;long Type;long Proto;long Age;long NextHopAS;long Metric1;long Metric2;long Metric3;long Metric4;long Metric5"
Func _EnumIpForwordEntries()
Local $iResult, $tBuffer, $tBinary, $pBinary, $tNumberofEntries, $aResult[1][14] = [[0]]
$iResult = DllCall($IPHLPAPI, "long", "GetIpForwardTable", "ptr", 0, "long*", 0, "bool", 1)
If ($iResult[2] < 4) Then Return SetError($iResult[0], 0, $aResult)
$tBinary = DllStructCreate("ubyte Binary[" & $iResult[2] & "]")
$pBinary = DllStructGetPtr($tBinary)
$iResult = DllCall($IPHLPAPI, "long", "GetIpForwardTable", "ptr", $pBinary, "long*", $iResult[2], "bool", 1)
If ($iResult[0]) Then Return SetError($iResult[0], 0, $aResult)
$tNumberofEntries = DllStructCreate("long NumberofEntries", $pBinary)
$aResult[0][0] = DllStructGetData($tNumberofEntries, "NumberofEntries")
Redim $aResult[$aResult[0][0] + 1][14]
For $i = 1 To $aResult[0][0]
$tBuffer = DllStructCreate($tagMIB_IPFORWARDROW, $pBinary + 4 + ($i - 1) * 56)
For $j = 0 To 13
$aResult[$i][$j] = DllStructGetData($tBuffer, $j + 1)
Next
$aResult[$i][0] = _ConvertUlongToChars($aResult[$i][0])
$aResult[$i][1] = _ConvertUlongToChars($aResult[$i][1])
$aResult[$i][3] = _ConvertUlongToChars($aResult[$i][3])
Next
Return $aResult
EndFunc ;==>_EnumIpForwordEntries
Func _ConvertUlongToChars($iUlong)
Local $iResult = DllCall("Ws2_32.dll", "str", "inet_ntoa", "ulong", $iUlong)
Return $iResult[0]
EndFunc ;==>_ConvertUlongToChars
Func _ConvertCharsToUlong($sChars)
Local $iResult = DllCall("Ws2_32.dll", "long", "inet_addr", "str", $sChars)
Return $iResult[0]
EndFunc ;==>_ConvertCharsToUlong
Func _CreateIpForwardEntry($sDestination, $sNetworkMask, $sNextHop, $iIfIndex, $iType, $iMetric)
Local $tBuffer, $iResult
If $iIfIndex = -1 Then $iIfIndex = _GetBestInterface($sNextHop)
$tBuffer = DllStructCreate($tagMIB_IPFORWARDROW)
DllStructSetData($tBuffer, "Destination", _ConvertCharsToUlong($sDestination))
DllStructSetData($tBuffer, "NetworkMask", _ConvertCharsToUlong($sNetworkMask))
DllStructSetData($tBuffer, "NextHop", _ConvertCharsToUlong($sNextHop))
DllStructSetData($tBuffer, "IfIndex", $iIfIndex)
DllStructSetData($tBuffer ,"Type", $iType)
DllStructSetData($tBuffer, "Proto", 3)
DllStructSetData($tBuffer, "Policy", 0)
DllStructSetData($tBuffer, "Age", 0)
DllStructSetData($tBuffer, "NextHopAS", 0)
DllStructSetData($tBuffer, "Metric1", $iMetric)
DllStructSetData($tBuffer ,"Metric2", -1)
DllStructSetData($tBuffer ,"Metric3", -1)
DllStructSetData($tBuffer ,"Metric4", -1)
DllStructSetData($tBuffer ,"Metric5", -1)
$iResult = DllCall($IPHLPAPI, "long", "CreateIpForwardEntry", "ptr", DllStructGetPtr($tBuffer))
Return SetError($iResult[0], 0, $iResult[0] = 0)
EndFunc ;==>_CreateIpForwardEntry
Func _DeleteIpForwardEntry($sDestination, $sNetworkMask, $sNextHop, $iIfIndex, $iProto = 3)
Local $iResult, $tBuffer
If $iIfIndex = -1 Then $iIfIndex = _GetBestInterface($sNextHop)
$tBuffer = DllStructCreate($tagMIB_IPFORWARDROW)
DllStructSetData($tBuffer, "Destination", _ConvertCharsToUlong($sDestination))
DllStructSetData($tBuffer, "NetworkMask", _ConvertCharsToUlong($sNetworkMask))
DllStructSetData($tBuffer, "NextHop", _ConvertCharsToUlong($sNextHop))
DllStructSetData($tBuffer, "IfIndex", $iIfIndex)
DllStructSetData($tBuffer, "Proto", $iProto)
$iResult = DllCall($IPHLPAPI, "long", "DeleteIpForwardEntry", "ptr", DllStructGetPtr($tBuffer))
Return SetError($iResult[0], 0, $iResult[0] = 0)
EndFunc ;==>_DeleteIpForwardEntry
Func _GetBestInterface($sAddress)
Local $iResult
$iResult = DllCall($IPHLPAPI, "long", "GetBestInterface", "long", _ConvertCharsToUlong($sAddress), "long*", 0)
Return SetError($iResult[0], 0, $iResult[2])
EndFunc ;==>_GetBestInterface
If _CreateIpForwardEntry("1.2.3.4", "255.255.255.255", "1.2.3.0", -1, 3, 60) Then
MsgBox(48, "OK", "Done")
Else
MsgBox(48, "Error", "Failed with error " & @error)
EndIf
If _DeleteIpForwardEntry("1.2.3.4", "255.255.255.255", "1.2.3.0", -1) Then
MsgBox(48, "OK", "Done")
Else
MsgBox(48, "Error", "Failed with error " & @error)
EndIf
注意目标地址、网络掩码、网关地址 这3个参数要相互匹配。 |