473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

''''''''''''''The class needs to be called clsRAS, paste the following
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
Private Const RDEOPT_IgnoreModemSpeaker = 4&
Private Const RDEOPT_SetModemSpeaker = &H8S

Private mvarprevioushandle As IntPtr

Public Declare Auto Function RasGetErrorString Lib "rasapi32.dll"
(ByVal uErrorValue As Integer, ByVal lpszErrorString As String, ByVal
cBufSize As Integer) As Integer

<StructLayout(LayoutKind.Sequential, Pack:=1,
CharSet:=CharSet.Auto)> _
Private Structure RASDIALEXTENSIONS
Public dwSize As Integer
Public dwfOptions As Integer
Public hwndParent As Integer
Public Reserved As Integer
End Structure

<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 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.dll" (ByRef
lpRasDialExtensions As RASDIALEXTENSIONS, 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.dll" (ByVal
hRasConn 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:=RAS_MaxEntryName + 1)> Public szEntryname As String
<MarshalAs(UnmanagedType.ByValTStr,
sizeconst:=RAS_MaxDeviceType + 1)> Public szDeviceType As String
<MarshalAs(UnmanagedType.ByValTStr,
sizeconst:=RAS_MaxDeviceName + 1)> 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 Property PreviousHandle() As IntPtr
Get
Return mvarprevioushandle
End Get
Set(ByVal Value As IntPtr)
mvarprevioushandle = Value
End Set
End Property
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)
ElseIf ret <> 0 Then
Throw New Exception(GetRasError(ret))
End If
If ret = 0 And lpcentries > 0 Then
ReDim entries(lpcentries - 1)
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
pentry = Nothing
End If

Marshal.FreeHGlobal(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.AllocHGlobal(structsize)
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(bufsize))
Marshal.WriteInt32(bufptr, structsize)
retcode = RasEnumConnections(bufptr, bufsize, realcount)
ElseIf retcode <> 0 Then
Throw New Exception(GetRasError(retcode))
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
runptr = Nothing
End If

Marshal.FreeHGlobal(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 mvarRasExtension As New RASDIALEXTENSIONS()
Dim hRASConn As New IntPtr()

With mvarRasExtension
.hwndParent = 0&
.Reserved = 0
.dwfOptions = RDEOPT_IgnoreModemSpeaker
.dwSize = Marshal.SizeOf(GetType(RASDIALEXTENSIONS))
End With

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

Dim intRet As Integer = RasDial(mvarRasExtension, Nothing,
objRASParams, 1, mcallback, hRASConn)
If intRet <> 0 Then
Dim errorstring As String = GetRasError(intRet)
If errorstring.IndexOf("already being dialed") Then
If hRASConn.Equals(IntPtr.Zero) = False Then
RasHangUp(hRASConn)
If mvarprevioushandle.Equals(IntPtr.Zero) = False Then
RasHangUp(mvarprevioushandle)
End If
Throw New Exception(errorstring)
End If

DialEntry = hRASConn

objRASParams = Nothing
hRASConn = Nothing
mvarRasExtension = 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.AllocHGlobal(structsize)
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(bufsize))
Marshal.WriteInt2(bufptr, structsize)
retcode = RasEnumConnections(bufptr, bufsize, realcount)
ElseIf retcode <> 0 Then
Throw New Exception(GetRasError(retcode))
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

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

runptr = Nothing
End If

Marshal.FreeHGlobal(bufptr)

bufptr = Nothing
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 entrycount As Integer
Dim entries() As RASCONN

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

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

If retcode = ERROR_BUFFER_TOO_SMALL Then
bufptr = Marshal.ReAllocHGlobal(bufptr, New
IntPtr(bufsize))
Marshal.WriteInt32(bufptr, structsize)
retcode = RasEnumConnections(bufptr, bufsize, entrycount)
ElseIf retcode <> 0 Then
Throw New Exception(GetRasError(retcode))
End If

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

For i = 0 To (entrycount - 1)
entries(i) = Marshal.PtrToStructure(runptr,
structtype)
runptr = New IntPtr(runptr.ToInt32 + structsize)
Next

runptr = Nothing

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

End If

Marshal.FreeHGlobal(bufptr)

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

Dim sErrMsg As New String(Space(512))
Dim lret As Integer

