473,545 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ras, dotnet, vb Here is a RAS wrapper for VB, along with some examples

''''''''''''''T he class needs to be called clsRAS, paste the following
Imports System.Runtime. InteropServices
Public Class ras
Private Const RAS_MaxEntryNam e As Integer = 256
Private Const RAS_MaxPhoneNum ber 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_MaxDeviceTy pe As Integer = 16
Private Const RAS_MaxDeviceNa me As Integer = 128
Private Const RAS_MaxCallback Number As Integer =
RAS_MaxPhoneNum ber
Private Const ERROR_BUFFER_TO O_SMALL = 603
Private Const RDEOPT_IgnoreMo demSpeaker = 4&
Private Const RDEOPT_SetModem Speaker = &H8S

Private mvarprevioushan dle As IntPtr

Public Declare Auto Function RasGetErrorStri ng Lib "rasapi32.d ll"
(ByVal uErrorValue As Integer, ByVal lpszErrorString As String, ByVal
cBufSize As Integer) As Integer

<StructLayout(L ayoutKind.Seque ntial, Pack:=1,
CharSet:=CharSe t.Auto)> _
Private Structure RASDIALEXTENSIO NS
Public dwSize As Integer
Public dwfOptions As Integer
Public hwndParent As Integer
Public Reserved As Integer
End Structure

<StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Auto)> _
Public Structure RASDIALPARAMS
Public dwSize As Integer
<MarshalAs(Unma nagedType.ByVal TStr,
SizeConst:=RAS_ MaxEntryName + 1)> Public szEntryName As String
<MarshalAs(Unma nagedType.ByVal TStr,
SizeConst:=RAS_ MaxPhoneNumber + 1)> Public szPhoneNumber As String
<MarshalAs(Unma nagedType.ByVal TStr,
SizeConst:=RAS_ MaxCallbackNumb er + 1)> Public szCallbackNumbe r As
String
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=UNLE N + 1)>
Public szUserName As String
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=PWLE N + 1)>
Public szPassword As String
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=DNLE N + 1)>
Public szDomain As String
End Structure

Public Delegate Function myrasdialfunc(B yVal hrasconn As IntPtr,
ByVal unMsg As Integer, ByVal rasconnstate As Integer, ByVal dwError
As Integer, ByVal dwexterror As Integer) As Integer

Private Declare Auto Function RasDial Lib "rasapi32.d ll" (ByRef
lpRasDialExtens ions As RASDIALEXTENSIO NS, ByVal lpszPhonebook As
String, ByRef lpRasDialParams As RASDIALPARAMS, ByVal dwNotifierType
As Integer, ByVal lpvNotifier As myrasdialfunc, ByRef lphRasConn As
IntPtr) As Integer
Public Declare Auto Function RasHangUp Lib "rasapi32.d ll" (ByVal
hRasConn As IntPtr) As Integer

<StructLayout(L ayoutKind.Seque ntial, Pack:=4,
CharSet:=CharSe t.Auto)> _
Public Structure RASCONN
Public dwSize As Integer
Public hRasCon As IntPtr
<MarshalAs(Unma nagedType.ByVal TStr,
sizeconst:=RAS_ MaxEntryName + 1)> Public szEntryname As String
<MarshalAs(Unma nagedType.ByVal TStr,
sizeconst:=RAS_ MaxDeviceType + 1)> Public szDeviceType As String
<MarshalAs(Unma nagedType.ByVal TStr,
sizeconst:=RAS_ MaxDeviceName + 1)> Public szDeviceName As String
End Structure

Private Declare Auto Function RasEnumConnecti ons Lib
"rasapi32.d ll" (ByVal lpRasCon As IntPtr, ByRef lpcb As Integer, ByRef
lpcConnections As Integer) As Integer

<StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Auto)> _
Public Structure RASENTRYNAME
Public dwSize As Integer
<MarshalAs(Unma nagedType.ByVal TStr,
SizeConst:=RAS_ MaxEntryName + 1)> Public szEntryName As String
Public dwFlags As Integer
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=MAX_ PATH + 1)>
Public szPhonebookPath As String
End Structure

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

Public Property PreviousHandle( ) As IntPtr
Get
Return mvarprevioushan dle
End Get
Set(ByVal Value As IntPtr)
mvarprevioushan dle = Value
End Set
End Property
Public Sub EnumEntries(ByR ef mincoming() As RASENTRYNAME)
Dim entries() As RASENTRYNAME
Dim rasentrynamelen As Integer =
Marshal.SizeOf( GetType(RASENTR YNAME))
Dim lpcb As Integer = rasentrynamelen
Dim lpcentries As Integer

