找回密码
 加入
搜索
查看: 8753|回复: 13

[网络通信] 如何获取VPN的连接名、ip?

  [复制链接]
发表于 2013-4-20 14:56:41 | 显示全部楼层 |阅读模式
搜索了一下论坛,找到以下代码:但在xp下正常,win7 64位下没效果。各位大侠还有啥办法没?
#include <rasapi.au3>
$aEntry = _RasEnumEntries()
$aConn = _RasEnumConnections()
If Not $aEntry[0][0] = "" Then
        If Not $aConn[0][0] = "" Then
                For $i = 1 To $aConn[0][0]
                        If $aConn[$i][2] = "VPN" Then
                                _ArrayDisplay($aConn)
                                $aSen = _RasGetConnectionIpAddresses($aConn[$i][0])
                                _ArrayDisplay($aSen)
                                MsgBox(0, "Gateway:" & $aSen[1], "IP:" & $aSen[0])
                        EndIf
                Next
        EndIf
EndIf
 楼主| 发表于 2013-4-23 11:25:42 | 显示全部楼层
自己顶下
xp下面用wmi也能获得VPN的ip 在win7 64位下也不行。
发表于 2013-4-23 15:34:29 | 显示全部楼层
替换其中的_RasEnumConnections函数:
Func _RasEnumConnections()
        Local $aResult[1][7], $tBuffer, $pBuffer, $iBufferSize, $iResult

        If @AutoItX64 Then
                $tBuffer = DllStructCreate($tagRAS_CONNECTION_X64)
        Else
                $tBuffer = DllStructCreate($tagRAS_CONNECTION_X86)
        EndIf
        $pBuffer = DllStructGetPtr($tBuffer)
        $iBufferSize = DllStructGetSize($tBuffer)
        DllStructSetData($tBuffer, "Size", $iBufferSize)

        $iResult = DllCall($RAS_DllHandle, "dword", "RasEnumConnectionsW", "ptr", $pBuffer, _
                        "dword*", $iBufferSize, "dword*", 0)
        
        $tBuffer = 0
        $pBuffer = _RasAllocHeap($iResult[3] * $iBufferSize)

        If @AutoItX64 Then
                $tBuffer = DllStructCreate($tagRAS_CONNECTION_X64, $pBuffer)
        Else
                $tBuffer = DllStructCreate($tagRAS_CONNECTION_X86, $pBuffer)
        EndIf

        DllStructSetData($tBuffer, "Size", $iBufferSize)

        $iResult = DllCall($RAS_DllHandle, "dword", "RasEnumConnectionsW", "ptr", $pBuffer, _
                        "dword*", $iBufferSize * $iResult[3], "dword*", 0)
        If ($iResult[0]) Then
                _RasFreeVar($tBuffer, 0, _RasFreeHeap($pBuffer))
                Return SetError($iResult[0], 0, $aResult)
        EndIf
        $aResult[0][0] = $iResult[3]
        Redim $aResult[$iResult[3] + 1][7]

        For $i = 1 To $iResult[3]
                $aResult[$i][0] = DllStructGetData($tBuffer, "RasConnection")
                $aResult[$i][1] = DllStructGetData($tBuffer, "EntryName")
                $aResult[$i][2] = DllStructGetData($tBuffer, "DeviceType")
                $aResult[$i][3] = DllStructGetData($tBuffer, "DeviceName")
                $aResult[$i][4] = DllStructGetData($tBuffer, "Pnebook")
                $aResult[$i][5] = DllStructGetData($tBuffer, "SubEntry")
                $aResult[$i][6] = DllStructGetData($tBuffer, "Flags")
                $tBuffer = 0
                If @AutoItX64 Then
                        $tBuffer = DllStructCreate($tagRAS_CONNECTION_X64, $pBuffer + ($i * $iBufferSize))
                Else
                        $tBuffer = DllStructCreate($tagRAS_CONNECTION_X86, $pBuffer + ($i * $iBufferSize))
                EndIf
        Next
        _RasFreeVar($tBuffer, 0, _RasFreeHeap($pBuffer))
        Return $aResult
EndFunc        ;==>_RasEnumConnections


函数用到的两个数据结构如下 ——
Const $tagRAS_CONNECTION_X86 = "dword Size;hWnd RasConnection;wchar EntryName[257];wchar DeviceType[17];wchar DeviceName[129];wchar Pnebook[260];dword SubEntry;byte GuidEntry[16];dword Flags;dword Luid[2]" ; ubyte GuidCorrelationId[16]"

Const $tagRAS_CONNECTION_X64 = "dword Size;hWnd RasConnection;wchar EntryName[257];wchar DeviceType[17];wchar DeviceName[129];wchar Pnebook[260];dword SubEntry;byte GuidEntry[16];dword Flags;dword Luid[2];ubyte GuidCorrelationId[16]"
 楼主| 发表于 2013-4-23 22:19:21 | 显示全部楼层
老大出面了啊。。。
谢谢,明天去单位电脑上试下!
 楼主| 发表于 2013-4-24 09:24:08 | 显示全部楼层