lret = ras.RasGetErrorString(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.myrasdialfunc = AddressOf
VPNRasDialFunc
Private mvarispcallback As ras.myrasdialfunc = AddressOf
ISPRasDialFunc
Private mvarProgramName As String
Private mvarvpnconnectionname As String
Private mvarvpnusername As String
Private mvarvpnpassword As String
Private mvarvpnretryinterval As Integer
Private mvarispconnectionname As String
Private mvarispusername As String
Private mvarisppassword As String
Private mvarispretryinterval As Integer
Private mvarvpnprevhandle As IntPtr
Private mvarispprevhandle 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.WriteLine("DF: " & unMsg.ToString & " - " &
rasconnstate.ToString & " - " & dwError.ToString)
'If dwError <> 0 Then Debug.WriteLine(GetRasError(dwError))
Try
If dwError <> 0 Then
ras.RasHangUp(hrasconn)
Throw New System.Exception(GetRasError(dwError))
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.WriteLine("DF: " & unMsg.ToString & " - " &
rasconnstate.ToString & " - " & dwError.ToString)
'If dwError <> 0 Then Debug.WriteLine(GetRasError(dwError))
Try
If dwError <> 0 Then
ras.RasHangUp(hrasconn)
Throw New System.Exception(GetRasError(dwError))
End If
Catch exp As Exception
ErrorToXML(exp)
End Try

End Function
''''''''''''''Fill in all the usernames and passwords as appropriate
Private Sub GetRegistrySettings()

Try
mvarvpnusername = "VPNUserName"
mvarvpnconnectionname = "VPNConnectionName"
mvarvpnpassword = "VPNPassword"
mvarvpnretryinterval = "VPNRetryInterval"
mvarispusername = "ISPUserName"
mvarispconnectionname = "ISPConnectionName"
mvarisppassword = "ISPPassowrd"
Catch exp As Exception
ErrorToXML(exp)
Finally
mvarcregistry = Nothing
End Try
End Sub
Private Sub ErrorToXML(ByVal 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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If (UBound(Diagnostics.Process.GetProcessesByName(Dia gnostics.Process.GetCurrentProcess.ProcessName))
0) = True Then

Me.Close()
End If
mvarProgramName = System.Windows.Forms.Application.ProductName
& " " & System.Windows.Forms.Application.ProductVersion
Try
GetRegistrySettings()
Catch exp As Exception
ErrorToXML(exp)
End Try

End Sub

''''''''''''This 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.EventArgs) Handles tmrDialer.Tick
Dim mvarVPNRas As New ras(), mvarISPRAS As New ras(),
mvartemphandle As New IntPtr()

Try
If mvarispretryinterval > 0 Then
If Now.Minute Mod mvarispretryinterval = 0 Then
If mvarISPRAS.IsConnected(mvarispconnectionname) =
False Then
If mvarispprevhandle.Equals(IntPtr.Zero) =
False Then mvarISPRAS.PreviousHandle = mvarispprevhandle
mvartemphandle =
mvarISPRAS.DialEntry(mvarispconnectionname, mvarispusername,
mvarisppassword, mvarispcallback)
If mvartemphandle.Equals(IntPtr.Zero) = False
Then mvarispprevhandle = mvartemphandle
End If
End If
End If
Catch exp As Exception
ErrorToXML(exp)
End Try

Try
If mvarvpnretryinterval > 0 Then
If Now.Minute Mod mvarvpnretryinterval = 0 Then
If mvarVPNRas.IsConnected(mvarvpnconnectionname) =
False Then
If mvarvpnprevhandle.Equals(IntPtr.Zero) =
False Then mvarVPNRas.PreviousHandle = mvarvpnprevhandle
mvartemphandle =
mvarVPNRas.DialEntry(mvarvpnconnectionname, mvarvpnusername,
mvarvpnpassword, mvarvpncallback)
If mvartemphandle.Equals(IntPtr.Zero) = False
Then mvarvpnprevhandle = 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(ByVal dwerror As Integer) As String

Dim sErrMsg As New String(Space(512))
Dim lret As Integer

lret = ras.RasGetErrorString(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 2965
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 RasEnumConnections function - I still have some problems
with this...
Please help,
zurg
Uzytkownik "Black Ninja" <zm****@hotmail.com> napisal w wiadomosci
news:ba**************************@posting.google.c om...
''''''''''''''The class needs to be called clsRAS, paste the following
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
Private Const RDEOPT_IgnoreModemSpeaker = 4&
Private Const RDEOPT_SetModemSpeaker = &H8S

Private mvarprevioushandle As IntPtr

Public Declare Auto Function RasGetErrorString Lib "rasapi32.dll"
(ByVal uErrorValue As Integer, ByVal lpszErrorString As String, ByVal
cBufSize As Integer) As Integer

<StructLayout(LayoutKind.Sequential, Pack:=1,
CharSet:=CharSet.Auto)> _
Private Structure RASDIALEXTENSIONS
Public dwSize As Integer
Public dwfOptions As Integer
Public hwndParent As Integer
Public Reserved As Integer
End Structure

<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 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.dll" (ByRef
lpRasDialExtensions As RASDIALEXTENSIONS, 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.dll" (ByVal
hRasConn 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:=RAS_MaxEntryName + 1)> Public szEntryname As String
<MarshalAs(UnmanagedType.ByValTStr,
sizeconst:=RAS_MaxDeviceType + 1)> Public szDeviceType As String
<MarshalAs(UnmanagedType.ByValTStr,
sizeconst:=RAS_MaxDeviceName + 1)> 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 Property PreviousHandle() As IntPtr
Get
Return mvarprevioushandle
End Get
Set(ByVal Value As IntPtr)
mvarprevioushandle = Value
End Set
End Property
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)
ElseIf ret <> 0 Then
Throw New Exception(GetRasError(ret))
End If
If ret = 0 And lpcentries > 0 Then
ReDim entries(lpcentries - 1)
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
pentry = Nothing
End If