Dim parray As IntPtr = Marshal.AllocHG lobal(rasentryn amelen)
Marshal.WriteIn t32(parray, rasentrynamelen )

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

If ret = ERROR_BUFFER_TO O_SMALL Then
parray = Marshal.ReAlloc HGlobal(parray, New IntPtr(lpcb))
Marshal.WriteIn t32(parray, rasentrynamelen )
ret = RasEnumEntries( Nothing, Nothing, parray, lpcb,
lpcentries)
ElseIf ret <> 0 Then
Throw New Exception(GetRa sError(ret))
End If
If ret = 0 And lpcentries > 0 Then
ReDim entries(lpcentr ies - 1)
Dim pentry As IntPtr = parray
Dim i As Integer
For i = 0 To lpcentries - 1
entries(i) = Marshal.PtrToSt ructure(pentry,
GetType(RASENTR YNAME))
pentry = New IntPtr(pentry.T oInt32 + rasentrynamelen )
Next
pentry = Nothing
End If

Marshal.FreeHGl obal(parray)

mincoming = entries

entries = Nothing
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.AllocHG lobal(structsiz e)
Marshal.WriteIn t32(bufptr, structsize)

Dim retcode As Integer = RasEnumConnecti ons(bufptr, bufsize,
realcount)

If retcode = ERROR_BUFFER_TO O_SMALL Then
bufptr = Marshal.ReAlloc HGlobal(bufptr, New
IntPtr(bufsize) )
Marshal.WriteIn t32(bufptr, structsize)
retcode = RasEnumConnecti ons(bufptr, bufsize, realcount)
ElseIf retcode <> 0 Then
Throw New Exception(GetRa sError(retcode) )
End If

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

For i = 0 To (realcount - 1)
TRasCon(i) = Marshal.PtrToSt ructure(runptr,
structtype)
runptr = New IntPtr(runptr.T oInt32 + structsize)
Next
runptr = Nothing
End If

Marshal.FreeHGl obal(bufptr)

mincoming = TRasCon

TRasCon = Nothing

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

Dim objRASParams As New RASDIALPARAMS()
Dim mvarRasExtensio n As New RASDIALEXTENSIO NS()
Dim hRASConn As New IntPtr()

With mvarRasExtensio n
.hwndParent = 0&
.Reserved = 0
.dwfOptions = RDEOPT_IgnoreMo demSpeaker
.dwSize = Marshal.SizeOf( GetType(RASDIAL EXTENSIONS))
End With

With objRASParams
.szEntryName = mentryname
.szPhoneNumber = ""
.szCallbackNumb er = ""
.szUserName = musername
.szPassword = mpassword
.szDomain = "*"
.dwSize = Marshal.SizeOf( GetType(RASDIAL PARAMS))
End With

Dim intRet As Integer = RasDial(mvarRas Extension, Nothing,
objRASParams, 1, mcallback, hRASConn)
If intRet <> 0 Then
Dim errorstring As String = GetRasError(int Ret)
If errorstring.Ind exOf("already being dialed") Then
If hRASConn.Equals (IntPtr.Zero) = False Then
RasHangUp(hRASC onn)
If mvarprevioushan dle.Equals(IntP tr.Zero) = False Then
RasHangUp(mvarp revioushandle)
End If
Throw New Exception(error string)
End If

DialEntry = hRASConn

objRASParams = Nothing
hRASConn = Nothing
mvarRasExtensio n = Nothing

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.AllocHG lobal(structsiz e)
Marshal.WriteIn t32(bufptr, structsize)

Dim retcode As Integer = RasEnumConnecti ons(bufptr, bufsize,
realcount)

If retcode = ERROR_BUFFER_TO O_SMALL Then
bufptr = Marshal.ReAlloc HGlobal(bufptr, New
IntPtr(bufsize) )
Marshal.WriteIn t2(bufptr, structsize)
retcode = RasEnumConnecti ons(bufptr, bufsize, realcount)
ElseIf retcode <> 0 Then
Throw New Exception(GetRa sError(retcode) )
End If

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

For i = 0 To (realcount - 1)
TRasCon(i) = Marshal.PtrToSt ructure(runptr,
structtype)
runptr = New IntPtr(runptr.T oInt32 + structsize)
Next

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

runptr = Nothing
End If

Marshal.FreeHGl obal(bufptr)

