Что нового

[Сеть, интернет] Инициализация интернет-подключения через RasDial (API rasapi32.dll)

erlik

Продвинутый
Сообщения
317
Репутация
84
Хотел сделать обертку для системной функции инициалиазции интернет подключения RasDial msdn(rasdial.exe является ее консольной реализацией). Но пока глухо. Как и вообще с rasapi32.dll - никто пока, увы, не сделал UDF под нее, а ведь полезная библиотека.
Если кому нужно могу выложить обертку для RasEnumConnections (возвращает инфу об активном подключении) и еще случайно нашел готовую обертку для RasGetConnectionStatistics - выдает статистику по интернет-трафику.

Вот мой образец RasDial который не работает :smile:
Код:
#include <array.au3>
#include <WinAPI.au3>

Local $Res=_RasDial('имя интернет подключения','логин','пароль')
ConsoleWrite($Res &'|error=' & @error)

Func _RasDial($sConnectName,$sUserName,$sPassword,$sDomain='',$sPhoneNumber='',$sCallbackNumber='',$vRasDialExtensions=0,$sPhonebook='',$vNotifierType='',$hNotifier=0)
	Local Const $MAX_PATH              = 260
	Local Const $RAS_MaxEntryName      = 256
	Local Const $RAS_MaxPhoneNumber    = 128
	Local Const $RAS_MaxCallbackNumber = 128
	Local Const $UNLEN                 = 128
	Local Const $PWLEN                 = 128
	Local Const $DNLEN                 = 128
	Local Const $RAS_MaxDeviceType     = 16
    Local Const $RAS_MaxDeviceName     = 128

	Local $tRASDIALPARAMS=DllStructCreate( _
	'dword dwSize;' & _
    'char szEntryName['      & $RAS_MaxEntryName      + 1 &'];' & _
	'char szPhoneNumber['    & $RAS_MaxPhoneNumber    + 1 &'];' & _
	'char szCallbackNumber[' & $RAS_MaxCallbackNumber + 1 &'];' & _
	'char szUserName['       & $UNLEN                 + 1 &'];' & _
	'char szPassword['       & $PWLEN                 + 1 &'];' & _
	'char szDomain['         & $DNLEN                 + 1 &'];' & _
	'dword dwSubEntry;' & _
	'ulong_ptr dwCallbackId;' & _
    'dword dwIfIndex')

   DllStructSetData($tRASDIALPARAMS,'dwSize',          DllStructGetSize($tRASDIALPARAMS))
   DllStructSetData($tRASDIALPARAMS,'szEntryName'     ,$sConnectName)
   DllStructSetData($tRASDIALPARAMS,'szPhoneNumber'   ,$sPhoneNumber)
   DllStructSetData($tRASDIALPARAMS,'szCallbackNumber',$sCallbackNumber)
   DllStructSetData($tRASDIALPARAMS,'szUserName',      $sUserName)
   DllStructSetData($tRASDIALPARAMS,'szPassword',      $sPassword)
   DllStructSetData($tRASDIALPARAMS,'szDomain',        $sDomain)
   DllStructSetData($tRASDIALPARAMS,'dwSubEntry',      0)
   DllStructSetData($tRASDIALPARAMS,'dwCallbackId',    0)
   DllStructSetData($tRASDIALPARAMS,'dwIfIndex',       0)

    Local $tRASCONN = DllStructCreate( _
	'dword dwSize;hwnd hRasConn;' & _
	'char szEntryName[' & $RAS_MaxEntryName  +1 &'];' & _
	'char szDeviceType['& $RAS_MaxDeviceType +1 &'];' & _
	'char szDeviceName['& $RAS_MaxDeviceName +1 &'];' & _
	'char szPhonebook[' & $MAX_PATH &'];' & _
	'dword dwSubEntry;byte guidEntry[16];dword dwFlags;byte luid[8]')

	Local $pRASDIALPARAMS = DllStructGetPtr($tRASDIALPARAMS)
	Local $pRASCONN       = DllStructGetPtr($tRASCONN)

	Local $aRet = DllCall('rasapi32.dll', 'int', 'RasDialW', 'ptr',$vRasDialExtensions,'str',$sPhonebook,'ptr',$pRASDIALPARAMS,'int',$vNotifierType,'hwnd', $hNotifier,'ptr',$pRASCONN)

	;проверяем корректность заполнения структуры
	;ConsoleWrite(DllStructGetData($tRASDIALPARAMS,1) & @CRLF)
	;ConsoleWrite(DllStructGetData($tRASDIALPARAMS,2)& @CRLF)
	;ConsoleWrite(DllStructGetData($tRASDIALPARAMS,5)& @CRLF)
	;ConsoleWrite(DllStructGetData($tRASDIALPARAMS,6)& @CRLF)

	Local $hRasConn =  DllStructGetData($tRASCONN,'hRasConn')
	;вернем описатель подключения
	Return SetError(_WinAPI_GetLastError(), 0, $hRasConn)
