T
TJoker .NET
Hi all.
I'm trying to programmatically connect to a remote network using dialup.
I've found the following code in C# from someone at MS.
http://groups.google.com/groups?selm=An87uzi#BHA.1532@cpmsftngxa08
I converted it to VB.NET using this utility:
http://csharpconverter.claritycon.com/Default.aspx
but the result code had some minor compilation errors. I think I fixed the
errors manually but when I try to connect using the code I get a runtime
error (immediately, without even trying to dial) and the app crashes. The
callback routine is invoked only once with the following parameters just
before the crash:
unMsg: 52429
rasconnstate: 0
dwError: 0
I have a valid dialup entry. Has anyone been through this ?
Here's the class I'm using:
------------------------------------------------------------------
Imports System
Imports System.Runtime.InteropServices
Namespace RAS
Public Class RasManager
Public Const RAS_MaxEntryName As Integer = 256
Public Const RAS_MaxPhoneNumber As Integer = 128
Public Const UNLEN As Integer = 256
Public Const PWLEN As Integer = 256
Public Const DNLEN As Integer = 15
Public Const MAX_PATH As Integer = 260
Public Const RAS_MaxDeviceType As Integer = 16
Public Const RAS_MaxCallbackNumber As Integer = RAS_MaxPhoneNumber
Delegate Sub Callback(ByVal unMsg As System.UInt32, ByVal
rasconnstate As Integer, ByVal dwError As Integer) 'ToDo: Unsigned Integers
not supported
<StructLayout(LayoutKind.Sequential, Pack:=4,
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
Public dwSubEntry As Integer
Public dwCallbackId As Integer
End Structure 'RASDIALPARAMS
'<DllImport("rasapi32.dll", CharSet:=CharSet.Auto)> _
Public Declare Auto Function RasDial Lib "rasapi32.dll" (ByVal
lpRasDialExtensions As Integer, ByVal lpszPhonebook As String, ByRef
lprasdialparams As RASDIALPARAMS, ByVal dwNotifierType As Integer, ByVal
lpvNotifier As Callback, ByRef lphRasConn As Integer) As Integer
Private RasDialPar As RASDIALPARAMS
Private Connection As Integer
Public Sub New()
Connection = 0
RasDialPar = New RASDIALPARAMS()
RasDialPar.dwSize = Marshal.SizeOf(RasDialPar)
End Sub 'New
#Region "Properties"
Public Property UserName() As String
Get
Return RasDialPar.szUserName
End Get
Set(ByVal Value As String)
RasDialPar.szUserName = Value
End Set
End Property
Public Property Password() As String
Get
Return RasDialPar.szPassword
End Get
Set(ByVal Value As String)
RasDialPar.szPassword = Value
End Set
End Property
Public Property EntryName() As String
Get
Return RasDialPar.szEntryName
End Get
Set(ByVal Value As String)
RasDialPar.szEntryName = Value
End Set
End Property
#End Region
Public Function Connect() As Integer
Dim rasDialFunc As New Callback(AddressOf
RasManager.RasDialFunc)
RasDialPar.szEntryName += ControlChars.NullChar
RasDialPar.szUserName += ControlChars.NullChar
RasDialPar.szPassword += ControlChars.NullChar
Dim result As Integer = RasDial(0, Nothing, RasDialPar, 0,
rasDialFunc, Connection)
Return result
End Function 'Connect
Public Shared Sub RasDialFunc(ByVal unMsg As System.UInt32, ByVal
rasconnstate As Integer, ByVal dwError As Integer) 'ToDo: Unsigned Integers
not supported
Debug.WriteLine("Callback function ----------------")
Debug.WriteLine(" unMsg: " & unMsg.ToString)
Debug.WriteLine(" rasconnstate: " & rasconnstate)
Debug.WriteLine(" dwError: " & dwError)
Debug.WriteLine("----------------------------------")
End Sub 'RasDialFunc
End Class 'RasManager
End Namespace 'RAS
-------------------------------------------------
The code calling it:
Dim myRas As New RAS.RasManager()
myRas.EntryName = "myentry"
myRas.UserName = "myuser"
myRas.Password = ""
Dim res As Integer = myRas.Connect().ToString()
Debug.WriteLine("result = " & res) 'prints 0
Thanks a lot
TJ!
I'm trying to programmatically connect to a remote network using dialup.
I've found the following code in C# from someone at MS.
http://groups.google.com/groups?selm=An87uzi#BHA.1532@cpmsftngxa08
I converted it to VB.NET using this utility:
http://csharpconverter.claritycon.com/Default.aspx
but the result code had some minor compilation errors. I think I fixed the
errors manually but when I try to connect using the code I get a runtime
error (immediately, without even trying to dial) and the app crashes. The
callback routine is invoked only once with the following parameters just
before the crash:
unMsg: 52429
rasconnstate: 0
dwError: 0
I have a valid dialup entry. Has anyone been through this ?
Here's the class I'm using:
------------------------------------------------------------------
Imports System
Imports System.Runtime.InteropServices
Namespace RAS
Public Class RasManager
Public Const RAS_MaxEntryName As Integer = 256
Public Const RAS_MaxPhoneNumber As Integer = 128
Public Const UNLEN As Integer = 256
Public Const PWLEN As Integer = 256
Public Const DNLEN As Integer = 15
Public Const MAX_PATH As Integer = 260
Public Const RAS_MaxDeviceType As Integer = 16
Public Const RAS_MaxCallbackNumber As Integer = RAS_MaxPhoneNumber
Delegate Sub Callback(ByVal unMsg As System.UInt32, ByVal
rasconnstate As Integer, ByVal dwError As Integer) 'ToDo: Unsigned Integers
not supported
<StructLayout(LayoutKind.Sequential, Pack:=4,
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
Public dwSubEntry As Integer
Public dwCallbackId As Integer
End Structure 'RASDIALPARAMS
'<DllImport("rasapi32.dll", CharSet:=CharSet.Auto)> _
Public Declare Auto Function RasDial Lib "rasapi32.dll" (ByVal
lpRasDialExtensions As Integer, ByVal lpszPhonebook As String, ByRef
lprasdialparams As RASDIALPARAMS, ByVal dwNotifierType As Integer, ByVal
lpvNotifier As Callback, ByRef lphRasConn As Integer) As Integer
Private RasDialPar As RASDIALPARAMS
Private Connection As Integer
Public Sub New()
Connection = 0
RasDialPar = New RASDIALPARAMS()
RasDialPar.dwSize = Marshal.SizeOf(RasDialPar)
End Sub 'New
#Region "Properties"
Public Property UserName() As String
Get
Return RasDialPar.szUserName
End Get
Set(ByVal Value As String)
RasDialPar.szUserName = Value
End Set
End Property
Public Property Password() As String
Get
Return RasDialPar.szPassword
End Get
Set(ByVal Value As String)
RasDialPar.szPassword = Value
End Set
End Property
Public Property EntryName() As String
Get
Return RasDialPar.szEntryName
End Get
Set(ByVal Value As String)
RasDialPar.szEntryName = Value
End Set
End Property
#End Region
Public Function Connect() As Integer
Dim rasDialFunc As New Callback(AddressOf
RasManager.RasDialFunc)
RasDialPar.szEntryName += ControlChars.NullChar
RasDialPar.szUserName += ControlChars.NullChar
RasDialPar.szPassword += ControlChars.NullChar
Dim result As Integer = RasDial(0, Nothing, RasDialPar, 0,
rasDialFunc, Connection)
Return result
End Function 'Connect
Public Shared Sub RasDialFunc(ByVal unMsg As System.UInt32, ByVal
rasconnstate As Integer, ByVal dwError As Integer) 'ToDo: Unsigned Integers
not supported
Debug.WriteLine("Callback function ----------------")
Debug.WriteLine(" unMsg: " & unMsg.ToString)
Debug.WriteLine(" rasconnstate: " & rasconnstate)
Debug.WriteLine(" dwError: " & dwError)
Debug.WriteLine("----------------------------------")
End Sub 'RasDialFunc
End Class 'RasManager
End Namespace 'RAS
-------------------------------------------------
The code calling it:
Dim myRas As New RAS.RasManager()
myRas.EntryName = "myentry"
myRas.UserName = "myuser"
myRas.Password = ""
Dim res As Integer = myRas.Connect().ToString()
Debug.WriteLine("result = " & res) 'prints 0
Thanks a lot
TJ!