[LONG] Ras Connection - Code example

P

Paolo Gazzei

Since my post, i've been asked for code many times. So, i decided to open a
new post with my code. It's not "perfect" and a little bit dirty, but it
works fine in 3 of my applications. I do not have time to improve it, so
take it "as is".
It has only 2 functions: Dial and Hangup. You have to save a hRasConn
Integer to keep connection reference.
This only dial an existing connection, therefore you have to create it
manually.


Hope this helps!


' memory class, taken by msdn example (advanced P/Invoke)
Imports System.Runtime.InteropServices
Imports System.ComponentModel
Public Class Memory
<DllImport("coredll.dll", SetLastError:=True)> _
Private Shared Function LocalAlloc(ByVal uFlags As Integer, _
ByVal uBytes As Integer) As IntPtr
End Function

<DllImport("coredll.dll", SetLastError:=True)> _
Private Shared Function LocalFree(ByVal hMem As IntPtr) As IntPtr
End Function

<DllImport("coredll.dll", SetLastError:=True)> _
Private Shared Function LocalReAlloc(ByVal hMem As IntPtr, _
ByVal uBytes As Integer, ByVal fuFlags As Integer) As IntPtr
End Function

Private Const LMEM_FIXED As Integer = 0
Private Const LMEM_MOVEABLE As Integer = 2
Private Const LMEM_ZEROINIT As Integer = &H40
Private Const LPTR = (LMEM_FIXED Or LMEM_ZEROINIT)

' Allocates a block of memory using LocalAlloc
Public Shared Function AllocHLocal(ByVal cb As Integer) As IntPtr
Return LocalAlloc(LPTR, cb)
End Function

' Frees memory allocated by AllocHLocal
Public Shared Sub FreeHLocal(ByVal hlocal As IntPtr)
If Not hlocal.Equals(IntPtr.Zero) Then
If Not IntPtr.Zero.Equals(LocalFree(hlocal)) Then
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
hlocal = IntPtr.Zero
End If
End Sub

' Resizes a block of memory previously allocated with AllocHLocal
Public Shared Function ReAllocHLocal(ByVal pv As IntPtr, _
ByVal cb As Integer) As IntPtr
Dim newMem As IntPtr = LocalReAlloc(pv, cb, LMEM_MOVEABLE)
If newMem.Equals(IntPtr.Zero) Then
Throw New OutOfMemoryException
End If
Return newMem
End Function

' Copies the contents of a managed string to unmanaged memory
Public Shared Function StringToHLocalUni( _
ByVal s As String) As IntPtr
If s Is Nothing Then
Return IntPtr.Zero
Else
Dim nc As Integer = s.Length
Dim len As Integer = 2 * (1 + nc)
Dim hLocal As IntPtr = AllocHLocal(len)
If hLocal.Equals(IntPtr.Zero) Then
Throw New OutOfMemoryException
Else
Marshal.Copy(s.ToCharArray(), 0, hLocal, s.Length)
Return hLocal
End If
End If
End Function
End Class

'class RAS
Imports System.Runtime.InteropServices
Class RAS
#Region "funzioni_importate"

<DllImport("coredll.dll", SetLastError:=True)> _
Private Shared Function RasDial( _
ByVal dialExtensions As Integer, _
ByVal phoneBookPath As Integer, _
ByVal rasDialParam As IntPtr, _
ByVal NotifierType As Integer, _
ByVal notifier As Integer, _
ByRef hRasConn As Integer _
) As Integer
End Function
<DllImport("coredll.dll", SetLastError:=True)> _
Private Shared Function RasHangUp(ByVal hRasConn As Integer) As Integer
End Function
Private Structure rasDialParams
Public dwSize As Integer
End Structure
#End Region
Public Shared Function Dial(ByVal szRasConnection As String, ByVal
szUsername As String, ByVal szPassword As String, ByRef hRasConnection As
Integer) As Boolean
Dim p As New rasDialParams
Dim dwSize As Integer
Dim strPointer As IntPtr
Dim bRasConnection(szRasConnection.Length) As Char
Dim offset As IntPtr
Dim hrasconn As Integer
Dim bUsername(szUsername.Length) As Char
Dim bPassword(szPassword.Length) As Char
Dim i As Integer
Dim ret As Integer

dwSize = Marshal.SizeOf(p)
dwSize += Marshal.SystemDefaultCharSize * 21
dwSize += Marshal.SystemDefaultCharSize * 129
dwSize += Marshal.SystemDefaultCharSize * 49
dwSize += Marshal.SystemDefaultCharSize * 257
dwSize += Marshal.SystemDefaultCharSize * 257
dwSize += Marshal.SystemDefaultCharSize * 16

bRasConnection = szRasConnection.ToCharArray
bUsername = szUsername.ToCharArray
bPassword = szPassword.ToCharArray

strPointer = Memory.AllocHLocal(dwSize)
offset = New IntPtr(strPointer.ToInt32() + Marshal.SizeOf(p))
Marshal.Copy(bRasConnection, 0, offset, szRasConnection.Length)
offset = New IntPtr(offset.ToInt32 + Marshal.SystemDefaultCharSize *
21)
Marshal.Copy("".ToCharArray, 0, offset, "".Length)
offset = New IntPtr(offset.ToInt32 + Marshal.SystemDefaultCharSize *
129)
Marshal.Copy("".ToCharArray, 0, offset, "".Length)
offset = New IntPtr(offset.ToInt32 + Marshal.SystemDefaultCharSize *
49)
Marshal.Copy(bUsername, 0, offset, szUsername.Length)
offset = New IntPtr(offset.ToInt32 + Marshal.SystemDefaultCharSize *
257)
Marshal.Copy(bPassword, 0, offset, szPassword.Length)
offset = New IntPtr(offset.ToInt32 + Marshal.SystemDefaultCharSize *
257)
Marshal.Copy("".ToCharArray, 0, offset, "".Length)

Try
ret = RasDial(0, 0, strPointer, 0, 0, hrasconn)
If ret = 0 Then
hRasConnection = hrasconn
Return (True)
Else
Return (False)
End If
Catch ex As Exception
Return (False)
End Try

End Function
Public Shared Function HangUp(ByVal hrasconn As Integer)
Dim ret As Integer

Try
ret = RasHangUp(hrasconn)
If ret = 0 Then
Return (True)
Else
Return (False)
End If
Catch ex As Exception
Return (False)
End Try
End Function
End Class
 
T

Trollpower

Thank you very much for your provided code. But as always theres
someone with stupid questions. This time its me... again.

Can you give an example on how to use this code? Especially what i
have to use for the parameters in Dial. Where can i get the password,
username and RASConnection-String? Im really new to this stuff, so any
help is appreciated.

Thanks in advance

Greets

Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top