找回密码
 加入
搜索
查看: 1080|回复: 2

帮看看下面代码写错在哪了!

[复制链接]
发表于 2009-5-29 16:47:34 | 显示全部楼层 |阅读模式
本帖最后由 FBWOLF 于 2009-5-29 19:33 编辑

Local $iIndex = 0
DllCall("iphlpapi.dll", "dword", "GetBestInterface", "long", _inet_addr("192.168.0.1"), "int*", $iIndex)
if @error = 0 then
        MSGBOX(0,0,$iIndex)
EndIf
; 清除原有静态绑定
DllCall("iphlpapi.dll", "dword","FlushIpNetTable","dword",$iIndex)



Func _inet_addr($sAddr)
        Local $iAddr = DllCall("ws2_32.dll", "long", "inet_addr", "str", $sAddr)
        Return $iAddr[0]
EndFunc   ;==>_inet_addr




以下是原DELPHI代码  可以返回正确的INTERFACE INDEX


program ARP_S;

uses
  WinSock, Windows,SysUtils;

function GetBestInterface(dwDestAddr: integer; var pdwBestIfIndex: DWORD): DWORD; stdcall; external 'iphlpapi.dll' name 'GetBestInterface';
var
  iIndex: DWORD;
begin
  GetBestInterface(inet_addr('192.168.0.1'), iIndex);
  MessageBox(0, PChar(inttostr(iIndex)), '', 0);
end.
 楼主| 发表于 2009-5-29 16:56:12 | 显示全部楼层
无法返回正常的接口INDEX
发表于 2009-5-29 19:41:24 | 显示全部楼层
本帖最后由 sensel 于 2009-5-29 19:42 编辑

好吧,我们继续。。。

Local Const $sGWAddr = "192.168.0.1"
Local $iIndex

$iIndex = _GetBestInterface($sGWAddr)
If $iIndex <> 0 Then _FlushIpNetTable($iIndex)
Exit


;===============================================================================
; 说明:   获取到指定IP地址的最佳路由接口索引号
; 语法:   _GetBestInterface($sAddr)
; 参数:   $sAddr - IP地址
; 需要:   无
; 返回:   成功 - 接口索引号
;         失败 - 0, 并设置 @error 到 API操作返回值
; 备注:   无
;===============================================================================
Func _GetBestInterface($sAddr)
        Local $aResult, $iAddr, $iIndex, $tBestIfIndex, $pBestIfIndex

        $iAddr = _inet_addr($sAddr)
        $tBestIfIndex = DllStructCreate("dword")
        $pBestIfIndex = DllStructGetPtr($tBestIfIndex)

        $aResult = DllCall("iphlpapi.dll", "long", "GetBestInterface", "dword", $iAddr, "ptr", $pBestIfIndex)
        If $aResult[0] <> 0 Then Return SetError($aResult[0], 0, 0)

        Return DllStructGetData($tBestIfIndex, 1)
EndFunc   ;==>_GetBestInterface

;===============================================================================
; 说明:   清空指定网络适配器ARP表项目
; 语法:   _FlushIpNetTable($iAdptIndex)
; 参数:   $iAdptIndex - 网络适配器索引号
; 需要:   无
; 返回:   成功 - 1
;         失败 - 0, 并设置 @error 到 API操作返回值
; 备注:   出处: http://www.autoitx.com/forum.php?mod=viewthread&tid=7308 3#, 作者: pusofalse
;===============================================================================
Func _FlushIpNetTable($iAdptIndex)
        Local $aResult

        $aResult = DllCall("iphlpapi.dll", "long", "FlushIpNetTable", "long", $iAdptIndex)
        Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc   ;==>_FlushIpNetTable

Func _inet_addr($sAddr)
        Local $iAddr = DllCall("ws2_32.dll", "long", "inet_addr", "str", $sAddr)
        Return $iAddr[0]
EndFunc   ;==>_inet_addr
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-10 15:41 , Processed in 0.073468 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表