Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RAS functionalities

26 views
Skip to first unread message

Black Ninja

unread,
May 15, 2003, 11:34:43 PM5/15/03
to
I have wrote a class to wrap a lot of RAS stuff in VB.NET. Here it is.
The deletegate in dialentry would be used as follows in a Windows
Form. I havent yet converted the unMSG to the actual ras text codes.
But that should be easy to do. My style of code writing is sloppy, but
you can tweak the code as you see fit.

'In a new form paste the code below

'The following line needs to have global scope in your form
Dim mcallback as RAS.myrasdialfunc=addressof rasdialfunc

Public Function RasDialFunc(ByVal unMsg As Integer, ByVal rasconnstate
As Integer, ByVal dwError As Integer) As Integer
Debug.WriteLine("DF: " & unMsg.ToString & " - " &
rasconnstate.ToString & " - " & dwError.ToString)
End Function

'In a new class paste

Imports System.Runtime.InteropServices
Public Class ras
Private Const RAS_MaxEntryName As Integer = 256
Private Const RAS_MaxPhoneNumber As Integer = 128
Private Const UNLEN As Integer = 256
Private Const PWLEN As Integer = 256
Private Const DNLEN As Integer = 15
Private Const MAX_PATH As Integer = 260
Private Const RAS_MaxDeviceType As Integer = 16
Private Const RAS_MaxDeviceName As Integer = 128
Private Const RAS_MaxCallbackNumber As Integer =
RAS_MaxPhoneNumber
Private Const ERROR_BUFFER_TOO_SMALL = 603


<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure RASDIALPARAMS
Public dwSize As Integer
<MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=RAS_MaxEntryName + 1)> Public szEntryName As String
<MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=RAS_MaxPhoneNumber + 1)> Public szPhoneNumber As String
<MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=RAS_MaxCallbackNumber + 1)> Public szCallbackNumber As
String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=UNLEN + 1)>
Public szUserName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=PWLEN + 1)>
Public szPassword As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=DNLEN + 1)>
Public szDomain As String
End Structure

Public Delegate Function myrasdialfunc(ByVal unMsg As Integer,
ByVal rasconnstate As Integer, ByVal dwError As Integer) As Integer

Private Declare Auto Function RasDial Lib "rasapi32.dll" (ByVal
lpRasDialExtensions As IntPtr, ByVal lpszPhonebook As String, ByRef
lpRasDialParams As RASDIALPARAMS, ByVal dwNotifierType As Integer,
ByVal lpvNotifier As myrasdialfunc, ByRef lphRasConn As IntPtr) As
Integer
Private Declare Auto Function RasHangUp Lib "rasapi32.dll" (ByVal
hRasCon As IntPtr) As Integer

<StructLayout(LayoutKind.Sequential, Pack:=4,
CharSet:=CharSet.Auto)> _
Public Structure RASCONN
Public dwSize As Integer
Public hRasCon As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=257)> Public
szEntryname As String
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=17)> Public
szDeviceType As String
<MarshalAs(UnmanagedType.ByValTStr, sizeconst:=129)> Public
szDeviceName As String
End Structure

Private Declare Auto Function RasEnumConnections Lib
"rasapi32.dll" (ByVal lpRasCon As IntPtr, ByRef lpcb As Integer, ByRef
lpcConnections As Integer) As Integer

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure RASENTRYNAME
Public dwSize As Integer
<MarshalAs(UnmanagedType.ByValTStr,
SizeConst:=RAS_MaxEntryName + 1)> Public szEntryName As String
Public dwFlags As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH + 1)>
Public szPhonebookPath As String
End Structure

Private Declare Auto Function RasEnumEntries Lib "rasapi32.dll"
(ByVal lpStrNull As String, ByVal lpszPhonebook As String, ByVal
lpRasEntryName As IntPtr, ByRef lpCb As Integer, ByRef lpCEntries As
Integer) As Integer

Public Sub EnumEntries(ByRef mincoming() As RASENTRYNAME)
Dim entries() As RASENTRYNAME
Dim rasentrynamelen As Integer =
Marshal.SizeOf(GetType(RASENTRYNAME))
Dim lpcb As Integer = rasentrynamelen
Dim lpcentries As Integer

Dim parray As IntPtr = Marshal.AllocHGlobal(rasentrynamelen)
Marshal.WriteInt32(parray, rasentrynamelen)

Dim ret As Integer = RasEnumEntries(Nothing, Nothing, parray,
lpcb, lpcentries)

If ret = ERROR_BUFFER_TOO_SMALL Then
parray = Marshal.ReAllocHGlobal(parray, New IntPtr(lpcb))
Marshal.WriteInt32(parray, rasentrynamelen)
ret = RasEnumEntries(Nothing, Nothing, parray, lpcb,
lpcentries)
End If

If ret = 0 And lpcentries > 0 Then
ReDim entries(lpcentries)
Dim pentry As IntPtr = parray
Dim i As Integer
For i = 0 To lpcentries - 1
entries(i) = Marshal.PtrToStructure(pentry,
GetType(RASENTRYNAME))
pentry = New IntPtr(pentry.ToInt32 + rasentrynamelen)
Next
End If