EndFunc
Может кто разберется, что там не так - внятной ошибку не выдает, но и ничего не делает.
 

AZJIO

Меценат
Меценат
Сообщения
2,814
Репутация
1,170
Перевёл GUI вышеуказанной утилиты на русский.

Код:
; WinPEng
; http://www.autoit.de/index.php?page=Thread&threadID=26567
#include <Date.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global Const $RAS_MaxEntryName = 256
Global Const $RAS_MaxPhoneNumber = 128
Global Const $RAS_MaxCallbackNumber = 128
Global Const $UNLEN = 256
Global Const $PWLEN = 256
Global Const $DNLEN = 12
Global Const $RAS_MaxDeviceType = 16
Global Const $RAS_MaxDeviceName = 128
Global Const $MAX_PATH = 260
Global $RasActiveEntryName, $hRasConn, $RASAPIDLL, $RasEntrieNames, $RasDefaultEntrie
; Global $RasUserName = "[email protected]"
Global $RasUserName = ""
; Global $RasPassword = "1M1111"
Global $RasPassword = ""
Global $RASGUIEntries, $i
Global $NoRAS = False
Global $RASConState = "ОФЛАЙН", $RASConStateOld = $RASConState
Global $BytesSend, $BytesRec
Global $STTime, $DITime
#region ### START Koda GUI section ### Form=v:\ras\___neu\formrascon.kxf
$FormRASCon = GUICreate("RAS Connection", 413, 274, -1, -1)
$LRasCon = GUICtrlCreateLabel("Учётка:", 16, 24, 65, 17)
$LRasStat = GUICtrlCreateLabel("Состояние: ", 16, 56, 65, 17, $SS_LEFTNOWORDWRAP)
$LRasStatValue = GUICtrlCreateLabel($RASConState, 116, 56, 65, 17)
GUICtrlSetColor($LRasStatValue, 0xff0000)
GUICtrlSetFont(-1, -1, 700)
$LRasSend = GUICtrlCreateLabel("Отправлено: ", 232, 24, 83, 17)
$LRasSendValue = GUICtrlCreateLabel("", 322, 24, 70, 17, $SS_RIGHT)
$LRasRec = GUICtrlCreateLabel("Получено: ", 232, 56, 92, 17)
$LRasRecValue = GUICtrlCreateLabel("", 322, 56, 70, 17, $SS_RIGHT)
$CRASCon = GUICtrlCreateCombo("", 88, 22, 129, 25)
$ELog = GUICtrlCreateEdit("", 16, 96, 377, 123)
; $BCancel = GUICtrlCreateButton("Закрыть", 320, 234, 75, 25, $WS_GROUP)
$BConnect = GUICtrlCreateButton("Подключение", 220, 234, 75, 25, $WS_GROUP)
$BReConnect = GUICtrlCreateButton("Новый набор", 120, 234, 75, 25, $WS_GROUP)
GUICtrlSetState($BConnect, $GUI_DISABLE)
GUICtrlSetState($BReConnect, $GUI_DISABLE)
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
WritelnLog("Исследование учёток RAS...")
_RasStart()
For $i = 0 To UBound($RasEntrieNames) - 1
	$RASGUIEntries = $RASGUIEntries & $RasEntrieNames[$i] & "|"
	WritelnLog($RasEntrieNames[$i] & " найден...")
Next
GUICtrlSetData($CRASCon, $RASGUIEntries, $RasDefaultEntrie)
$STTime = TimerInit()
While 1
	$DITime = TimerDiff($STTime)
	If $DITime > 100 Then
		UpdateValues()
		$STTime = TimerInit()
	EndIf
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE;, $BCancel
			_RasEnd()
			Exit
		Case $BConnect
			If _RasIsActive() Then
				_RasHangUp($hRasConn)
				UpdateValues()
			Else
				_RasDial($RasDefaultEntrie, $RasUserName, $RasPassword)
				UpdateValues()
			EndIf
		Case $BReConnect
			If _RasIsActive() Then
				_RasHangUp($hRasConn)
				UpdateValues()
				_RasDial($RasDefaultEntrie, $RasUserName, $RasPassword)
				UpdateValues()
			Else
				_RasDial($RasDefaultEntrie, $RasUserName, $RasPassword)
				UpdateValues()
			EndIf
	EndSwitch
