找回密码
 加入
搜索
查看: 7696|回复: 9

[网络通信] _NetworkAdapterInfo() 获取网卡信息出错

  [复制链接]
发表于 2012-5-7 15:59:35 | 显示全部楼层 |阅读模式
2块虚拟网卡,2块真实网卡,一块无线网卡,$NetworkAdapterInfo[0][0]=6?实际上不应该是5吗?

Msgbox后发现其中一块虚拟网卡被统计了两次?!

而且$NetworkAdapterInfo[0][9]的值也不是当前网卡的名称?
 楼主| 发表于 2012-5-8 22:16:41 | 显示全部楼层
回复 1# ljxu


    没人知道吗?
发表于 2012-5-9 07:29:31 | 显示全部楼层
本帖最后由 nmgwddj 于 2012-5-9 07:32 编辑

回复 2# ljxu





#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 634, 246)
$ListView1 = GUICtrlCreateListView("网卡名称|MAC地址|IP地址|链路速度", 8, 40, 618, 198)
_GUICtrlListView_SetColumnWidth($ListView1, 0, 290)
$Button1 = GUICtrlCreateButton("Exit", 552, 8, 75, 25)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
SetListView()
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE, $Button1
                        Exit

        EndSwitch
WEnd

Func SetListView()
        $a = _GetAdaptersInfo()
        $index = ($a[1][3])
        For $i = 1 To $a[0][0]
                GUICtrlCreateListViewItem($a[$i][1] & '|' & _
                                StringLeft(Hex($a[$i][2]), 12) & '|' & _
                                $a[$i][6] & '|' & _
                                GetIfEntry($a[$i][3]) & ' MB', $ListView1)
        Next
EndFunc   ;==>SetListView

Func GetIfEntry($ifIndex)
        Local $tagBuffer, $tBuffer, $pBuffer, $iResult, $iSpeed, $sDescr

        $tagBuffer = "wchar[256];dword[5];byte[8];dword[16];char[256]"
        $tBuffer = DllStructCreate($tagBuffer)
        $pBuffer = DllStructGetPtr($tBuffer)
        DllStructSetData($tBuffer, 2, $ifIndex, 1)

        $iResult = DllCall("iphlpapi.dll", "long", "GetIfEntry", "ptr", $pBuffer)
        $iSpeed = DllStructGetData($tBuffer, 2, 4) / 1000 / 1000
        $sDescr = DllStructGetData($tBuffer, 5)
        $tBuffer = 0
        Return SetError($iResult[0], $iSpeed, $iSpeed)
EndFunc   ;==>GetIfEntry

Func _GetAdaptersInfo()
        Local $iResult, $tBuffer, $pBuffer, $aResult[1][9], $tagADPTINFO, $tAdpt

        ; 第一次调用传递空值,pOutBufLen ( $iResult[2] ) 设为结构所需大小,单位byte。
        $iResult = DllCall("iphlpapi.dll", "dword", "GetAdaptersInfo", "ptr", 0, "ulong*", 0)
        $tBuffer = DllStructCreate("byte[" & $iResult[2] & "]") ; 定义$iResult[2] 字节的缓存区域 (分配内存空间)。
        $pBuffer = DllStructGetPtr($tBuffer) ; 获取内存指针。

        ; 第二次调用,GetAdaptersInfo把网卡信息复制到指定的内存空间 ($tBuffer) 中。
        $iResult = DllCall("iphlpapi.dll", "dword", "GetAdaptersInfo", "ptr", $pBuffer, "ulong*", $iResult[2])
        ; $iResult[0]值为0则调用成功,否则为系统错误号。

        ; 数据转换, byte --> IP_ADAPTER_INFO
        $tagADPTINFO = "ptr NextAdpt; dword ComboIndex; char AdptName[260]; char AdptDescr[132];uint AddrLength;byte MacAddr[8];dword Index;uint Type; uint DhcpEnabled;ptr CurrentIpAddr;ptr NextIpAddr; char IpAddr[16];char IpAddrMask[16]; dword IpAddrCxt; ptr NextGateway; char GatewayAddr[16]; char GatewayAddrMask[16];dword GatewayCxt; ptr NextDhcp; char DhcpAddr[16]; char DhcpAddrMask[16];dword DhcpCxt; int HaveWins; ptr NextPriWinsServer; char PriWinsServerAddr[16]; char PriWinsServerAddrMask[16]; dword PriWinsServerCxt; ptr NextSecWinsServer; char SecWinsServerAddr[16]; char SecWinsServerAddrMask[16]; dword LeaseObtained; dword LeaseExpires"

        While $pBuffer
                $tAdpt = DllStructCreate($tagADPTINFO, $pBuffer)
                $aResult[0][0] += 1
                ReDim $aResult[$aResult[0][0] + 1][9]
                $aResult[$aResult[0][0]][0] = DllStructGetData($tAdpt, "AdptName") ; 网卡名称
                $aResult[$aResult[0][0]][1] = DllStructGetData($tAdpt, "AdptDescr") ; 网卡描述
                $aResult[$aResult[0][0]][2] = DllStructGetData($tAdpt, "MacAddr") ; 网卡MAC
                $aResult[$aResult[0][0]][3] = DllStructGetData($tAdpt, "Index") ; 网卡索引号
                $aResult[$aResult[0][0]][4] = DllStructGetData($tAdpt, "Type") ; 类型
                $aResult[$aResult[0][0]][5] = DllStructGetData($tAdpt, "DhcpEnabled") ; DHCP是否启用 true = 启用, false = 禁用
                $aResult[$aResult[0][0]][6] = DllStructGetData($tAdpt, "IpAddr") ; IP 地址
                $aResult[$aResult[0][0]][7] = DllStructGetData($tAdpt, "GatewayAddr") ; 网关地址
                $aResult[$aResult[0][0]][8] = DllStructGetData($tAdpt, "DhcpAddr") ; DHCP地址, 只有DhcpEnabled为true时,此值才有效。
                $pBuffer = DllStructGetData($tAdpt, "NextAdpt") ; [下一张网卡信息的内存地址。]
                $tAdpt = 0
        WEnd
        $tBuffer = 0
        Return SetError($iResult[0], 0, $aResult)