Marshal.FreeHGlobal(parray)

mincoming = entries

End Sub
Public Sub EnumConnections(ByRef mincoming() As RASCONN)


Dim structtype As Type = GetType(RASCONN)
Dim structsize As Integer = Marshal.SizeOf(GetType(RASCONN))
Dim bufsize As Integer = structsize
Dim realcount As Integer
Dim TRasCon() As RASCONN

Dim bufptr As IntPtr = Marshal.AllocHGlobal(bufsize)
Marshal.WriteInt32(bufptr, structsize)

Dim retcode As Integer = RasEnumConnections(bufptr, bufsize,
realcount)

If retcode = ERROR_BUFFER_TOO_SMALL Then
bufptr = Marshal.ReAllocHGlobal(bufptr, New
IntPtr(structsize))
Marshal.WriteInt32(bufptr, structsize)
retcode = RasEnumConnections(bufptr, bufsize, realcount)
End If

If (retcode = 0) And (realcount > 0) Then
ReDim TRasCon(realcount - 1)
Dim i As Integer
Dim runptr As IntPtr = bufptr

For i = 0 To (realcount - 1)
TRasCon(i) = Marshal.PtrToStructure(runptr,
structtype)
runptr = New IntPtr(runptr.ToInt32 + structsize)
Next
End If

Marshal.FreeHGlobal(bufptr)

mincoming = TRasCon

End Sub
Public Function DialEntry(ByVal mentryname As String, ByVal
musername As String, ByVal mpassword As String, ByRef mcallback As
myrasdialfunc) As IntPtr

Dim objRASParams As RASDIALPARAMS
Dim hRASConn As IntPtr

Dim mystr() As String

With objRASParams
.szEntryName = mentryname
.szPhoneNumber = ""
.szCallbackNumber = "*"
.szUserName = musername
.szPassword = mpassword
.szDomain = "*"
.dwSize = Marshal.SizeOf(GetType(RASDIALPARAMS))
End With

Dim intRet As Integer = RasDial(IntPtr.Zero, Nothing,
objRASParams, 0, mcallback, hRASConn)
DialEntry = hRASConn

End Function
Public Sub HangEntry(ByVal mentryname As String)

Dim structtype As Type = GetType(RASCONN)
Dim structsize As Integer = Marshal.SizeOf(GetType(RASCONN))
Dim bufsize As Integer = structsize
Dim realcount As Integer
Dim TRasCon() As RASCONN

Dim bufptr As IntPtr = Marshal.AllocHGlobal(bufsize)
Marshal.WriteInt32(bufptr, structsize)

Dim retcode As Integer = RasEnumConnections(bufptr, bufsize,
realcount)

If retcode = ERROR_BUFFER_TOO_SMALL Then
bufptr = Marshal.ReAllocHGlobal(bufptr, New
IntPtr(structsize))
Marshal.WriteInt32(bufptr, structsize)
retcode = RasEnumConnections(bufptr, bufsize, realcount)
End If

If (retcode = 0) And (realcount > 0) Then
ReDim TRasCon(realcount - 1)
Dim i As Integer
Dim runptr As IntPtr = bufptr

For i = 0 To (realcount - 1)
TRasCon(i) = Marshal.PtrToStructure(runptr,
structtype)
runptr = New IntPtr(runptr.ToInt32 + structsize)
Next
End If

Marshal.FreeHGlobal(bufptr)

Dim m As RASCONN
For Each m In TRasCon
If m.szEntryname = mentryname Then
RasHangUp(m.hRasCon)
End If
Next

End Sub
Public Function IsConnected(ByVal mentryname As String) As Boolean

Dim structtype As Type = GetType(RASCONN)
Dim structsize As Integer = Marshal.SizeOf(GetType(RASCONN))
Dim bufsize As Integer = structsize
Dim realcount As Integer
Dim TRasCon() As RASCONN

Dim bufptr As IntPtr = Marshal.AllocHGlobal(bufsize)
Marshal.WriteInt32(bufptr, structsize)

Dim retcode As Integer = RasEnumConnections(bufptr, bufsize,
realcount)

If retcode = ERROR_BUFFER_TOO_SMALL Then
bufptr = Marshal.ReAllocHGlobal(bufptr, New
IntPtr(structsize))
Marshal.WriteInt32(bufptr, structsize)
retcode = RasEnumConnections(bufptr, bufsize, realcount)
End If

If (retcode = 0) And (realcount > 0) Then
ReDim TRasCon(realcount - 1)
Dim i As Integer
Dim runptr As IntPtr = bufptr

For i = 0 To (realcount - 1)
TRasCon(i) = Marshal.PtrToStructure(runptr,
structtype)
runptr = New IntPtr(runptr.ToInt32 + structsize)
Next
End If

Marshal.FreeHGlobal(bufptr)

Dim m As RASCONN
For Each m In TRasCon
If m.szEntryname = mentryname Then
IsConnected = True
End If
Next

End Function

End Class

0 new messages