找回密码
 加入
搜索
查看: 1405|回复: 6

有其它办法返回当前活动IP的吗?我分享一个

  [复制链接]
发表于 2010-1-23 16:52:09 | 显示全部楼层 |阅读模式
本帖最后由 itljl 于 2010-1-24 23:25 编辑
$objWMIService = ObjGet("winmgmts:\\.\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE", "WQL", 0x10 + 0x20)
For $objItem In $colItems
 $LocalIP = $objItem.IPAddress(0)
Next
MsgBox(0, 0, 'IP:' & $LocalIP)
先分享上面这个。
这里活动IP的意思有几种情况。
1,如果机子有两个网卡,只用了其中一个,那怎样获取正在使用这个。
2,如果有两个网卡,其中一个没有接网线,怎样获取接了网线,正在使用这个。
3,如果机子是ADSL拨号上网,那正确的应该是获取外网的地址,@ipaddress1获取的有可能是 127.0.0.1 ,还有可能是本地连接设置的IP,而不是拨号连接的地址。这个可以用我分享这个代码来解决。

想知道哪位兄弟收藏的有其它方法,最好是API的。因为有些机子没开WMI服务的。
 楼主| 发表于 2010-1-23 16:54:31 | 显示全部楼层
论坛上已有的均是API的方式
发表于 2010-1-23 17:08:49 | 显示全部楼层
论坛里的网卡代码多的是WMI和注册表好不好。。API倒很少见。。
WMI有获取网卡状态(已连接、未插网线等等)的办法,可惜楼主的情况无法用上。。
发表于 2010-1-23 22:11:26 | 显示全部楼层
本帖最后由 pusofalse 于 2010-1-23 22:49 编辑

试下这样:
$iResult = DllCall("iphlpapi.dll", "dword", "GetIfTable", "ptr", 0, "ulong*", 0, "int", 1)
$tBinary = DllStructCreate("ubyte[" & $iResult[2] & "]")
$pBinary = DllStructGetPtr($tBinary)
$iResult = DllCall("iphlpapi.dll", "dword", "GetIfTable", "ptr", $pBinary, "ulong*", $iResult[2], 

"int", 1)

$tNum = DllStructCreate("ulong", $pBinary)
$iNum = DllStructGetData($tNum, 1)
$pBinary += 4

Local $sIfIndices = ""

For $i = 1 to $iNum
        $tBuffer = DllStructCreate("wchar[256];dword[5];byte[8];dword[16];char[256]", $pBinary)
        $pBinary += DllStructGetSize($tBuffer)
        If DllStructGetData($tBuffer, 2, 2) = 23 Then
                $sIfIndices = "," & DllStructGetData($tBuffer, 2, 1) & ","
                ExitLoop
        ElseIf DllStructGetData($tBuffer, 2, 2) = 6 And DllStructGetData($tBuffer, 4, 2) = 5 Then 
                $sIfIndices &= "," & DllStructGetData($tBuffer, 2, 1) & ","
        EndIf
Next

$iResult = DllCall("iphlpapi.dll", "dword", "GetAdaptersInfo", "ptr", 0, "ulong*", 0)
$tBinary = DllStructCreate("ubyte[" & $iResult[2] & "]")
$pBinary = DllStructGetPtr($tBinary)
$iResult = DllCall("iphlpapi.dll", "dword", "GetAdaptersInfo", "ptr", $pBinary, "ulong*", $iResult

[2])

$sBuffer = "ptr;dword;char[260];char[132];uint;byte[8];dword;uint[2];ptr;ptr;char[16];char[16];dword"
While $pBinary <> 0
        $tBuffer = DllStructCreate($sBuffer, $pBinary)
        $pBinary = DllStructGetData($tBuffer, 1)

        If StringInStr($sIfIndices, "," & DllStructGetData($tBuffer, 7) & ",") Then
                Msgbox(0, DllStructGetData($tBuffer, 11), DllStructGetData($tBuffer, 4))
        EndIf
WEnd

评分

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

查看全部评分

 楼主| 发表于 2010-1-24 18:22:05 | 显示全部楼层
回复 4# pusofalse


    谢谢P版。
但这个没有达到目的。
顶楼的WMI在拨号的机子上,可以得到其拨号连接的IP。
而你这个得到的却是本地连接的IP
这个截图是我在ADSL上截的。

本帖子中包含更多资源

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

×
发表于 2010-1-24 19:57:32 | 显示全部楼层
根本的原因是在枚举连接时,没有发现拨号的连接。这一句就是判断是否是拨号连接的。
If DllStructGetData($tBuffer, 2, 2) = 23 Then ... 23对应的连接类型是IF_TYPE_PPP,A PPP network interface. MSDN中是这样解释的。
一台机子中,有ADSL连接,但没有拨号到网络,同样可看做这是一个非活动的连接。当然这是一句多余的话。
 楼主| 发表于 2010-1-24 23:25:26 | 显示全部楼层
回复 6# pusofalse

谢谢,这下成功了。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-28 02:12 , Processed in 0.162343 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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