bufptr = Nothing
End Sub
Public Function IsConnected(ByV al 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 entrycount As Integer
Dim entries() As RASCONN

Dim bufptr As IntPtr = Marshal.AllocHG lobal(structsiz e)
Marshal.WriteIn t32(bufptr, structsize)

Dim retcode As Integer = RasEnumConnecti ons(bufptr, bufsize,
entrycount)

If retcode = ERROR_BUFFER_TO O_SMALL Then
bufptr = Marshal.ReAlloc HGlobal(bufptr, New
IntPtr(bufsize) )
Marshal.WriteIn t32(bufptr, structsize)
retcode = RasEnumConnecti ons(bufptr, bufsize, entrycount)
ElseIf retcode <> 0 Then
Throw New Exception(GetRa sError(retcode) )
End If

If (retcode = 0) And (entrycount > 0) Then
ReDim entries(entryco unt - 1)
Dim i As Integer
Dim runptr As IntPtr = bufptr

For i = 0 To (entrycount - 1)
entries(i) = Marshal.PtrToSt ructure(runptr,
structtype)
runptr = New IntPtr(runptr.T oInt32 + structsize)
Next

runptr = Nothing

Dim mEntry As RASCONN
For Each mEntry In entries
If mEntry.szEntryn ame = mentryname Then
IsConnected = True
End If
Next

End If

Marshal.FreeHGl obal(bufptr)

bufptr = Nothing
End Function
Private Function GetRasError(ByV al dwerror As Integer) As String

Dim sErrMsg As New String(Space(51 2))
Dim lret As Integer

lret = ras.RasGetError String(dwerror, sErrMsg, Len(sErrMsg))
If lret = 0 Then
sErrMsg = sErrMsg.Remove( sErrMsg.IndexOf (Chr(0)),
Len(sErrMsg) - sErrMsg.IndexOf (Chr(0)))
GetRasError = "Error # " & dwerror & "; Error Description:
" & sErrMsg
Else
GetRasError = "Unknown RAS Error"
End If

End Function
End Class

''''''''''''''' ''The form can be called anything, paste the following
Imports System.IO
Imports System.Net
Public Class frmRAS
Inherits System.Windows. Forms.Form
Private mvarvpncallback As ras.myrasdialfu nc = AddressOf
VPNRasDialFunc
Private mvarispcallback As ras.myrasdialfu nc = AddressOf
ISPRasDialFunc
Private mvarProgramName As String
Private mvarvpnconnecti onname As String
Private mvarvpnusername As String
Private mvarvpnpassword As String
Private mvarvpnretryint erval As Integer
Private mvarispconnecti onname As String
Private mvarispusername As String
Private mvarisppassword As String
Private mvarispretryint erval As Integer
Private mvarvpnprevhand le As IntPtr
Private mvarispprevhand le As IntPtr

Private Function VPNRasDialFunc( ByVal hrasconn As IntPtr, ByVal
unMsg As Integer, ByVal rasconnstate As Integer, ByVal dwError As
Integer, ByVal dwexterror As Integer) As Integer
'Debug.WriteLin e("DF: " & unMsg.ToString & " - " &
rasconnstate.To String & " - " & dwError.ToStrin g)
'If dwError <> 0 Then Debug.WriteLine (GetRasError(dw Error))
Try
If dwError <> 0 Then
ras.RasHangUp(h rasconn)
Throw New System.Exceptio n(GetRasError(d wError))
End If
Catch exp As Exception
ErrorToXML(exp)
End Try

End Function
Private Function ISPRasDialFunc( ByVal hrasconn As IntPtr, ByVal
unMsg As Integer, ByVal rasconnstate As Integer, ByVal dwError As
Integer, ByVal dwexterror As Integer) As Integer
'Debug.WriteLin e("DF: " & unMsg.ToString & " - " &
rasconnstate.To String & " - " & dwError.ToStrin g)
'If dwError <> 0 Then Debug.WriteLine (GetRasError(dw Error))
Try
If dwError <> 0 Then
ras.RasHangUp(h rasconn)
Throw New System.Exceptio n(GetRasError(d wError))
End If
Catch exp As Exception
ErrorToXML(exp)
End Try

End Function
''''''''''''''F ill in all the usernames and passwords as appropriate
Private Sub GetRegistrySett ings()

Try
mvarvpnusername = "VPNUserNam e"
mvarvpnconnecti onname = "VPNConnectionN ame"
mvarvpnpassword = "VPNPasswor d"
mvarvpnretryint erval = "VPNRetryInterv al"
mvarispusername = "ISPUserNam e"
mvarispconnecti onname = "ISPConnectionN ame"
mvarisppassword = "ISPPassowr d"
Catch exp As Exception
ErrorToXML(exp)
Finally
mvarcregistry = Nothing
End Try
End Sub
Private Sub ErrorToXML(ByVa l exp As Exception)
Try
MessageBox.Show (exp.ToString)
Catch expprivate As Exception
MessageBox.Show (exp.ToString)
Finally
'nothing
End Try
End Sub

Private Sub frmRAS_Load(ByV al sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
If (UBound(Diagnos tics.Process.Ge tProcessesByNam e(Diagnostics.P rocess.GetCurre ntProcess.Proce ssName))
0) = True Then

Me.Close()
End If
mvarProgramName = System.Windows. Forms.Applicati on.ProductName
& " " & System.Windows. Forms.Applicati on.ProductVersi on
Try
GetRegistrySett ings()
Catch exp As Exception
ErrorToXML(exp)
End Try

End Sub

''''''''''''Thi s is the main function that calls the RAS class. The
previous handle, if it failed, must be hung, before trying to redial.
This program was designed to keep the computer on the internet and vpn
indefinately.

Private Sub tmrDialer_Tick( ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles tmrDialer.Tick
Dim mvarVPNRas As New ras(), mvarISPRAS As New ras(),
mvartemphandle As New IntPtr()

Try
If mvarispretryint erval > 0 Then
If Now.Minute Mod mvarispretryint erval = 0 Then
If mvarISPRAS.IsCo nnected(mvarisp connectionname) =
False Then
If mvarispprevhand le.Equals(IntPt r.Zero) =
False Then mvarISPRAS.Prev iousHandle = mvarispprevhand le
mvartemphandle =
mvarISPRAS.Dial Entry(mvarispco nnectionname, mvarispusername ,
mvarisppassword , mvarispcallback )
If mvartemphandle. Equals(IntPtr.Z ero) = False
Then mvarispprevhand le = mvartemphandle
End If
End If
End If
Catch exp As Exception
ErrorToXML(exp)
End Try

Try
If mvarvpnretryint erval > 0 Then
If Now.Minute Mod mvarvpnretryint erval = 0 Then
If mvarVPNRas.IsCo nnected(mvarvpn connectionname) =
False Then
If mvarvpnprevhand le.Equals(IntPt r.Zero) =
False Then mvarVPNRas.Prev iousHandle = mvarvpnprevhand le
mvartemphandle =
mvarVPNRas.Dial Entry(mvarvpnco nnectionname, mvarvpnusername ,
mvarvpnpassword , mvarvpncallback )
If mvartemphandle. Equals(IntPtr.Z ero) = False
Then mvarvpnprevhand le = mvartemphandle
End If
End If
End If
Catch exp As Exception
ErrorToXML(exp)
End Try

mvarVPNRas = Nothing
mvarISPRAS = Nothing
mvartemphandle = Nothing

End Sub
Private Function GetRasError(ByV al dwerror As Integer) As String

Dim sErrMsg As New String(Space(51 2))
Dim lret As Integer

lret = ras.RasGetError String(dwerror, sErrMsg, Len(sErrMsg))
If lret = 0 Then
sErrMsg = sErrMsg.Remove( sErrMsg.IndexOf (Chr(0)),
Len(sErrMsg) - sErrMsg.IndexOf (Chr(0)))
GetRasError = "Error # " & dwerror & "; Error Description:
" & sErrMsg
Else
GetRasError = "Unknown RAS Error"
End If

End Function

End Class
Nov 20 '05 #1
2 2974
That's exacly what I was looking for...
But I still have problems with running your code...
Would you be so kind and send me running wersion of it?
mailto: zu**@wp.pl
I need only working RasEnumConnecti ons function - I still have some problems
with this...
Please help,
zurg
Uzytkownik "Black Ninja" <zm****@hotmail .com> napisal w wiadomosci
news:ba******** *************** ***@posting.goo gle.com...
''''''''''''''T he class needs to be called clsRAS, paste the following
Imports System.Runtime. InteropServices
Public Class ras
Private Const RAS_MaxEntryNam e As Integer = 256
Private Const RAS_MaxPhoneNum ber 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_MaxDeviceTy pe As Integer = 16
Private Const RAS_MaxDeviceNa me As Integer = 128
Private Const RAS_MaxCallback Number As Integer =
RAS_MaxPhoneNum ber
Private Const ERROR_BUFFER_TO O_SMALL = 603
Private Const RDEOPT_IgnoreMo demSpeaker = 4&
Private Const RDEOPT_SetModem Speaker = &H8S

Private mvarprevioushan dle As IntPtr

Public Declare Auto Function RasGetErrorStri ng Lib "rasapi32.d ll"
(ByVal uErrorValue As Integer, ByVal lpszErrorString As String, ByVal
cBufSize As Integer) As Integer

<StructLayout(L ayoutKind.Seque ntial, Pack:=1,
CharSet:=CharSe t.Auto)> _
Private Structure RASDIALEXTENSIO NS
Public dwSize As Integer
Public dwfOptions As Integer
Public hwndParent As Integer
Public Reserved As Integer
End Structure

<StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Auto)> _
Public Structure RASDIALPARAMS
Public dwSize As Integer
<MarshalAs(Unma nagedType.ByVal TStr,
SizeConst:=RAS_ MaxEntryName + 1)> Public szEntryName As String
<MarshalAs(Unma nagedType.ByVal TStr,
SizeConst:=RAS_ MaxPhoneNumber + 1)> Public szPhoneNumber As String
<MarshalAs(Unma nagedType.ByVal TStr,
SizeConst:=RAS_ MaxCallbackNumb er + 1)> Public szCallbackNumbe r As
String
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=UNLE N + 1)>
Public szUserName As String
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=PWLE N + 1)>
Public szPassword As String
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=DNLE N + 1)>
Public szDomain As String
End Structure

Public Delegate Function myrasdialfunc(B yVal hrasconn As IntPtr,
ByVal unMsg As Integer, ByVal rasconnstate As Integer, ByVal dwError
As Integer, ByVal dwexterror As Integer) As Integer

Private Declare Auto Function RasDial Lib "rasapi32.d ll" (ByRef
lpRasDialExtens ions As RASDIALEXTENSIO NS, ByVal lpszPhonebook As
String, ByRef lpRasDialParams As RASDIALPARAMS, ByVal dwNotifierType
As Integer, ByVal lpvNotifier As myrasdialfunc, ByRef lphRasConn As
IntPtr) As Integer
Public Declare Auto Function RasHangUp Lib "rasapi32.d ll" (ByVal
hRasConn As IntPtr) As Integer

<StructLayout(L ayoutKind.Seque ntial, Pack:=4,
CharSet:=CharSe t.Auto)> _
Public Structure RASCONN
Public dwSize As Integer
Public hRasCon As IntPtr
<MarshalAs(Unma nagedType.ByVal TStr,
sizeconst:=RAS_ MaxEntryName + 1)> Public szEntryname As String
<MarshalAs(Unma nagedType.ByVal TStr,
sizeconst:=RAS_ MaxDeviceType + 1)> Public szDeviceType As String
<MarshalAs(Unma nagedType.ByVal TStr,
sizeconst:=RAS_ MaxDeviceName + 1)> Public szDeviceName As String
End Structure

Private Declare Auto Function RasEnumConnecti ons Lib
"rasapi32.d ll" (ByVal lpRasCon As IntPtr, ByRef lpcb As Integer, ByRef
lpcConnections As Integer) As Integer

<StructLayout(L ayoutKind.Seque ntial, CharSet:=CharSe t.Auto)> _
Public Structure RASENTRYNAME
Public dwSize As Integer
<MarshalAs(Unma nagedType.ByVal TStr,
SizeConst:=RAS_ MaxEntryName + 1)> Public szEntryName As String
Public dwFlags As Integer
<MarshalAs(Unma nagedType.ByVal TStr, SizeConst:=MAX_ PATH + 1)>
Public szPhonebookPath As String
End Structure

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

Public Property PreviousHandle( ) As IntPtr
Get
Return mvarprevioushan dle
End Get
Set(ByVal Value As IntPtr)
mvarprevioushan dle = Value
End Set
End Property
Public Sub EnumEntries(ByR ef mincoming() As RASENTRYNAME)
Dim entries() As RASENTRYNAME
Dim rasentrynamelen As Integer =
Marshal.SizeOf( GetType(RASENTR YNAME))
Dim lpcb As Integer = rasentrynamelen
Dim lpcentries As Integer

Dim parray As IntPtr = Marshal.AllocHG lobal(rasentryn amelen)
Marshal.WriteIn t32(parray, rasentrynamelen )

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

If ret = ERROR_BUFFER_TO O_SMALL Then
parray = Marshal.ReAlloc HGlobal(parray, New IntPtr(lpcb))
Marshal.WriteIn t32(parray, rasentrynamelen )
ret = RasEnumEntries( Nothing, Nothing, parray, lpcb,
lpcentries)
ElseIf ret <> 0 Then
Throw New Exception(GetRa sError(ret))
End If
If ret = 0 And lpcentries > 0 Then
ReDim entries(lpcentr ies - 1)
Dim pentry As IntPtr = parray
Dim i As Integer
For i = 0 To lpcentries - 1
entries(i) = Marshal.PtrToSt ructure(pentry,
GetType(RASENTR YNAME))
pentry = New IntPtr(pentry.T oInt32 + rasentrynamelen )
Next
pentry = Nothing
End If

Marshal.FreeHGl obal(parray)

mincoming = entries

entries = Nothing
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.AllocHG lobal(structsiz e)
Marshal.WriteIn t32(bufptr, structsize)

Dim retcode As Integer = RasEnumConnecti ons(bufptr, bufsize,
realcount)

If retcode = ERROR_BUFFER_TO O_SMALL Then
bufptr = Marshal.ReAlloc HGlobal(bufptr, New
IntPtr(bufsize) )
Marshal.WriteIn t32(bufptr, structsize)
retcode = RasEnumConnecti ons(bufptr, bufsize, realcount)
ElseIf retcode <> 0 Then
Throw New Exception(GetRa sError(retcode) )
End If

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

For i = 0 To (realcount - 1)
TRasCon(i) = Marshal.PtrToSt ructure(runptr,
structtype)
runptr = New IntPtr(runptr.T oInt32 + structsize)
Next
runptr = Nothing
End If

Marshal.FreeHGl obal(bufptr)

mincoming = TRasCon

TRasCon = Nothing

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

Dim objRASParams As New RASDIALPARAMS()
Dim mvarRasExtensio n As New RASDIALEXTENSIO NS()
Dim hRASConn As New IntPtr()

With mvarRasExtensio n
.hwndParent = 0&
.Reserved = 0
.dwfOptions = RDEOPT_IgnoreMo demSpeaker
.dwSize = Marshal.SizeOf( GetType(RASDIAL EXTENSIONS))
End With

With objRASParams
.szEntryName = mentryname
.szPhoneNumber = ""
.szCallbackNumb er = ""
.szUserName = musername
.szPassword = mpassword
.szDomain = "*"
.dwSize = Marshal.SizeOf( GetType(RASDIAL PARAMS))
End With

Dim intRet As Integer = RasDial(mvarRas Extension, Nothing,
objRASParams, 1, mcallback, hRASConn)
If intRet <> 0 Then
Dim errorstring As String = GetRasError(int Ret)
If errorstring.Ind exOf("already being dialed") Then
If hRASConn.Equals (IntPtr.Zero) = False Then
RasHangUp(hRASC onn)
If mvarprevioushan dle.Equals(IntP tr.Zero) = False Then
RasHangUp(mvarp revioushandle)
End If
Throw New Exception(error string)
End If

DialEntry = hRASConn

objRASParams = Nothing
hRASConn = Nothing
mvarRasExtensio n = Nothing

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.AllocHG lobal(structsiz e)
Marshal.WriteIn t32(bufptr, structsize)

Dim retcode As Integer = RasEnumConnecti ons(bufptr, busize,
realcount)

If retcode = ERROR_BUFFER_TO O_SMALL Then
bufptr = Marshal.ReAlloc HGlobal(bufptr, New
IntPtr(bufsize) )
Marshal.WriteIn t32(bufptr, structsize)
retcode = RasEnumConnecti ons(bufptr, bufsize, realcount)
ElseIf retcode <> 0 Then
Throw New Exception(GetRa sError(retcode) )
End If

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

For i = 0 To (realcount - 1)
TRasCon(i) = Marshal.PtrToSt ructure(runptr,
structtype)
runptr = New IntPtr(runptr.T oInt32 + structsize)
Next

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

runptr = Nothing
End If

Marshal.FreeHGl obal(bufptr)

bufptr = Nothing
End Sub
Public Function IsConnected(ByV al 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 entrycount As Integer
Dim entries() As RASCONN

Dim bufptr As IntPtr = Marshal.AllocHG lobal(structsiz e)
Marshal.WriteIn t32(bufptr, structsize)

Dim retcode As Integer = RasEnumConnecti ons(bufptr, bufsize,
entrycount)

If retcode = ERROR_BUFFER_TO O_SMALL Then
bufptr = Marshal.ReAlloc HGlobal(bufptr, New
IntPtr(bufsize) )
Marshal.WriteIn t32(bufptr, structsize)
retcode = RasEnumConnecti ons(bufptr, bufsize, entrycount)
ElseIf retcode <> 0 Then
Throw New Exception(GetRa sError(retcode) )
End If

If (retcode = 0) And (entrycount > 0) Then
ReDim entries(entryco unt - 1)
Dim i As Integer
Dim runptr As IntPtr = bufptr

For i = 0 To (entrycount - 1)
entries(i) = Marshal.PtrToSt ructure(runptr,
structtype)
runptr = New IntPtr(runptr.T oInt32 + structsize)
Next

runptr = Nothing

Dim mEntry As RASCONN
For Each mEntry In entries
If mEntry.szEntryn ame = mentryname Then
IsConnected = True
End If
Next

End If

Marshal.FreeHGl obal(bufptr)

bufptr = Nothing
End Function
Private Function GetRasError(ByV al dwerror As Integer) As String

Dim sErrMsg As New String(Space(51 2))
Dim lret As Integer

lret = ras.RasGetError String(dwerror, sErrMsg, Len(sErrMsg))
If lret = 0 Then
sErrMsg = sErrMsg.Remove( sErrMsg.IndexOf (Chr(0)),
Len(sErrMsg) - sErrMsg.IndexOf (Chr(0)))
GetRasError = "Error # " & dwerror & "; Error Description:
" & sErrMsg
Else
GetRasError = "Unknown RAS Error"
End If

End Function
End Class

''''''''''''''' ''The form can be called anything, paste the following
Imports System.IO
Imports System.Net
Public Class frmRAS
Inherits System.Windows. Forms.Form
Private mvarvpncallback As ras.myrasdialfu nc = AddressOf
VPNRasDialFunc
Private mvarispcallback As ras.myrasdialfu nc = AddressOf
ISPRasDialFunc
Private mvarProgramName As String
Private mvarvpnconnecti onname As String
Private mvarvpnusername As String
Private mvarvpnpassword As String
Private mvarvpnretryint erval As Integer
Private mvarispconnecti onname As String
Private mvarispusername As String
Private mvarisppassword As String
Private mvarispretryint erval As Integer
Private mvarvpnprevhand le As IntPtr
Private mvarispprevhand le As IntPtr

Private Function VPNRasDialFunc( ByVal hrasconn As IntPtr, ByVal
unMsg As Integer, ByVal rasconnstate As Integer, ByVal dwError As
Integer, ByVal dwexterror As Integer) As Integer
'Debug.WriteLin e("DF: " & unMsg.ToString & " - " &
rasconnstate.To String & " - " & dwError.ToStrin g)
'If dwError <> 0 Then Debug.WriteLine (GetRasError(dw Error))
Try
If dwError <> 0 Then
ras.RasHangUp(h rasconn)
Throw New System.Exceptio n(GetRasError(d wError))
End If
Catch exp As Exception
ErrorToXML(exp)
End Try