WEnd

Func UpdateValues()
	If Not $NoRAS Then
		If GUICtrlGetState($BReConnect) And $GUI_DISABLE = $GUI_DISABLE Then GUICtrlSetState($BReConnect, $GUI_ENABLE)
		If Not _RasIsActive() Then
			If $RASConState <> "ОФЛАЙН" Then
				$RASConState = "ОФЛАЙН"
				GUICtrlSetData($BConnect, "Соединение")
				If GUICtrlGetState($BConnect) And $GUI_DISABLE = $GUI_DISABLE Then GUICtrlSetState($BConnect, $GUI_ENABLE)
			EndIf
			If $RASConState <> $RASConStateOld Then
				GUICtrlSetData($LRasStatValue, $RASConState)
				GUICtrlSetColor($LRasStatValue, 0xff0000)
				WritelnLog($RasDefaultEntrie & " отключен...")
				$RASConStateOld = $RASConState
			EndIf
		Else
			If $RASConState <> "ОНЛАЙН" Then
				$RASConState = "ОНЛАЙН"
				GUICtrlSetData($BConnect, "Отключить")
				If GUICtrlGetState($BConnect) And $GUI_DISABLE = $GUI_DISABLE Then GUICtrlSetState($BConnect, $GUI_ENABLE)
			EndIf
			If $RASConState <> $RASConStateOld Then
				GUICtrlSetData($LRasStatValue, $RASConState)
				GUICtrlSetColor($LRasStatValue, 0x00cc00)
				WritelnLog($RasDefaultEntrie & " подключен...")
				$RASConStateOld = $RASConState
			EndIf
			$BytesSend = _Filesize(_RASConStat(1), "", 2)
			$BytesRec = _Filesize(_RASConStat(2), "", 2)
			GUICtrlSetData($LRasSendValue, $BytesSend)
			GUICtrlSetData($LRasRecValue, $BytesRec)
		EndIf
	EndIf
EndFunc   ;==>UpdateValues

Func WritelnLog($sText, $sFlag = 1)
	If $sFlag = 1 Then $sText = $sText & @CRLF
	$sText = _Now() & " - " & $sText
	_GUICtrlEdit_AppendText($ELog, $sText)
EndFunc   ;==>WritelnLog

Func _RasStart()
	$RASAPIDLL = DllOpen("rasapi32.dll")
	$RasEntrieNames = _RasEnumEntries()
	If @error Then
		MsgBox(16, "Ошибка", "Нет ни одного установленного подключения RAS." & @CRLF & _
				"Программа завершается.")
		_RasEnd()
		Exit
	EndIf
	$RasDefaultEntrie = $RasEntrieNames[0]
EndFunc   ;==>_RasStart

Func _RasEnd()
	DllClose($RASAPIDLL)
EndFunc   ;==>_RasEnd

Func _RasIsActive()
	$hRasConn = _RasEnumConnections()
	If @error = 1 Then Return SetError(1, @extended, -1)
	If @error = 2 Then
		Return False
	Else
		Return True
	EndIf
EndFunc   ;==>_RasIsActive

; #FUNCTION# =========================================================================================================
; $sFlags:
; 1 - The number of bytes transmitted through this connection or link.
; 2 - The number of bytes received through this connection or link.
; 3 - Total
; 4 - The amount of time, in milliseconds, that the connection or link has been connected.
; ====================================================================================================================
Func _RASConStat($sFlags = 1)
	If Not _RasIsActive() Then Return SetError(1, 0, 0)
	$tRAS_STATS = DllStructCreate("dword dwSize;dword dwBytesXmited;dword dwBytesRcved;dword dwFramesXmited;" & _
			"dword dwFramesRcved;dword dwCrcErr;dword dwTimeoutErr;dword dwAlignmentErr;" & _
			"dword dwHardwareOverrunErr;dword dwFramingErr;dword dwBufferOverrunErr;" & _
			"dword dwCompressionRatioIn;dword dwCompressionRatioOut;dword dwBps;dword dwConnectDuration")
	DllStructSetData($tRAS_STATS, "dwSize", DllStructGetSize($tRAS_STATS))
	$aRet = DllCall($RASAPIDLL, "int", "RasGetConnectionStatistics", _
			"hwnd", $hRasConn, _
			"ptr", DllStructGetPtr($tRAS_STATS))
	If $aRet[0] Then Return SetError(2, 0, 0)
	Local $iResult = 0
	Switch $sFlags
		Case 1
			$iResult = DllStructGetData($tRAS_STATS, "dwBytesXmited")
		Case 2
			$iResult = DllStructGetData($tRAS_STATS, "dwBytesRcved")
		Case 3
			$iResult = DllStructGetData($tRAS_STATS, "dwBytesXmited") + DllStructGetData($tRAS_STATS, "dwBytesRcved")
		Case 4
			$iResult = DllStructGetData($tRAS_STATS, "dwConnectDuration")
	EndSwitch
	Return $iResult