Marshal.FreeHGlobal(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.AllocHGlobal(structsize)
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(bufsize))
Marshal.WriteInt32(bufptr, structsize)
retcode = RasEnumConnections(bufptr, bufsize, realcount)
ElseIf retcode <> 0 Then
Throw New Exception(GetRasError(retcode))
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
runptr = Nothing
End If

Marshal.FreeHGlobal(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 mvarRasExtension As New RASDIALEXTENSIONS()
Dim hRASConn As New IntPtr()

With mvarRasExtension
.hwndParent = 0&
.Reserved = 0
.dwfOptions = RDEOPT_IgnoreModemSpeaker
.dwSize = Marshal.SizeOf(GetType(RASDIALEXTENSIONS))
End With

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

Dim intRet As Integer = RasDial(mvarRasExtension, Nothing,
objRASParams, 1, mcallback, hRASConn)
If intRet <> 0 Then
Dim errorstring As String = GetRasError(intRet)
If errorstring.IndexOf("already being dialed") Then
If hRASConn.Equals(IntPtr.Zero) = False Then
RasHangUp(hRASConn)
If mvarprevioushandle.Equals(IntPtr.Zero) = False Then
RasHangUp(mvarprevioushandle)
End If
Throw New Exception(errorstring)
End If

DialEntry = hRASConn

objRASParams = Nothing
hRASConn = Nothing
mvarRasExtension = 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.AllocHGlobal(structsize)
Marshal.WriteInt32(bufptr, structsize)

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

If retcode = ERROR_BUFFER_TOO_SMALL Then
bufptr = Marshal.ReAllocHGlobal(bufptr, New
IntPtr(bufsize))
Marshal.WriteInt32(bufptr, structsize)
retcode = RasEnumConnections(bufptr, bufsize, realcount)
ElseIf retcode <> 0 Then
Throw New Exception(GetRasError(retcode))
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

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

runptr = Nothing
End If

Marshal.FreeHGlobal(bufptr)

bufptr = Nothing
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 entrycount As Integer
Dim entries() As RASCONN

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

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

If retcode = ERROR_BUFFER_TOO_SMALL Then
bufptr = Marshal.ReAllocHGlobal(bufptr, New
IntPtr(bufsize))
Marshal.WriteInt32(bufptr, structsize)
retcode = RasEnumConnections(bufptr, bufsize, entrycount)
ElseIf retcode <> 0 Then
Throw New Exception(GetRasError(retcode))
End If

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

For i = 0 To (entrycount - 1)
entries(i) = Marshal.PtrToStructure(runptr,
structtype)
runptr = New IntPtr(runptr.ToInt32 + structsize)
Next

runptr = Nothing

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

End If

Marshal.FreeHGlobal(bufptr)

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

Dim sErrMsg As New String(Space(512))
Dim lret As Integer

lret = ras.RasGetErrorString(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.myrasdialfunc = AddressOf
VPNRasDialFunc
Private mvarispcallback As ras.myrasdialfunc = AddressOf
ISPRasDialFunc
Private mvarProgramName As String
Private mvarvpnconnectionname As String
Private mvarvpnusername As String
Private mvarvpnpassword As String
Private mvarvpnretryinterval As Integer
Private mvarispconnectionname As String
Private mvarispusername As String
Private mvarisppassword As String
Private mvarispretryinterval As Integer
Private mvarvpnprevhandle As IntPtr
Private mvarispprevhandle 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.WriteLine("DF: " & unMsg.ToString & " - " &
rasconnstate.ToString & " - " & dwError.ToString)
'If dwError <> 0 Then Debug.WriteLine(GetRasError(dwError))
Try
If dwError <> 0 Then
ras.RasHangUp(hrasconn)
Throw New System.Exception(GetRasError(dwError))
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.WriteLine("DF: " & unMsg.ToString & " - " &
rasconnstate.ToString & " - " & dwError.ToString)
'If dwError <> 0 Then Debug.WriteLine(GetRasError(dwError))
Try
If dwError <> 0 Then
ras.RasHangUp(hrasconn)
Throw New System.Exception(GetRasError(dwError))
End If
Catch exp As Exception
ErrorToXML(exp)
End Try

End Function
''''''''''''''Fill in all the usernames and passwords as appropriate
Private Sub GetRegistrySettings()

Try
mvarvpnusername = "VPNUserName"
mvarvpnconnectionname = "VPNConnectionName"
mvarvpnpassword = "VPNPassword"
mvarvpnretryinterval = "VPNRetryInterval"
mvarispusername = "ISPUserName"
mvarispconnectionname = "ISPConnectionName"
mvarisppassword = "ISPPassowrd"
Catch exp As Exception
ErrorToXML(exp)
Finally
mvarcregistry = Nothing
End Try
End Sub
Private Sub ErrorToXML(ByVal 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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If

(UBound(Diagnostics.Process.GetProcessesByName(Dia gnostics.Process.GetCurren
tProcess.ProcessName))
0) = True Then

Me.Close()
End If
mvarProgramName = System.Windows.Forms.Application.ProductName
& " " & System.Windows.Forms.Application.ProductVersion
Try
GetRegistrySettings()
Catch exp As Exception
ErrorToXML(exp)
End Try

End Sub

''''''''''''This 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.EventArgs) Handles tmrDialer.Tick
Dim mvarVPNRas As New ras(), mvarISPRAS As New ras(),
mvartemphandle As New IntPtr()

Try
If mvarispretryinterval > 0 Then
If Now.Minute Mod mvarispretryinterval = 0 Then
If mvarISPRAS.IsConnected(mvarispconnectionname) =
False Then
If mvarispprevhandle.Equals(IntPtr.Zero) =
False Then mvarISPRAS.PreviousHandle = mvarispprevhandle
mvartemphandle =
mvarISPRAS.DialEntry(mvarispconnectionname, mvarispusername,
mvarisppassword, mvarispcallback)
If mvartemphandle.Equals(IntPtr.Zero) = False
Then mvarispprevhandle = mvartemphandle
End If
End If
End If
Catch exp As Exception
ErrorToXML(exp)
End Try

Try
If mvarvpnretryinterval > 0 Then
If Now.Minute Mod mvarvpnretryinterval = 0 Then
If mvarVPNRas.IsConnected(mvarvpnconnectionname) =
False Then
If mvarvpnprevhandle.Equals(IntPtr.Zero) =
False Then mvarVPNRas.PreviousHandle = mvarvpnprevhandle
mvartemphandle =
mvarVPNRas.DialEntry(mvarvpnconnectionname, mvarvpnusername,
mvarvpnpassword, mvarvpncallback)
If mvartemphandle.Equals(IntPtr.Zero) = False
Then mvarvpnprevhandle = 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(ByVal dwerror As Integer) As String

Dim sErrMsg As New String(Space(512))
Dim lret As Integer

lret = ras.RasGetErrorString(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.google.c om...
''''''''''''''The 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
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...
0
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....
3
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...
15
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,...
41
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
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#...
9
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...
22
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....
23
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; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.