End Function
Private Function ISPRasDialFunc( ByVal hrasconn As IntPtr, ByVal
unMsg As Integer, ByVal rasconnstate As Integer, ByVal dwError As
Integer, ByVal dwexterror As Integer) As Integer
'Debug.WriteLin e("DF: " & unMsg.ToString & " - " &
rasconnstate.To String & " - " & dwError.ToStrin g)
'If dwError <> 0 Then Debug.WriteLine (GetRasError(dw Error))
Try
If dwError <> 0 Then
ras.RasHangUp(h rasconn)
Throw New System.Exceptio n(GetRasError(d wError))
End If
Catch exp As Exception
ErrorToXML(exp)
End Try

End Function
''''''''''''''F ill in all the usernames and passwords as appropriate
Private Sub GetRegistrySett ings()

Try
mvarvpnusername = "VPNUserNam e"
mvarvpnconnecti onname = "VPNConnectionN ame"
mvarvpnpassword = "VPNPasswor d"
mvarvpnretryint erval = "VPNRetryInterv al"
mvarispusername = "ISPUserNam e"
mvarispconnecti onname = "ISPConnectionN ame"
mvarisppassword = "ISPPassowr d"
Catch exp As Exception
ErrorToXML(exp)
Finally
mvarcregistry = Nothing
End Try
End Sub
Private Sub ErrorToXML(ByVa l exp As Exception)
Try
MessageBox.Show (exp.ToString)
Catch expprivate As Exception
MessageBox.Show (exp.ToString)
Finally
'nothing
End Try
End Sub