EndFunc   ;==>_RASConStat

Func _RasDial($szEntryName, $szUserName, $szPassword)
	Local $RASDIALPARAMS = DllStructCreate("dword dwSize;char szEntryName[" & $RAS_MaxEntryName + 1 & "];" & _
			"char szPhoneNumber[" & $RAS_MaxPhoneNumber + 1 & "];" & _
			"char szCallbackNumber[" & $RAS_MaxCallbackNumber + 1 & "];" & _
			"char szUserName[" & $UNLEN + 1 & "];" & _
			"char szPassword[" & $PWLEN + 1 & "];" & _
			"char szDomain[" & $DNLEN + 1 & "];dword dwSubEntry;ulong_ptr dwCallbackId;dword dwIfIndex")
	DllStructSetData($RASDIALPARAMS, "dwSize", DllStructGetSize($RASDIALPARAMS))
	DllStructSetData($RASDIALPARAMS, "szEntryName", $szEntryName)
	DllStructSetData($RASDIALPARAMS, "szUserName", $szUserName)
	DllStructSetData($RASDIALPARAMS, "szPassword", $szPassword)
	Local $RasConn = DllStructCreate("dword hRasConn")
	DllStructSetData($RasConn, "hRasConn", 0)
	Local $aRet = DllCall($RASAPIDLL, "int", "RasDial", _
			"ptr", 0, "ptr", 0, _
			"ptr", DllStructGetPtr($RASDIALPARAMS), _
			"dword", 0, "hwnd", 0, _
			"ptr", DllStructGetPtr($RasConn))
	If $aRet[0] Then Return SetError(1, $aRet[0], -1)
	$hRasConn = DllStructGetData($RasConn, "hRasConn")
	$RasActiveEntryName = $szEntryName
	Return $hRasConn
EndFunc   ;==>_RasDial

Func _RasHangUp($hRasConn)
	$aRet = DllCall($RASAPIDLL, "int", "RasHangUp", "hwnd", $hRasConn)
	Return $aRet[0]
EndFunc   ;==>_RasHangUp

Func _RasEnumConnections()
	Local $iCntByte = DllStructCreate("dword")
	Local $iCntConn = DllStructCreate("dword")
	Local $RasConn = DllStructCreate("dword dwSize;hwnd hRasConn;char szEntryName[" & $RAS_MaxEntryName + 1 & "];" & _
			"char szDeviceType[" & $RAS_MaxDeviceType + 1 & "];" & _
			"char szDeviceName[" & $RAS_MaxDeviceName + 1 & "];" & _
			"char szPhonebook[" & $MAX_PATH & "];" & _
			"dword dwSubEntry;byte guidEntry[16];dword dwFlags;byte luid[8]")
	DllStructSetData($RasConn, "dwSize", DllStructGetSize($RasConn))
	DllStructSetData($iCntByte, 1, DllStructGetSize($RasConn))
	Local $aRet = DllCall($RASAPIDLL, "int", "RasEnumConnections", _
			"ptr", DllStructGetPtr($RasConn), _
			"ptr", DllStructGetPtr($iCntByte), _
			"ptr", DllStructGetPtr($iCntConn))
	If $aRet[0] Then Return SetError(1, $aRet[0], -1)
	If DllStructGetData($iCntConn, 1) < 1 Then
		$RasActiveEntryName = ""
		Return SetError(2, 0, 0) ;Error: not opened connections
	EndIf
	$RasActiveEntryName = DllStructGetData($RasConn, "szEntryName")
	Return DllStructGetData($RasConn, "hRasConn")
EndFunc   ;==>_RasEnumConnections