本帖最后由 茫然 于 2013-4-24 09:57 编辑

哈哈哈~ win7 x64下正常了!
谢谢,老大!

下面测试代码:
#include <rasapi.au3>
$aEntry = _RasEnumEntries()
_ArrayDisplay($aEntry)

$aConn = _RasEnumConnections()
If Not $aConn[0][0] = "" Then
        For $i = 1 To $aConn[0][0]
                If $aConn[$i][2] = "VPN" Then
                        $aSen = _RasGetConnectionIpAddresses($aConn[$i][0])
                        MsgBox(0,"名称:",$aConn[$i][1])
                        MsgBox(0, "Gateway:" & $aSen[1], "IP:" & $aSen[0])
                EndIf
        Next
EndIf
报告:_RasEnumEntries()  在win7 x64下无显示
发表于 2013-4-25 05:57:04 | 显示全部楼层
回复 5# 茫然


    _RasEnumEntries也有异常吗?看下系统中有无这个路径 -
@AppDataCommonDir & "\Microsoft\Network\Connections\Pbk\rasphone.pbk"
 楼主| 发表于 2013-4-25 21:08:19 | 显示全部楼层
回复 6# pusofalse

没找个文件
@AppDataCommonDir & "\Microsoft\Network\Connections\ 到这就没了。
看了2台 win7 x64 都这样
发表于 2013-4-25 22:33:51 | 显示全部楼层
回复 7# 茫然


    这样呢?

Func _RasEnumEntries()
        Local Const $tagRASENTRYNAME = "dword dwSize;wchar szEntryName[257];dword dwFlags;wchar szPhonebookPath[261]"

        Local $tRasEntryName = DllStructCreate($tagRASENTRYNAME)
        Local $pRasEntryName = DllStructGetPtr($tRasEntryName)
        Local $iRasEntryName = DllStructGetSize($tRasEntryName)

        DllStructSetData($tRasEntryName, "dwSize", $iRasEntryName)

        Local $iResult, $aList[1][3] = [[0]]

        $iResult = DllCall($RAS_DllHandle, "long", "RasEnumEntriesW", "ptr", 0, "ptr", 0, "ptr", $pRasEntryName, "long*", 0, "long*", 0)

        If $iResult[4] = 0 Or $iResult[5] = 0 Then
                Return SetError($iResult[0], 0, $aList)
        EndIf

        Local $tBuffer = DllStructCreate("ubyte Data[" & $iResult[4] & "]")
        Local $pBuffer = DllStructGetPtr($tBuffer)

        $tRasEntryName = DllStructCreate($tagRASENTRYNAME, $pBuffer)
        DllStructSetData($tRasEntryName, "dwSize", $iRasEntryName)

        $iResult = DllCall($RAS_DllHandle, "long", "RasEnumEntriesW", "ptr", 0, "ptr", 0, "ptr", $pBuffer, "long*", $iResult[4], "long*", 0)

        If $iResult[0] Then
                Return SetError($iResult[0], 0, $aList)
        EndIf

        $aList[0][0] = $iResult[5]
        ReDim $aList[$aList[0][0] + 1][3]

        For $i = 1 To $aList[0][0]
                $tRasEntryName = DllStructCreate($tagRASENTRYNAME, $pBuffer + ($i - 1) * $iRasEntryName)

                $aList[$i][0] = DllStructGetData($tRasEntryName, "szEntryName")
                $aList[$i][1] = DllStructGetData($tRasEntryName, "dwFlags")
                $aList[$i][2] = DllStructGetData($tRasEntryName, "szPhonebookPath")
        Next
        
        Return $aList
EndFunc        ;==>_RasEnumEntries
 楼主| 发表于 2013-4-26 09:48:38 | 显示全部楼层
回复 8# pusofalse

貌似不行啊!
返回值不是个数组。
#include <rasapi.au3>
#include <Array.au3>
$aEntry = _RasEnumEntries()
MsgBox(0,"",IsArray($aEntry)) ;显示0
_ArrayDisplay($aEntry)
发表于 2013-4-26 16:34:03 | 显示全部楼层
回复 9# 茫然


    不可能。。。如果真显示为0,那就是撞鬼了,我不知怎么破解。
发表于 2013-4-27 11:14:12 | 显示全部楼层
还以为64位的不一样呢
 楼主| 发表于 2013-4-27 16:12:09 | 显示全部楼层
本帖最后由 茫然 于 2013-4-27 16:14 编辑

回复 10# pusofalse

本帖子中包含更多资源

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

×
发表于 2013-4-27 18:41:13 | 显示全部楼层
回复 12# 茫然

    抱歉,是我粗心大意了,8#的代码已修改,你再次用以下代码测试一下,看下@error的值是多少。

#include <rasapi.au3>
Local $aList = _RasEnumEntries()
MsgBox(0, @error, IsArray($aList))
 楼主| 发表于 2013-4-28 22:37:14 | 显示全部楼层
回复 13# pusofalse

老大辛苦了,谢谢!
不过win7 X64下数组还是空的,xp正常
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-4-28 12:54 , Processed in 0.080113 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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