iphlpapi.dll 操作路由表失败!XP下提示 87 求助
本帖最后由 风雨网络 于 2013-5-23 16:34 编辑WIN7 测试通过XP 失败了!#Region ;**** 参数创建于 ACNWrapper_GUI ****
#PRE_UseUpx=n
#PRE_UseX64=n
#PRE_Res_requestedExecutionLevel=None
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****
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 = []
$iResult = DllCall($IPHLPAPI, "long", "GetIpForwardTable", "ptr", 0, "long*", 0, "bool", 1)
If ($iResult < 4) Then Return SetError($iResult, 0, $aResult)
$tBinary = DllStructCreate("ubyte Binary[" & $iResult & "]")
$pBinary = DllStructGetPtr($tBinary)
$iResult = DllCall($IPHLPAPI, "long", "GetIpForwardTable", "ptr", $pBinary, "long*", $iResult, "bool", 1)
If ($iResult) Then Return SetError($iResult, 0, $aResult)
$tNumberofEntries = DllStructCreate("long NumberofEntries", $pBinary)
$aResult = DllStructGetData($tNumberofEntries, "NumberofEntries")
Redim $aResult[$aResult + 1]
For $i = 1 To $aResult
$tBuffer = DllStructCreate($tagMIB_IPFORWARDROW, $pBinary + 4 + ($i - 1) * 56)
For $j = 0 To 13
$aResult[$i][$j] = DllStructGetData($tBuffer, $j + 1)
Next
$aResult[$i] = _ConvertUlongToChars($aResult[$i])
$aResult[$i] = _ConvertUlongToChars($aResult[$i])
$aResult[$i] = _ConvertUlongToChars($aResult[$i])
Next
Return $aResult
EndFunc ;==>_EnumIpForwordEntries
Func _ConvertUlongToChars($iUlong)
Local $iResult = DllCall("Ws2_32.dll", "str", "inet_ntoa", "ulong", $iUlong)
Return $iResult
EndFunc ;==>_ConvertUlongToChars
Func _ConvertCharsToUlong($sChars)
Local $iResult = DllCall("Ws2_32.dll", "long", "inet_addr", "str", $sChars)
Return $iResult
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, $iResult = 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, $iResult = 0)
EndFunc ;==>_DeleteIpForwardEntry
Func _GetBestInterface($sAddress)
Local $iResult
$iResult = DllCall($IPHLPAPI, "long", "GetBestInterface", "long", _ConvertCharsToUlong($sAddress), "long*", 0)
Return SetError($iResult, 0, $iResult)
EndFunc ;==>_GetBestInterface
If _CreateIpForwardEntry("221.239.208.1", "255.255.255.255", "192.168.0.215", -1, 3, 60) Then
MsgBox(48, "OK", "Done")
Else
MsgBox(48, "Error", "Failed with error " & @error)
EndIf
If _DeleteIpForwardEntry("221.239.208.1", "255.255.255.255", "192.168.0.215", -1) Then
MsgBox(48, "OK", "Done")
Else
MsgBox(48, "Error", "Failed with error " & @error)
EndIf 本帖最后由 ceoguang 于 2013-5-23 18:24 编辑
87为ERROR_INVALID_PARAMETER(参数错误)
An input parameter is invalid, no action was taken. This error is returned if the pRoute parameter is NULL, the dwForwardProto member of MIB_IPFORWARDROW was not set to MIB_IPPROTO_NETMGMT, the dwForwardMask member of the PMIB_IPFORWARDROW structure is not a valid IPv4 subnet mask, or one of the other members of the MIB_IPFORWARDROW structure is invalid.
87为ERROR_INVALID_PARAMETER(参数错误)
An input parameter is invalid, no action was taken. This error is returned if the pRoute parameter is NULL, the dwForwardProto member of MIB_IPFORWARDROW was not set to MIB_IPPROTO_NETMGMT, the dwForwardMask member of the PMIB_IPFORWARDROW structure is not a valid IPv4 subnet mask, or one of the other members of the MIB_IPFORWARDROW structure is invalid.
回复 3# ceoguang
参数没错误吧! WIN7 上测试OK 的!不知道为何XP 不行! 如果上面的提示还不够清析,那么请参考http://msdn.microsoft.com/en-us/library/windows/desktop/aa366850(v=vs.85).aspx关于dwForwardProto成员的说明 回复 5# ceoguang
版主这个看得云里雾里! 我刚把这个发给别人XP 测试提示OK 是可以的! 我自己到2003上运行也是可以的!
应该是系统上精简或者优化了什么导致的! 只是我现在找不到修改系统上的那些东西导致的这个问题! 回复ceoguang
版主这个看得云里雾里! 我刚把这个发给别人XP 测试提示OK 是可以的! 我自己到2003上运 ...
风雨网络 发表于 2013-5-23 19:02 http://www.autoitx.com/images/common/back.gif
抱嫌我没看清你的代码,dwForwardProto成员在你的代码中是指定了MIB_IPPROTO_NETMGMT的.并没有问题.
而按你的返回提示,MSDN上指出是因为MIB_IPFORWARDROW结构中一个或多个成员未设置值. 回复 7# ceoguang
版主 ("221.239.208.1", "255.255.255.255", "x.x.x.x", -1) 在XP 上这个里的X 如果是本机IP 地址或者VPN 获取到的IP 运行就会失败返回87 如果这个IP是不存在的就会提示OK !我要实现的是将221.239.208.1添加到本地IP地址或者VPN获取到的IP 看了你说的这个!还是不懂怎么解决!
页:
[1]