如何获取VPN的连接名、ip?
搜索了一下论坛,找到以下代码:但在xp下正常,win7 64位下没效果。各位大侠还有啥办法没?#include <rasapi.au3>$aEntry = _RasEnumEntries()
$aConn = _RasEnumConnections()
If Not $aEntry = "" Then
If Not $aConn = "" Then
For $i = 1 To $aConn
If $aConn[$i] = "VPN" Then
_ArrayDisplay($aConn)
$aSen = _RasGetConnectionIpAddresses($aConn[$i])
_ArrayDisplay($aSen)
MsgBox(0, "Gateway:" & $aSen, "IP:" & $aSen)
EndIf
Next
EndIf
EndIf
自己顶下
xp下面用wmi也能获得VPN的ip 在win7 64位下也不行。 替换其中的_RasEnumConnections函数:
Func _RasEnumConnections()
Local $aResult, $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 * $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, "dword*", 0)
If ($iResult) Then
_RasFreeVar($tBuffer, 0, _RasFreeHeap($pBuffer))
Return SetError($iResult, 0, $aResult)
EndIf
$aResult = $iResult
Redim $aResult[$iResult + 1]
For $i = 1 To $iResult
$aResult[$i] = DllStructGetData($tBuffer, "RasConnection")
$aResult[$i] = DllStructGetData($tBuffer, "EntryName")
$aResult[$i] = DllStructGetData($tBuffer, "DeviceType")
$aResult[$i] = DllStructGetData($tBuffer, "DeviceName")
$aResult[$i] = DllStructGetData($tBuffer, "Pnebook")
$aResult[$i] = DllStructGetData($tBuffer, "SubEntry")
$aResult[$i] = 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;wchar DeviceType;wchar DeviceName;wchar Pnebook;dword SubEntry;byte GuidEntry;dword Flags;dword Luid" ; ubyte GuidCorrelationId"
Const $tagRAS_CONNECTION_X64 = "dword Size;hWnd RasConnection;wchar EntryName;wchar DeviceType;wchar DeviceName;wchar Pnebook;dword SubEntry;byte GuidEntry;dword Flags;dword Luid;ubyte GuidCorrelationId" 老大出面了啊。。。
谢谢,明天去单位电脑上试下! 本帖最后由 茫然 于 2013-4-24 09:57 编辑
哈哈哈~ win7 x64下正常了!
谢谢,老大!
下面测试代码:#include <rasapi.au3>
$aEntry = _RasEnumEntries()
_ArrayDisplay($aEntry)
$aConn = _RasEnumConnections()
If Not $aConn = "" Then
For $i = 1 To $aConn
If $aConn[$i] = "VPN" Then
$aSen = _RasGetConnectionIpAddresses($aConn[$i])
MsgBox(0,"名称:",$aConn[$i])
MsgBox(0, "Gateway:" & $aSen, "IP:" & $aSen)
EndIf
Next
EndIf
报告:_RasEnumEntries()在win7 x64下无显示 回复 5# 茫然
_RasEnumEntries也有异常吗?看下系统中有无这个路径 -
@AppDataCommonDir & "\Microsoft\Network\Connections\Pbk\rasphone.pbk" 回复 6# pusofalse
没找个文件
@AppDataCommonDir & "\Microsoft\Network\Connections\ 到这就没了。
看了2台 win7 x64 都这样 回复 7# 茫然
这样呢?
Func _RasEnumEntries()
Local Const $tagRASENTRYNAME = "dword dwSize;wchar szEntryName;dword dwFlags;wchar szPhonebookPath"
Local $tRasEntryName = DllStructCreate($tagRASENTRYNAME)
Local $pRasEntryName = DllStructGetPtr($tRasEntryName)
Local $iRasEntryName = DllStructGetSize($tRasEntryName)
DllStructSetData($tRasEntryName, "dwSize", $iRasEntryName)
Local $iResult, $aList = []
$iResult = DllCall($RAS_DllHandle, "long", "RasEnumEntriesW", "ptr", 0, "ptr", 0, "ptr", $pRasEntryName, "long*", 0, "long*", 0)
If $iResult = 0 Or $iResult = 0 Then
Return SetError($iResult, 0, $aList)
EndIf
Local $tBuffer = DllStructCreate("ubyte Data[" & $iResult & "]")
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, "long*", 0)
If $iResult Then
Return SetError($iResult, 0, $aList)
EndIf
$aList = $iResult
ReDim $aList[$aList + 1]
For $i = 1 To $aList
$tRasEntryName = DllStructCreate($tagRASENTRYNAME, $pBuffer + ($i - 1) * $iRasEntryName)
$aList[$i] = DllStructGetData($tRasEntryName, "szEntryName")
$aList[$i] = DllStructGetData($tRasEntryName, "dwFlags")
$aList[$i] = DllStructGetData($tRasEntryName, "szPhonebookPath")
Next
Return $aList
EndFunc ;==>_RasEnumEntries 回复 8# pusofalse
貌似不行啊!
返回值不是个数组。
#include <rasapi.au3>
#include <Array.au3>
$aEntry = _RasEnumEntries()
MsgBox(0,"",IsArray($aEntry)) ;显示0
_ArrayDisplay($aEntry) 回复 9# 茫然
不可能。。。如果真显示为0,那就是撞鬼了,我不知怎么破解。 还以为64位的不一样呢 本帖最后由 茫然 于 2013-4-27 16:14 编辑
回复 10# pusofalse
回复 12# 茫然
抱歉,是我粗心大意了,8#的代码已修改,你再次用以下代码测试一下,看下@error的值是多少。
#include <rasapi.au3>
Local $aList = _RasEnumEntries()
MsgBox(0, @error, IsArray($aList)) 回复 13# pusofalse
老大辛苦了,谢谢!
不过win7 X64下数组还是空的,xp正常
页:
[1]