EndFunc   ;==>_GetAdaptersInfo

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 1金钱 +10 收起 理由
iamwonvy + 10 谢谢

查看全部评分

 楼主| 发表于 2012-5-9 09:02:13 | 显示全部楼层
回复 3# nmgwddj
 楼主| 发表于 2012-5-9 09:03:13 | 显示全部楼层
回复 4# ljxu


    谢谢,但是不是_NetworkAdapterInfo() 这个函数本身就是有问题?
发表于 2012-5-9 09:22:04 | 显示全部楼层
SVN 3.3.9.4
发表于 2012-5-9 09:53:41 | 显示全部楼层
你什么版本 我用没问题 啊
 楼主| 发表于 2012-5-9 12:22:07 | 显示全部楼层
回复 7# ooxxgod


    v3.3.7.15 (beta),是版本问题?
发表于 2012-5-9 13:15:54 | 显示全部楼层
2块虚拟网卡,2块真实网卡,一块无线网卡,$NetworkAdapterInfo[0][0]=6?实际上不应该是5吗?

Msgbox后 ...
ljxu 发表于 2012-5-7 15:59


嗯,我直接运行下面两句:
-------------------------------------
#Include <ACN_NET.au3>
_NetworkAdapterInfo()
---------------------------------------
也会出现下面的错误,以前的老版本记得是不会出错的
现在的AU3版本:        3.3.9.0

"D:\autoit3\UserInclude\ACN_NET.au3" (236) : ==> ?????????? ')'.:
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled != 0", "WQL", 0x10 + 0x20)
$colItems = ^ ERROR
发表于 2012-5-9 21:10:35 | 显示全部楼层
;
; 函数名称:        _NetworkAdapterInfo()
; 详细信息:        获得网卡信息
; 返回值说明:
; 以二维数组方式返回.例如 $info=_NetworkAdapterInfo()
; $info[0][0]=网卡数量
; $info[1][0]第一块网卡的标志1
; $info[2][0]第二块网卡的标志2
; $info[1][1]第一块网卡的网卡名称
; $info[2][1]第二块网卡的网卡名称
; $info[1][2]第一块网卡的默认网关
; $info[1][3]第一块网卡的DNS主机名称(本机名称)
; $info[1][4]第一块网卡的IP地址
; $info[1][5]第一块网卡的主DNS
; $info[1][6]第一块网卡的次DNS
; $info[1][7]第一块网卡的子网掩码
; $info[1][8]第一块网卡的MAC地址
; $info[1][9]第一块网卡的连接名称
; 注意,此UDF不会获取已经禁用的网卡。
; 如果有需要,请删除' WHERE IPEnabled != 0'和' WHERE NetConnectionStatus >0'
; 作者:            thesnow(rundll32@126.com)
不要自以为是好不好, $info[1][9]第一块网卡的连接名称。
你说的$NetworkAdapterInfo[0][0]=6,你看看函数就知道了。
你把你的网络连接停用,你就觉得是所谓的“正常”了,
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-2 19:49 , Processed in 0.080009 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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