Private Sub frmRAS_Load(ByV al sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
If

(UBound(Diagnos tics.Process.Ge tProcessesByNam e(Diagnostics.P rocess.GetCurre n
tProcess.Proces sName))
0) = True Then

Me.Close()
End If
mvarProgramName = System.Windows. Forms.Applicati on.ProductName
& " " & System.Windows. Forms.Applicati on.ProductVersi on
Try
GetRegistrySett ings()
Catch exp As Exception
ErrorToXML(exp)
End Try

End Sub

''''''''''''Thi s is the main function that calls the RAS class. The
previous handle, if it failed, must be hung, before trying to redial.
This program was designed to keep the computer on the internet and vpn
indefinately.

Private Sub tmrDialer_Tick( ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles tmrDialer.Tick
Dim mvarVPNRas As New ras(), mvarISPRAS As New ras(),
mvartemphandle As New IntPtr()

Try
If mvarispretryint erval > 0 Then
If Now.Minute Mod mvarispretryint erval = 0 Then
If mvarISPRAS.IsCo nnected(mvarisp connectionname) =
False Then
If mvarispprevhand le.Equals(IntPt r.Zero) =
False Then mvarISPRAS.Prev iousHandle = mvarispprevhand le
mvartemphandle =
mvarISPRAS.Dial Entry(mvarispco nnectionname, mvarispusername ,
mvarisppassword , mvarispcallback )
If mvartemphandle. Equals(IntPtr.Z ero) = False
Then mvarispprevhand le = mvartemphandle
End If
End If
End If
Catch exp As Exception
ErrorToXML(exp)
End Try

Try
If mvarvpnretryint erval > 0 Then
If Now.Minute Mod mvarvpnretryint erval = 0 Then
If mvarVPNRas.IsCo nnected(mvarvpn connectionname) =
False Then
If mvarvpnprevhand le.Equals(IntPt r.Zero) =
False Then mvarVPNRas.Prev iousHandle = mvarvpnprevhand le
mvartemphandle =
mvarVPNRas.Dial Entry(mvarvpnco nnectionname, mvarvpnusername ,
mvarvpnpassword , mvarvpncallback )
If mvartemphandle. Equals(IntPtr.Z ero) = False
Then mvarvpnprevhand le = mvartemphandle
End If
End If
End If
Catch exp As Exception
ErrorToXML(exp)
End Try

mvarVPNRas = Nothing
mvarISPRAS = Nothing
mvartemphandle = Nothing

End Sub
Private Function GetRasError(ByV al dwerror As Integer) As String

Dim sErrMsg As New String(Space(51 2))
Dim lret As Integer

lret = ras.RasGetError String(dwerror, sErrMsg, Len(sErrMsg))
If lret = 0 Then
sErrMsg = sErrMsg.Remove( sErrMsg.IndexOf (Chr(0)),
Len(sErrMsg) - sErrMsg.IndexOf (Chr(0)))
GetRasError = "Error # " & dwerror & "; Error Description:
" & sErrMsg
Else
GetRasError = "Unknown RAS Error"
End If

End Function

End Class

Nov 20 '05 #2
I suggest you submit this to GotDotNet.Com
"Black Ninja" <zm****@hotmail .com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
''''''''''''''T he class needs to be called clsRAS, paste the following
Imports System.Runtime. InteropServices

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
4057
by: Egil M?ller | last post by:
Is there any way to create transparent wrapper objects in Python? I thought implementing __getattribute__ on either the wrapper class or its metaclass would do the trick, but it does not work for the built in operators: class Foo(object): class __metaclass__(type): def __getattribute__(self, name): print "Klass", name
0
1702
by: Nilsson Mats | last post by:
Hi! I have an intresting problem for our programming community on Solaris. I want to develop an environment where: 1) The developers shouldn't need to bother about which Perl version to use. 2) When the Perl version is changed in a controlled manner, the change should be instant. 3) For developers whose scripts fails after a Perl version...
3
3750
by: mscir | last post by:
I'm trying to find thorough documentation for creating graphs using serverside Excel from ASP. Any book recommendations? A well-documented COM wrapper would be nice if it allowed us to create may different types of graph, e.g. Wind Rose (for wind direction), thermometer, as well as standard pie, line, bar charts. I've found a few examples...
15
3799
by: Steve Richfield | last post by:
To All, First, some of your replies to me have been posted through DevelopersDex, and these are NOT posted on USENET for the world to see. DevelopersDex appears to be trying to hijack USENET, though there may be some more benign explanation that escapes me. If you want the world to see your replies, then you should post them directly to...
41
3376
by: Mountain Bikn' Guy | last post by:
What is the current preferred way to save user preferences in dotnet? Is the registry the right place to do this? Can anyone recommend a good article (or book) for this topic? Thanks.
0
1416
by: pavan kumar | last post by:
hi, Please refer this article because that is the dll iam using for compression/decompression of datatables. http://www.eggheadcafe.com/articles/20031219.asp This is a windows application in C# & iam using remoting extensively for my database layer. In the remote server iam having a method which returns byte instead of
9
2403
by: WithPit | last post by:
I am trying to create an Managed C++ Wrapper around an unmanaged library which contains C++ code. Some of the unmanaged methods returns an returntype which is of the abstract base type (for example unmanagedObject). How can i convert this to the managed abstract basetype? Hope somebody can help me Thanx
22
2488
by: linwu02 | last post by:
I am trying to write a Wrapper for our web wrapping engine, Cameleon. Currently Cameleon is able to answer certain SQL queries but with a restriction that all SQL queries must have a predicate. This is very similar to the BLAST wrapper. For example, Cameleon can answer queries like this by accessing Yahoo's IBM finance webpage Select...
23
1623
by: I.M. !Knuth | last post by:
A while back, I was mucking around with a recursive function. For brevity's sake, I'll define it like this: int func_recurs(int arg1, int arg2, int prev_pos) { int crnt_pos = 0; int result; /* stuff happens to arg1, arg2, and crnt_pos */
0
7499
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.