Func _RasEnumEntries()
	Local Const $SUCCESS = 0
	Local Const $ERROR_NOT_ENOUGH_MEMORY = 8
	Local Const $RASBASE = 600
	Local Const $ERROR_BUFFER_TOO_SMALL = $RASBASE + 3
	Local Const $ERROR_INVALID_SIZE = $RASBASE + 32
	Dim $result[1]
	Local $i
	Local $RAS_MaxEntryName = 0x104 ;don't no why it is not 0x100....
	Local $RasEntryName = "int dwSize;char szEntryName[" & $RAS_MaxEntryName & "]" ;RASENTRYNAME structure data
	Local $TRasEntryName = DllStructCreate($RasEntryName) ;temporary Structure for sizing
	Local $RAS_EntryName_Size = DllStructGetSize($TRasEntryName) ;Structure Size
	Local $mem = _MemGlobalAlloc(256 * $RAS_EntryName_Size, $GPTR) ;Allocate Memory for Array
	Dim $ARasEntryName[256] ;Create an Array [256] for Param 3
	For $i = 0 To 255
		$ARasEntryName[$i] = DllStructCreate($RasEntryName, $mem + ($i * $RAS_EntryName_Size)) ;Create 256 Structures in the Array
	Next
	Local $lpcb = 256 * $RAS_EntryName_Size ;calc BufferSize (Param4)
	Local $lpcEntries = 0 ;Init Param5
	DllStructSetData($ARasEntryName[0], 1, $RAS_EntryName_Size) ;write StructureSize to first member of the array
	Local $res = DllCall($RASAPIDLL, "int", "RasEnumEntries", "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($ARasEntryName[0]), "int*", $lpcb, "int*", $lpcEntries)
	If $res[0] = 0 Then ;Return Value
		Local $noEntries = $res[5] ;number of Entries
		If $noEntries > 0 Then
			For $i = 0 To $noEntries - 1
				$result[$i] = DllStructGetData($ARasEntryName[$i], "szEntryName") ;copy result to result-Array
				ReDim $result[UBound($result) + 1] ;resize result-Array
			Next
			ReDim $result[UBound($result) - 1] ;delete last (empty) entry
			Return $result
		Else
			SetError(1, 0, 0)
		EndIf
	Else
		SetError(1, 0, 0)
	EndIf
EndFunc   ;==>_RasEnumEntries

;===============================================================================
; Author(s): busysignal
; Version: 1.0.0
; AutoItVer: 3.1.1.+
; Created 2005-10-15
;
; Description: Formats a files size according to $sFormat & $iDecimal parameters
; Syntax: _Filesize($iValue, $sFormat, $iNum)
;
; Parameter(s): $iValue = The value in "Bytes"
; $SFormat = Specify the Size format you wish to display, options are:
; "" - Auto Select based on value entered
; "GB" - Display in Gigabytes
; "MB" - Display in Megabytes
; "KB" - Display in Kilobytes
; "B" - Display in Bytes
; $iDecimal = The number of decimals places to round up converted value to
; Requirement(s): None
; Return Value(s): On Success - Returns a string representation of $iValue formatted according to $sFormat
; On Failure - Returns an empty string "" if no files are found and sets @Error on errors
; @Error=1 $iValue = 0; no value to convert
; @Error=2 Either $iValue or $iDecimal contains non-numeric characters.
; Note(s): Example: _Filesize(2152971234,"GB", 2) would return "2.15 GB"
;
; Credits: Inspired by _PictureUDF() by SolidSnake <[email protected]>
;===============================================================================
Func _Filesize($iValue, $sFormat, $iDecimal)
	Local $sReturn, $iB, $iKB, $iMB, $iGB
	If $iValue = 0 Then
		SetError(1)
		Return 0
	EndIf
	If Not StringIsDigit($iValue) Or Not StringIsDigit($iDecimal) Then
		SetError(2)
		Return 0
	EndIf
	; Conversion Chart
	$iB = $iValue
	$iKB = Round($iB / 1024, $iDecimal)
	$iMB = Round($iKB / 1024, $iDecimal)
	$iGB = Round($iMB / 1024, $iDecimal)
	Select
		Case $sFormat = ""; Auto Select Format Display Type
			If $iMB > 1024 Then
				$iValue = $iGB & " GB"
			ElseIf $iKB > 1024 Then
				$iValue = $iMB & " MB"
			ElseIf $iKB > 0 Then
				$iValue = $iKB & " KB"
			ElseIf $iKB < 0 Then
				$iValue = $iB & " Bytes"
			EndIf
		Case $sFormat = "GB"
			$iValue = $iGB & " GB"
		Case $sFormat = "MB"
			$iValue = $iMB & " MB"
		Case $sFormat = "KB"
			$iValue = $iKB & " KB"
		Case $sFormat = "B"
			$iValue = $iB & " Bytes"
	EndSelect
	Return $iValue
EndFunc   ;==>_Filesize
 

madmasles

Модератор
Глобальный модератор
Сообщения
7,790
Репутация
2,321
В функции _RasEnumEntries() память не надо освобождать?
 

AZJIO

Меценат
Меценат
Сообщения
2,814
Репутация
1,170
Посмотрел в Include файл Clipboard.au3 содержит вызовы _MemGlobalAlloc, но не содержит _MemGlobalFree.
 
Верх