Solved

Need a Better Way Of Checking INternet Connection

Posted on 2004-09-19
20
524 Views
Last Modified: 2008-03-06
This code detects if the internet conneciton has been lost. However once it reconnects it seems to lose the ability to find a server. This is a vb.net windows program.

I tested it by starting the program, observing the connected icon appear. I then pulled the cable, waitied until the "Connected" icon appeared. I clicked Login, and it immediatley failed. If I dont pull the cable and just login normally everything executes fine.

Can anyone make a better suggestion of how to handle the internet check better? This operates in a seperate thread.

Public Sub ConnectionCheck()
               Do
            Dim porttest As New TcpClient
            Try
                porttest.Connect(arrgen.ServerIPAddress, "23")
                porttest.GetStream()
                Label19.Visible = True
                Label20.Visible = False
                porttest.Close()
                t1.Sleep(150)
            Catch
                Label19.Visible = False
                Label20.Visible = True
                porttest.Close()
                t1.Sleep(150)
            End Try
        Loop
    End Sub

0
Comment
Question by:scorpion53061
  • 7
  • 7
  • 6
20 Comments
 
LVL 18

Expert Comment

by:armoghan
Comment Utility
0
 

Author Comment

by:scorpion53061
Comment Utility
Do you know how to convert this to vb.net?

My converters are not accepting it.
0
 
LVL 18

Expert Comment

by:armoghan
Comment Utility
you may be able to find the speed using performancemonitor
http://www.codeproject.com/csharp/NetworkMonitorl.asp
0
 
LVL 18

Expert Comment

by:armoghan
Comment Utility
0
 
LVL 18

Accepted Solution

by:
armoghan earned 250 total points
Comment Utility
0
 
LVL 5

Assisted Solution

by:ajitanand
ajitanand earned 250 total points
Comment Utility
I use the foll. class in my applications, and it works fine!

Public Class ConnectionChecker

        Private Declare Auto Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Integer, ByVal dwReserved As Integer) As Boolean

        Public Function IsInternetConnection() As Boolean
                Dim flag As Integer = 1
                Return InternetGetConnectedState(flag, 0)
        End Function
End Class

rgds,
Ajit Anand
0
 

Author Comment

by:scorpion53061
Comment Utility
does this work with dialup too?
0
 
LVL 5

Expert Comment

by:ajitanand
Comment Utility
Yes! This works with dialup too.

Even in my application, I first check to see if Internet connection exists or not through this class. If it does not, I bring up the dialup dialog to connect, through another class (RASManager).

rgds,
Ajit Anand
0
 

Author Comment

by:scorpion53061
Comment Utility
Could I see that please? That would be helpful.

You are going to get the points of course. And of course thank you very much.
0
 
LVL 18

Expert Comment

by:armoghan
Comment Utility
My first and third link are almost the same as the code send by ajitanand

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:ajitanand
Comment Utility
hi

Here is how I use the class (the class is very long, and has a lot of functions for use and is given below):
---------------------------------
Try
      'Check if connected to the Internet, if not then dial the default connection
      If Not MyObject.IsInternetConnection Then
            Try
                  'Simple way to dial a connection
                  Dim Manager As New RAS.RasManager()
                  Manager.InternetDial(Me.Handle, "")

            Catch Ex As RAS.RASException
                  If ex.ErrorCode = RAS.RASException.RASERROR.ERROR_USER_DISCONNECTION Then
                        Exit Sub
                  Else
                        Throw ex 'Let the global exception handler take care of this
                  End If
            End Try
      End If

Catch ex2 AS Exception
      
End Try

--------------------------------------------------------------


Option Strict Off

Imports System
Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.StructLayoutAttribute
Imports QuickGeneric.RAS.RASException

Namespace RAS
    Public Class RasManager

        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_MaxCallbackNumber As Integer = RAS_MaxPhoneNumber

        Public Delegate Sub Callback(ByVal unMsg As System.UInt32, ByVal rasconnstate As Integer, ByVal dwError As Integer)

        <StructLayout(LayoutKind.Sequential, Pack:=4, CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
        Private 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
            '#if (WINVER >= 0x401)
            Public dwSubEnTry As Integer
            Public dwCallbackId As Integer
            '#end if
        End Structure

        <StructLayout(LayoutKind.Sequential, CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
        Private Structure RASENTRYNAME
            Public dwSize As Int32
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxEnTryName + 1)> _
            Public szEntryName As String
            Public dwFlags As Int32
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH + 1)> _
            Public szPhonebookPath As String
        End Structure

        Private Declare Auto Function APIRasEnumEntries Lib "rasapi32.dll" Alias "RasEnumEntries" ( _
            ByVal reserved As String, _
            ByVal lpszPhonebook As String, _
            ByVal lpRasEntryName As IntPtr, _
            ByRef lpcb As Integer, _
            ByRef lpcEntries As Integer _
        ) As Integer


        Private 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 Declare Auto Function RasCreatePhonebookEntry Lib "rasapi32.dll" (ByVal hwnd As IntPtr, ByVal lpszPhonebook As String) As Integer
        Private Declare Auto Function RasEditPhonebookEntry Lib "rasapi32.dll" (ByVal hwnd As IntPtr, ByVal lpszPhonebook As String, ByVal lpszEntryName As String) As Integer
        Private Declare Auto Function InternetDial Lib "wininet.dll" (ByVal hwndParent As IntPtr, ByVal strEntryName As String, ByVal dwFlags As Integer, ByRef lpdwConnection As Integer, ByVal dwReserved As Integer) As Integer

        Private RasDialParam As RASDIALPARAMS
        Private Connection As Integer

        Public Enum RASCONNSTATE
            RAS_UNUSED = -1
            RASCS_OpenPort = 0
            RASCS_PortOpened = 1
            RASCS_ConnectDevice = 2
            RASCS_DeviceConnected = 3
            RASCS_AllDevicesConnected = 4
            RASCS_Authenticate = 5
            RASCS_AuthNotify
            RASCS_AuthRetry
            RASCS_AuthCallback
            RASCS_AuthChangePassword
            RASCS_AuthProject
            RASCS_AuthLinkSpeed
            RASCS_AuthAck
            RASCS_ReAuthenticate
            RASCS_Authenticated = 14
            RASCS_PrepareForCallback = 15
            RASCS_WaitForModemReset = 16
            RASCS_WaitForCallback = 17
            RASCS_Projected = 18
            '#if (WINVER >= 0x400)
            RASCS_StartAuthentication ' Windows 95 only
            RASCS_CallbackComplete ' Windows 95 only
            RASCS_LogonNetwork ' Windows 95 only
            '#endif
            RASCS_SubEntryConnected
            RASCS_SubEntryDisconnected
            RASCS_Interactive = &H1000
            RASCS_PAUSED = 4096
            RASCS_RetryAuthentication = 4097
            RASCS_CallbackSetByCaller = 4098
            RASCS_PasswordExpired = 4099
            '#if (WINVER >= 0x500)
            RASCS_InvokeEapUI
            '#endif
            RASCS_Connected = 8192
            RASCS_DONE = &H2000&
            RASCS_Disconnected = 8193
        End Enum

        <StructLayout(LayoutKind.Sequential, Pack:=1)> _
        Friend Structure RASCONNSTATUS
            Public dwSize As Integer
            Public rasconnstate As rasconnstate
            Public dwError As Integer
            Public szDeviceType As String
            Public szDeviceName As String
        End Structure

        Public Sub New()

            Connection = 0
            RasDialParam = New RASDIALPARAMS()
            RasDialParam.dwSize = Marshal.SizeOf(RasDialParam)

        End Sub

        Public Property UserName() As String
            Get
                Return RasDialParam.szUserName
            End Get
            Set(ByVal Value As String)
                RasDialParam.szUserName = Value
            End Set
        End Property

        Public Property Password() As String
            Get
                Return RasDialParam.szPassword
            End Get
            Set(ByVal Value As String)
                RasDialParam.szPassword = Value
            End Set
        End Property

        Public Property EntryName() As String
            Get
                Return RasDialParam.szEnTryName
            End Get
            Set(ByVal Value As String)
                RasDialParam.szEnTryName = Value
            End Set
        End Property

        Public Function Connect(ByVal rasDialFunc As Callback) As Integer
            RasDialParam.szEnTryName += Chr(0)
            RasDialParam.szUserName += Chr(0)
            RasDialParam.szPassword += Chr(0)
            Return RasDial(0, Nothing, RasDialParam, 0, rasDialFunc, Connection)
        End Function

        Public Function ConnectSync() As RASCONNSTATE
            RasDialParam.szEnTryName += Chr(0)
            RasDialParam.szUserName += Chr(0)
            RasDialParam.szPassword += Chr(0)
            RasDial(0, Nothing, RasDialParam, 0, Nothing, Connection)
            Return Connection
        End Function

        Public Function GetRasEntries() As String()

            'Dim entries() As RASENTRYNAME
            Dim RasEntryNameLen As Integer = Marshal.SizeOf(GetType(RASENTRYNAME))
            Dim lpCB As Integer = RasEntryNameLen
            Dim lpcEntries As Integer

            ' allocate memory from the unmanaged native heap
            ' as we cannot pass out an array of marshalled structures ByRef
            ' this needs to be freed when we have finished
            ' Start with a single item in the array

            Dim pArray As IntPtr = Marshal.AllocHGlobal(RasEntryNameLen)
            ' the following line sets RASENTRYNAME.dwSize
            Marshal.WriteInt32(pArray, RasEntryNameLen)

            Dim nnn As RASERROR = APIRasEnumEntries(Nothing, Nothing, pArray, lpCB, lpcEntries)
            If (nnn = RASERROR.ERROR_BUFFER_TOO_SMALL) Then
                'free the previous allocation
                Marshal.FreeHGlobal(pArray)
                'allocate another block of memory using the size returned in lpCB
                pArray = Marshal.AllocHGlobal(lpCB)
                'copy the size of a RASENTRYNAME structure to the first four bytes
                Marshal.WriteInt32(pArray, RasEntryNameLen)
                'call the api again
                nnn = APIRasEnumEntries(Nothing, Nothing, pArray, lpCB, lpcEntries)
            End If

            Dim rEntries() As String

            If (nnn = RASERROR.SUCCESS) Then

                Dim ii As Integer
                Dim rasEntry As RASENTRYNAME
                Dim pEntry As IntPtr = pArray

                ReDim rEntries(lpcEntries - 1)

                For ii = 0 To lpcEntries - 1

                    'loop through the  allocated block of memory and
                    'copy to a RASENTRYNAME structure
                    ' note: the System.Runtime.InteropServices.Marshal.PtrToStructure takes
                    ' either a Type or an Object as the second parameter, so we use GetType to

                    rasEntry = CType(Marshal.PtrToStructure(pEntry, GetType(RASENTRYNAME)), RASENTRYNAME)
                    rEntries(ii) = rasEntry.szEntryName
                    Debug.WriteLine(rasEntry.szPhonebookPath)

                    ' increment the pointer
                    pEntry = New IntPtr(pEntry.ToInt32 + RasEntryNameLen)

                Next

            End If

            Marshal.FreeHGlobal(pArray)

            If nnn <> RASERROR.SUCCESS Then
                Throw New RASException(nnn, GetRASErrorDescription(nnn))
            End If

            Return rEntries

        End Function

        Public Sub CreatePhoneBookEntry(ByVal Handle As IntPtr, ByVal PhoneBook As String)
            RasCreatePhonebookEntry(Handle, PhoneBook)
        End Sub

        Public Sub EditPhoneBookEntry(ByVal Handle As IntPtr, ByVal PhoneBook As String, ByVal EntryName As String)
            RasEditPhonebookEntry(Handle, PhoneBook, EntryName)
        End Sub

        Public Sub InternetDial(ByVal Handle As IntPtr, ByVal EntryName As String)
            Dim lpdwConnection As Integer
            Dim rValue As Integer
            rValue = InternetDial(Handle, EntryName, 1, lpdwConnection, 0)
            If rValue <> 0 Then
                Throw New RAS.RASException(rValue, RAS.RASException.GetRASErrorDescription(rValue))
            End If
        End Sub

    End Class

    Public Class RASException
        Inherits Exception

        Private eCode As RASERROR

        Public Sub New(ByVal ErrorCode As RASERROR, ByVal message As String)
            MyBase.New(message)
            eCode = ErrorCode
        End Sub

        Public ReadOnly Property ErrorCode() As RASERROR
            Get
                Return eCode
            End Get
        End Property

        Public Enum RASERROR
            ' RAS Control Error Codes
            '
            ' These error codes are returned by the LastError property and
            ' passed as an argument to the LastError event. These are the
            ' same codes returned by the RAS library
            '
            SUCCESS = 0
            ERROR_INVALID_PORT_HANDLE = 601
            ERROR_PORT_ALREADY_OPEN = 602
            ERROR_BUFFER_TOO_SMALL = 603
            ERROR_WRONG_INFO_SPECIFIED = 604
            ERROR_CANNOT_SET_PORT_INFO = 605
            ERROR_PORT_NOT_CONNECTED = 606
            ERROR_EVENT_INVALID = 607
            ERROR_DEVICE_DOES_NOT_EXIST = 608
            ERROR_DEVICETYPE_DOES_NOT_EXIST = 609
            ERROR_INVALID_BUFFER = 610
            ERROR_ROUTE_NOT_AVAILABLE = 611
            ERROR_ROUTE_NOT_ALLOCATED = 612
            ERROR_INVALID_COMPRESSION_SPECIFIED = 613
            ERROR_OUT_OF_BUFFERS = 614
            ERROR_PORT_NOT_FOUND = 615
            ERROR_ASYNC_REQUEST_PENDING = 616
            ERROR_ALREADY_DISCONNECTING = 617
            ERROR_PORT_NOT_OPEN = 618
            ERROR_PORT_DISCONNECTED = 619
            ERROR_NO_ENDPOINTS = 620
            ERROR_CANNOT_OPEN_PHONEBOOK = 621
            ERROR_CANNOT_LOAD_PHONEBOOK = 622
            ERROR_CANNOT_FIND_PHONEBOOK_ENTRY = 623
            ERROR_CANNOT_WRITE_PHONEBOOK = 624
            ERROR_CORRUPT_PHONEBOOK = 625
            ERROR_CANNOT_LOAD_STRING = 626
            ERROR_KEY_NOT_FOUND = 627
            ERROR_DISCONNECTION = 628
            ERROR_REMOTE_DISCONNECTION = 629
            ERROR_HARDWARE_FAILURE = 630
            ERROR_USER_DISCONNECTION = 631
            ERROR_INVALID_SIZE = 632
            ERROR_PORT_NOT_AVAILABLE = 633
            ERROR_CANNOT_PROJECT_CLIENT = 634
            ERROR_UNKNOWN = 635
            ERROR_WRONG_DEVICE_ATTACHED = 636
            ERROR_BAD_STRING = 637
            ERROR_REQUEST_TIMEOUT = 638
            ERROR_CANNOT_GET_LANA = 639
            ERROR_NETBIOS_ERROR = 640
            ERROR_SERVER_OUT_OF_RESOURCES = 641
            ERROR_NAME_EXISTS_ON_NET = 642
            ERROR_SERVER_GENERAL_NET_FAILURE = 643
            ERROR_AUTH_INTERNAL = 645
            ERROR_RESTRICTED_LOGON_HOURS = 646
            ERROR_ACCT_DISABLED = 647
            ERROR_PASSWD_EXPIRED = 648
            ERROR_NO_DIALIN_PERMISSION = 649
            ERROR_SERVER_NOT_RESPONDING = 650
            ERROR_FROM_DEVICE = 651
            ERROR_UNRECOGNIZED_RESPONSE = 652
            ERROR_MACRO_NOT_FOUND = 653
            ERROR_MACRO_NOT_DEFINED = 654
            ERROR_MESSAGE_MACRO_NOT_FOUND = 655
            ERROR_DEFAULTOFF_MACRO_NOT_FOUND = 656
            ERROR_FILE_COULD_NOT_BE_OPENED = 657
            ERROR_DEVICENAME_TOO_LONG = 658
            ERROR_DEVICENAME_NOT_FOUND = 659
            ERROR_NO_RESPONSES = 660
            ERROR_NO_COMMAND_FOUND = 661
            ERROR_WRONG_KEY_SPECIFIED = 662
            ERROR_UNKNOWN_DEVICE_TYPE = 663
            ERROR_ALLOCATING_MEMORY = 664
            ERROR_PORT_NOT_CONFIGURED = 665
            ERROR_DEVICE_NOT_READY = 666
            ERROR_READING_INI_FILE = 667
            ERROR_NO_CONNECTION = 668
            ERROR_BAD_USAGE_IN_INI_FILE = 669
            ERROR_READING_SECTIONNAME = 670
            ERROR_READING_DEVICETYPE = 671
            ERROR_READING_DEVICENAME = 672
            ERROR_READING_USAGE = 673
            ERROR_READING_MAXCONNECTBPS = 674
            ERROR_READING_MAXCARRIERBPS = 675
            ERROR_LINE_BUSY = 676
            ERROR_VOICE_ANSWER = 677
            ERROR_NO_ANSWER = 678
            ERROR_NO_CARRIER = 679
            ERROR_NO_DIALTONE = 680
            ERROR_IN_COMMAND = 681
            ERROR_WRITING_SECTIONNAME = 682
            ERROR_WRITING_DEVICETYPE = 683
            ERROR_WRITING_DEVICENAME = 684
            ERROR_WRITING_MAXCONNECTBPS = 685
            ERROR_WRITING_MAXCARRIERBPS = 686
            ERROR_WRITING_USAGE = 687
            ERROR_WRITING_DEFAULTOFF = 688
            ERROR_READING_DEFAULTOFF = 689
            ERROR_EMPTY_INI_FILE = 690
            ERROR_AUTHENTICATION_FAILURE = 691
            ERROR_PORT_OR_DEVICE = 692
            ERROR_NOT_BINARY_MACRO = 693
            ERROR_DCB_NOT_FOUND = 694
            ERROR_STATE_MACHINES_NOT_STARTED = 695
            ERROR_STATE_MACHINES_ALREADY_STARTED = 696
            ERROR_PARTIAL_RESPONSE_LOOPING = 697
            ERROR_UNKNOWN_RESPONSE_KEY = 698
            ERROR_RECV_BUF_FULL = 699
            ERROR_CMD_TOO_LONG = 700
            ERROR_UNSUPPORTED_BPS = 701
            ERROR_UNEXPECTED_RESPONSE = 702
            ERROR_INTERACTIVE_MODE = 703
            ERROR_BAD_CALLBACK_NUMBER = 704
            ERROR_INVALID_AUTH_STATE = 705
            ERROR_WRITING_INITBPS = 706
            ERROR_INVALID_WIN_HANDLE = 707
            ERROR_NO_PASSWORD = 708
            ERROR_NO_USERNAME = 709
            ERROR_CANNOT_START_STATE_MACHINE = 710
            ERROR_GETTING_COMMSTATE = 711
            ERROR_SETTING_COMMSTATE = 712
            ERROR_COMM_FUNCTION = 713
            ERROR_CONFIGURATION_PROBLEM = 714
            ERROR_X25_DIAGNOSTIC = 715
            ERROR_TOO_MANY_LINE_ERRORS = 716
            ERROR_OVERRUN = 717
            ERROR_ACCT_EXPIRED = 718
            ERROR_CHANGING_PASSWORD = 719
            ERROR_NO_ACTIVE_ISDN_LINES = 720
            ERROR_NO_ISDN_CHANNELS_AVAILABLE = 721
        End Enum

        Private Declare Auto Function RasGetErrorString Lib "rasapi32.dll" (ByVal uErrorValue As Integer, ByVal lpszErrorString As String, ByVal cBufSize As Integer) As Integer
        Private Declare Auto Function FormatMessage Lib "kernel32" (ByVal dwFlags As Integer, ByVal lpSource As Object, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByVal lpBuffer As String, ByVal nSize As Integer, ByVal Arguments As Integer) As Integer

        Friend Shared Function GetRASErrorDescription(ByVal rtn As Long) As String
            Dim strError As String, i As Long
            strError = New String(Chr(0), 512)
            If rtn > 600 Then
                RasGetErrorString(rtn, strError, 512&)
            Else
                FormatMessage(&H1000, 0&, rtn, 0&, strError, 512, 0&)
            End If
            i = InStr(strError, Chr(0))
            If i > 1 Then
                Return Left$(strError, i - 1)
            End If
        End Function

    End Class

End Namespace



-----------------------------------------------------

Hope this helps.

rgds,
Ajit Anand
0
 

Author Comment

by:scorpion53061
Comment Utility
ajitanand,

Would you be okay if I split the points with  armoghan ?

0
 
LVL 5

Expert Comment

by:ajitanand
Comment Utility
sure no probs. :) I am not here solely for points dude!
0
 
LVL 18

Expert Comment

by:armoghan
Comment Utility
:)
0
 
LVL 18

Expert Comment

by:armoghan
Comment Utility
thanks both of you
Glad to help you scorpion53061
0
 

Author Comment

by:scorpion53061
Comment Utility
What is QuickGeneric.RAS.RASException? I cannot seem to find a reference for it?
0
 
LVL 5

Expert Comment

by:ajitanand
Comment Utility
Sorry, that was my mistake. The thing is that I keep my generic code in the namespace QuickGeneric.*

So in your case, to rectify the reference, just change:

Imports QuickGeneric.RAS.*
to
Imports RAS.*

---------------------------
So here is how your code looks like:
---------------------------
Option Strict Off

Imports System
Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.StructLayoutAttribute
Imports RAS.RASException

Namespace RAS
    Public Class RasManager

        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_MaxCallbackNumber As Integer = RAS_MaxPhoneNumber

        Public Delegate Sub Callback(ByVal unMsg As System.UInt32, ByVal rasconnstate As Integer, ByVal dwError As Integer)

        <StructLayout(LayoutKind.Sequential, Pack:=4, CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
        Private 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
            '#if (WINVER >= 0x401)
            Public dwSubEnTry As Integer
            Public dwCallbackId As Integer
            '#end if
        End Structure

        <StructLayout(LayoutKind.Sequential, CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
        Private Structure RASENTRYNAME
            Public dwSize As Int32
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxEnTryName + 1)> _
            Public szEntryName As String
            Public dwFlags As Int32
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH + 1)> _
            Public szPhonebookPath As String
        End Structure

        Private Declare Auto Function APIRasEnumEntries Lib "rasapi32.dll" Alias "RasEnumEntries" ( _
            ByVal reserved As String, _
            ByVal lpszPhonebook As String, _
            ByVal lpRasEntryName As IntPtr, _
            ByRef lpcb As Integer, _
            ByRef lpcEntries As Integer _
        ) As Integer


        Private 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 Declare Auto Function RasCreatePhonebookEntry Lib "rasapi32.dll" (ByVal hwnd As IntPtr, ByVal lpszPhonebook As String) As Integer
        Private Declare Auto Function RasEditPhonebookEntry Lib "rasapi32.dll" (ByVal hwnd As IntPtr, ByVal lpszPhonebook As String, ByVal lpszEntryName As String) As Integer
        Private Declare Auto Function InternetDial Lib "wininet.dll" (ByVal hwndParent As IntPtr, ByVal strEntryName As String, ByVal dwFlags As Integer, ByRef lpdwConnection As Integer, ByVal dwReserved As Integer) As Integer

        Private RasDialParam As RASDIALPARAMS
        Private Connection As Integer

        Public Enum RASCONNSTATE
            RAS_UNUSED = -1
            RASCS_OpenPort = 0
            RASCS_PortOpened = 1
            RASCS_ConnectDevice = 2
            RASCS_DeviceConnected = 3
            RASCS_AllDevicesConnected = 4
            RASCS_Authenticate = 5
            RASCS_AuthNotify
            RASCS_AuthRetry
            RASCS_AuthCallback
            RASCS_AuthChangePassword
            RASCS_AuthProject
            RASCS_AuthLinkSpeed
            RASCS_AuthAck
            RASCS_ReAuthenticate
            RASCS_Authenticated = 14
            RASCS_PrepareForCallback = 15
            RASCS_WaitForModemReset = 16
            RASCS_WaitForCallback = 17
            RASCS_Projected = 18
            '#if (WINVER >= 0x400)
            RASCS_StartAuthentication ' Windows 95 only
            RASCS_CallbackComplete ' Windows 95 only
            RASCS_LogonNetwork ' Windows 95 only
            '#endif
            RASCS_SubEntryConnected
            RASCS_SubEntryDisconnected
            RASCS_Interactive = &H1000
            RASCS_PAUSED = 4096
            RASCS_RetryAuthentication = 4097
            RASCS_CallbackSetByCaller = 4098
            RASCS_PasswordExpired = 4099
            '#if (WINVER >= 0x500)
            RASCS_InvokeEapUI
            '#endif
            RASCS_Connected = 8192
            RASCS_DONE = &H2000&
            RASCS_Disconnected = 8193
        End Enum

        <StructLayout(LayoutKind.Sequential, Pack:=1)> _
        Friend Structure RASCONNSTATUS
            Public dwSize As Integer
            Public rasconnstate As rasconnstate
            Public dwError As Integer
            Public szDeviceType As String
            Public szDeviceName As String
        End Structure

        Public Sub New()

            Connection = 0
            RasDialParam = New RASDIALPARAMS()
            RasDialParam.dwSize = Marshal.SizeOf(RasDialParam)

        End Sub

        Public Property UserName() As String
            Get
                Return RasDialParam.szUserName
            End Get
            Set(ByVal Value As String)
                RasDialParam.szUserName = Value
            End Set
        End Property

        Public Property Password() As String
            Get
                Return RasDialParam.szPassword
            End Get
            Set(ByVal Value As String)
                RasDialParam.szPassword = Value
            End Set
        End Property

        Public Property EntryName() As String
            Get
                Return RasDialParam.szEnTryName
            End Get
            Set(ByVal Value As String)
                RasDialParam.szEnTryName = Value
            End Set
        End Property

        Public Function Connect(ByVal rasDialFunc As Callback) As Integer
            RasDialParam.szEnTryName += Chr(0)
            RasDialParam.szUserName += Chr(0)
            RasDialParam.szPassword += Chr(0)
            Return RasDial(0, Nothing, RasDialParam, 0, rasDialFunc, Connection)
        End Function

        Public Function ConnectSync() As RASCONNSTATE
            RasDialParam.szEnTryName += Chr(0)
            RasDialParam.szUserName += Chr(0)
            RasDialParam.szPassword += Chr(0)
            RasDial(0, Nothing, RasDialParam, 0, Nothing, Connection)
            Return Connection
        End Function

        Public Function GetRasEntries() As String()

            'Dim entries() As RASENTRYNAME
            Dim RasEntryNameLen As Integer = Marshal.SizeOf(GetType(RASENTRYNAME))
            Dim lpCB As Integer = RasEntryNameLen
            Dim lpcEntries As Integer

            ' allocate memory from the unmanaged native heap
            ' as we cannot pass out an array of marshalled structures ByRef
            ' this needs to be freed when we have finished
            ' Start with a single item in the array

            Dim pArray As IntPtr = Marshal.AllocHGlobal(RasEntryNameLen)
            ' the following line sets RASENTRYNAME.dwSize
            Marshal.WriteInt32(pArray, RasEntryNameLen)

            Dim nnn As RASERROR = APIRasEnumEntries(Nothing, Nothing, pArray, lpCB, lpcEntries)
            If (nnn = RASERROR.ERROR_BUFFER_TOO_SMALL) Then
                'free the previous allocation
                Marshal.FreeHGlobal(pArray)
                'allocate another block of memory using the size returned in lpCB
                pArray = Marshal.AllocHGlobal(lpCB)
                'copy the size of a RASENTRYNAME structure to the first four bytes
                Marshal.WriteInt32(pArray, RasEntryNameLen)
                'call the api again
                nnn = APIRasEnumEntries(Nothing, Nothing, pArray, lpCB, lpcEntries)
            End If

            Dim rEntries() As String

            If (nnn = RASERROR.SUCCESS) Then

                Dim ii As Integer
                Dim rasEntry As RASENTRYNAME
                Dim pEntry As IntPtr = pArray

                ReDim rEntries(lpcEntries - 1)

                For ii = 0 To lpcEntries - 1

                    'loop through the  allocated block of memory and
                    'copy to a RASENTRYNAME structure
                    ' note: the System.Runtime.InteropServices.Marshal.PtrToStructure takes
                    ' either a Type or an Object as the second parameter, so we use GetType to

                    rasEntry = CType(Marshal.PtrToStructure(pEntry, GetType(RASENTRYNAME)), RASENTRYNAME)
                    rEntries(ii) = rasEntry.szEntryName
                    Debug.WriteLine(rasEntry.szPhonebookPath)

                    ' increment the pointer
                    pEntry = New IntPtr(pEntry.ToInt32 + RasEntryNameLen)

                Next

            End If

            Marshal.FreeHGlobal(pArray)

            If nnn <> RASERROR.SUCCESS Then
                Throw New RASException(nnn, GetRASErrorDescription(nnn))
            End If

            Return rEntries

        End Function

        Public Sub CreatePhoneBookEntry(ByVal Handle As IntPtr, ByVal PhoneBook As String)
            RasCreatePhonebookEntry(Handle, PhoneBook)
        End Sub

        Public Sub EditPhoneBookEntry(ByVal Handle As IntPtr, ByVal PhoneBook As String, ByVal EntryName As String)
            RasEditPhonebookEntry(Handle, PhoneBook, EntryName)
        End Sub

        Public Sub InternetDial(ByVal Handle As IntPtr, ByVal EntryName As String)
            Dim lpdwConnection As Integer
            Dim rValue As Integer
            rValue = InternetDial(Handle, EntryName, 1, lpdwConnection, 0)
            If rValue <> 0 Then
                Throw New RAS.RASException(rValue, RAS.RASException.GetRASErrorDescription(rValue))
            End If
        End Sub

    End Class

    Public Class RASException
        Inherits Exception

        Private eCode As RASERROR

        Public Sub New(ByVal ErrorCode As RASERROR, ByVal message As String)
            MyBase.New(message)
            eCode = ErrorCode
        End Sub

        Public ReadOnly Property ErrorCode() As RASERROR
            Get
                Return eCode
            End Get
        End Property

        Public Enum RASERROR
            ' RAS Control Error Codes
            '
            ' These error codes are returned by the LastError property and
            ' passed as an argument to the LastError event. These are the
            ' same codes returned by the RAS library
            '
            SUCCESS = 0
            ERROR_INVALID_PORT_HANDLE = 601
            ERROR_PORT_ALREADY_OPEN = 602
            ERROR_BUFFER_TOO_SMALL = 603
            ERROR_WRONG_INFO_SPECIFIED = 604
            ERROR_CANNOT_SET_PORT_INFO = 605
            ERROR_PORT_NOT_CONNECTED = 606
            ERROR_EVENT_INVALID = 607
            ERROR_DEVICE_DOES_NOT_EXIST = 608
            ERROR_DEVICETYPE_DOES_NOT_EXIST = 609
            ERROR_INVALID_BUFFER = 610
            ERROR_ROUTE_NOT_AVAILABLE = 611
            ERROR_ROUTE_NOT_ALLOCATED = 612
            ERROR_INVALID_COMPRESSION_SPECIFIED = 613
            ERROR_OUT_OF_BUFFERS = 614
            ERROR_PORT_NOT_FOUND = 615
            ERROR_ASYNC_REQUEST_PENDING = 616
            ERROR_ALREADY_DISCONNECTING = 617
            ERROR_PORT_NOT_OPEN = 618
            ERROR_PORT_DISCONNECTED = 619
            ERROR_NO_ENDPOINTS = 620
            ERROR_CANNOT_OPEN_PHONEBOOK = 621
            ERROR_CANNOT_LOAD_PHONEBOOK = 622
            ERROR_CANNOT_FIND_PHONEBOOK_ENTRY = 623
            ERROR_CANNOT_WRITE_PHONEBOOK = 624
            ERROR_CORRUPT_PHONEBOOK = 625
            ERROR_CANNOT_LOAD_STRING = 626
            ERROR_KEY_NOT_FOUND = 627
            ERROR_DISCONNECTION = 628
            ERROR_REMOTE_DISCONNECTION = 629
            ERROR_HARDWARE_FAILURE = 630
            ERROR_USER_DISCONNECTION = 631
            ERROR_INVALID_SIZE = 632
            ERROR_PORT_NOT_AVAILABLE = 633
            ERROR_CANNOT_PROJECT_CLIENT = 634
            ERROR_UNKNOWN = 635
            ERROR_WRONG_DEVICE_ATTACHED = 636
            ERROR_BAD_STRING = 637
            ERROR_REQUEST_TIMEOUT = 638
            ERROR_CANNOT_GET_LANA = 639
            ERROR_NETBIOS_ERROR = 640
            ERROR_SERVER_OUT_OF_RESOURCES = 641
            ERROR_NAME_EXISTS_ON_NET = 642
            ERROR_SERVER_GENERAL_NET_FAILURE = 643
            ERROR_AUTH_INTERNAL = 645
            ERROR_RESTRICTED_LOGON_HOURS = 646
            ERROR_ACCT_DISABLED = 647
            ERROR_PASSWD_EXPIRED = 648
            ERROR_NO_DIALIN_PERMISSION = 649
            ERROR_SERVER_NOT_RESPONDING = 650
            ERROR_FROM_DEVICE = 651
            ERROR_UNRECOGNIZED_RESPONSE = 652
            ERROR_MACRO_NOT_FOUND = 653
            ERROR_MACRO_NOT_DEFINED = 654
            ERROR_MESSAGE_MACRO_NOT_FOUND = 655
            ERROR_DEFAULTOFF_MACRO_NOT_FOUND = 656
            ERROR_FILE_COULD_NOT_BE_OPENED = 657
            ERROR_DEVICENAME_TOO_LONG = 658
            ERROR_DEVICENAME_NOT_FOUND = 659
            ERROR_NO_RESPONSES = 660
            ERROR_NO_COMMAND_FOUND = 661
            ERROR_WRONG_KEY_SPECIFIED = 662
            ERROR_UNKNOWN_DEVICE_TYPE = 663
            ERROR_ALLOCATING_MEMORY = 664
            ERROR_PORT_NOT_CONFIGURED = 665
            ERROR_DEVICE_NOT_READY = 666
            ERROR_READING_INI_FILE = 667
            ERROR_NO_CONNECTION = 668
            ERROR_BAD_USAGE_IN_INI_FILE = 669
            ERROR_READING_SECTIONNAME = 670
            ERROR_READING_DEVICETYPE = 671
            ERROR_READING_DEVICENAME = 672
            ERROR_READING_USAGE = 673
            ERROR_READING_MAXCONNECTBPS = 674
            ERROR_READING_MAXCARRIERBPS = 675
            ERROR_LINE_BUSY = 676
            ERROR_VOICE_ANSWER = 677
            ERROR_NO_ANSWER = 678
            ERROR_NO_CARRIER = 679
            ERROR_NO_DIALTONE = 680
            ERROR_IN_COMMAND = 681
            ERROR_WRITING_SECTIONNAME = 682
            ERROR_WRITING_DEVICETYPE = 683
            ERROR_WRITING_DEVICENAME = 684
            ERROR_WRITING_MAXCONNECTBPS = 685
            ERROR_WRITING_MAXCARRIERBPS = 686
            ERROR_WRITING_USAGE = 687
            ERROR_WRITING_DEFAULTOFF = 688
            ERROR_READING_DEFAULTOFF = 689
            ERROR_EMPTY_INI_FILE = 690
            ERROR_AUTHENTICATION_FAILURE = 691
            ERROR_PORT_OR_DEVICE = 692
            ERROR_NOT_BINARY_MACRO = 693
            ERROR_DCB_NOT_FOUND = 694
            ERROR_STATE_MACHINES_NOT_STARTED = 695
            ERROR_STATE_MACHINES_ALREADY_STARTED = 696
            ERROR_PARTIAL_RESPONSE_LOOPING = 697
            ERROR_UNKNOWN_RESPONSE_KEY = 698
            ERROR_RECV_BUF_FULL = 699
            ERROR_CMD_TOO_LONG = 700
            ERROR_UNSUPPORTED_BPS = 701
            ERROR_UNEXPECTED_RESPONSE = 702
            ERROR_INTERACTIVE_MODE = 703
            ERROR_BAD_CALLBACK_NUMBER = 704
            ERROR_INVALID_AUTH_STATE = 705
            ERROR_WRITING_INITBPS = 706
            ERROR_INVALID_WIN_HANDLE = 707
            ERROR_NO_PASSWORD = 708
            ERROR_NO_USERNAME = 709
            ERROR_CANNOT_START_STATE_MACHINE = 710
            ERROR_GETTING_COMMSTATE = 711
            ERROR_SETTING_COMMSTATE = 712
            ERROR_COMM_FUNCTION = 713
            ERROR_CONFIGURATION_PROBLEM = 714
            ERROR_X25_DIAGNOSTIC = 715
            ERROR_TOO_MANY_LINE_ERRORS = 716
            ERROR_OVERRUN = 717
            ERROR_ACCT_EXPIRED = 718
            ERROR_CHANGING_PASSWORD = 719
            ERROR_NO_ACTIVE_ISDN_LINES = 720
            ERROR_NO_ISDN_CHANNELS_AVAILABLE = 721
        End Enum

        Private Declare Auto Function RasGetErrorString Lib "rasapi32.dll" (ByVal uErrorValue As Integer, ByVal lpszErrorString As String, ByVal cBufSize As Integer) As Integer
        Private Declare Auto Function FormatMessage Lib "kernel32" (ByVal dwFlags As Integer, ByVal lpSource As Object, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByVal lpBuffer As String, ByVal nSize As Integer, ByVal Arguments As Integer) As Integer

        Friend Shared Function GetRASErrorDescription(ByVal rtn As Long) As String
            Dim strError As String, i As Long
            strError = New String(Chr(0), 512)
            If rtn > 600 Then
                RasGetErrorString(rtn, strError, 512&)
            Else
                FormatMessage(&H1000, 0&, rtn, 0&, strError, 512, 0&)
            End If
            i = InStr(strError, Chr(0))
            If i > 1 Then
                Return Left$(strError, i - 1)
            End If
        End Function

    End Class

End Namespace

-----------------------------------------

rgds,
Ajit Anand
0
 

Author Comment

by:scorpion53061
Comment Utility
I feel very bad and I am sorry but it is reporting after I copy and paste this class

C:\Visual Studio Projects\Customer Software\ClassConnection.vb(6): Namespace or type 'RASException' for the Imports 'RAS.RASException' cannot be found.

WHen I type

Imports RAS.RASException

It does appear in the drop down menu......

Just to be sure I have it right I am pasting what I have......

Option Strict Off

Imports System
Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.StructLayoutAttribute
Imports RAS.RASException

Namespace RAS
    Public Class RasManager

        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_MaxCallbackNumber As Integer = RAS_MaxPhoneNumber

        Public Delegate Sub Callback(ByVal unMsg As System.UInt32, ByVal rasconnstate As Integer, ByVal dwError As Integer)

        <StructLayout(LayoutKind.Sequential, Pack:=4, CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
        Private 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
            '#if (WINVER >= 0x401)
            Public dwSubEnTry As Integer
            Public dwCallbackId As Integer
            '#end if
        End Structure

        <StructLayout(LayoutKind.Sequential, CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
        Private Structure RASENTRYNAME
            Public dwSize As Int32
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxEnTryName + 1)> _
            Public szEntryName As String
            Public dwFlags As Int32
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH + 1)> _
            Public szPhonebookPath As String
        End Structure

        Private Declare Auto Function APIRasEnumEntries Lib "rasapi32.dll" Alias "RasEnumEntries" ( _
            ByVal reserved As String, _
            ByVal lpszPhonebook As String, _
            ByVal lpRasEntryName As IntPtr, _
            ByRef lpcb As Integer, _
            ByRef lpcEntries As Integer _
        ) As Integer


        Private 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 Declare Auto Function RasCreatePhonebookEntry Lib "rasapi32.dll" (ByVal hwnd As IntPtr, ByVal lpszPhonebook As String) As Integer
        Private Declare Auto Function RasEditPhonebookEntry Lib "rasapi32.dll" (ByVal hwnd As IntPtr, ByVal lpszPhonebook As String, ByVal lpszEntryName As String) As Integer
        Private Declare Auto Function InternetDial Lib "wininet.dll" (ByVal hwndParent As IntPtr, ByVal strEntryName As String, ByVal dwFlags As Integer, ByRef lpdwConnection As Integer, ByVal dwReserved As Integer) As Integer

        Private RasDialParam As RASDIALPARAMS
        Private Connection As Integer

        Public Enum RASCONNSTATE
            RAS_UNUSED = -1
            RASCS_OpenPort = 0
            RASCS_PortOpened = 1
            RASCS_ConnectDevice = 2
            RASCS_DeviceConnected = 3
            RASCS_AllDevicesConnected = 4
            RASCS_Authenticate = 5
            RASCS_AuthNotify
            RASCS_AuthRetry
            RASCS_AuthCallback
            RASCS_AuthChangePassword
            RASCS_AuthProject
            RASCS_AuthLinkSpeed
            RASCS_AuthAck
            RASCS_ReAuthenticate
            RASCS_Authenticated = 14
            RASCS_PrepareForCallback = 15
            RASCS_WaitForModemReset = 16
            RASCS_WaitForCallback = 17
            RASCS_Projected = 18
            '#if (WINVER >= 0x400)
            RASCS_StartAuthentication ' Windows 95 only
            RASCS_CallbackComplete ' Windows 95 only
            RASCS_LogonNetwork ' Windows 95 only
            '#endif
            RASCS_SubEntryConnected
            RASCS_SubEntryDisconnected
            RASCS_Interactive = &H1000
            RASCS_PAUSED = 4096
            RASCS_RetryAuthentication = 4097
            RASCS_CallbackSetByCaller = 4098
            RASCS_PasswordExpired = 4099
            '#if (WINVER >= 0x500)
            RASCS_InvokeEapUI
            '#endif
            RASCS_Connected = 8192
            RASCS_DONE = &H2000&
            RASCS_Disconnected = 8193
        End Enum

        <StructLayout(LayoutKind.Sequential, Pack:=1)> _
        Friend Structure RASCONNSTATUS
            Public dwSize As Integer
            Public rasconnstate As rasconnstate
            Public dwError As Integer
            Public szDeviceType As String
            Public szDeviceName As String
        End Structure

        Public Sub New()

            Connection = 0
            RasDialParam = New RASDIALPARAMS
            RasDialParam.dwSize = Marshal.SizeOf(RasDialParam)

        End Sub

        Public Property UserName() As String
            Get
                Return RasDialParam.szUserName
            End Get
            Set(ByVal Value As String)
                RasDialParam.szUserName = Value
            End Set
        End Property

        Public Property Password() As String
            Get
                Return RasDialParam.szPassword
            End Get
            Set(ByVal Value As String)
                RasDialParam.szPassword = Value
            End Set
        End Property

        Public Property EntryName() As String
            Get
                Return RasDialParam.szEnTryName
            End Get
            Set(ByVal Value As String)
                RasDialParam.szEnTryName = Value
            End Set
        End Property

        Public Function Connect(ByVal rasDialFunc As Callback) As Integer
            RasDialParam.szEnTryName += Chr(0)
            RasDialParam.szUserName += Chr(0)
            RasDialParam.szPassword += Chr(0)
            Return RasDial(0, Nothing, RasDialParam, 0, rasDialFunc, Connection)
        End Function

        Public Function ConnectSync() As RASCONNSTATE
            RasDialParam.szEnTryName += Chr(0)
            RasDialParam.szUserName += Chr(0)
            RasDialParam.szPassword += Chr(0)
            RasDial(0, Nothing, RasDialParam, 0, Nothing, Connection)
            Return Connection
        End Function

        Public Function GetRasEntries() As String()

            'Dim entries() As RASENTRYNAME
            Dim RasEntryNameLen As Integer = Marshal.SizeOf(GetType(RASENTRYNAME))
            Dim lpCB As Integer = RasEntryNameLen
            Dim lpcEntries As Integer

            ' allocate memory from the unmanaged native heap
            ' as we cannot pass out an array of marshalled structures ByRef
            ' this needs to be freed when we have finished
            ' Start with a single item in the array

            Dim pArray As IntPtr = Marshal.AllocHGlobal(RasEntryNameLen)
            ' the following line sets RASENTRYNAME.dwSize
            Marshal.WriteInt32(pArray, RasEntryNameLen)

            Dim nnn As RASERROR = APIRasEnumEntries(Nothing, Nothing, pArray, lpCB, lpcEntries)
            If (nnn = RASERROR.ERROR_BUFFER_TOO_SMALL) Then
                'free the previous allocation
                Marshal.FreeHGlobal(pArray)
                'allocate another block of memory using the size returned in lpCB
                pArray = Marshal.AllocHGlobal(lpCB)
                'copy the size of a RASENTRYNAME structure to the first four bytes
                Marshal.WriteInt32(pArray, RasEntryNameLen)
                'call the api again
                nnn = APIRasEnumEntries(Nothing, Nothing, pArray, lpCB, lpcEntries)
            End If

            Dim rEntries() As String

            If (nnn = RASERROR.SUCCESS) Then

                Dim ii As Integer
                Dim rasEntry As RASENTRYNAME
                Dim pEntry As IntPtr = pArray

                ReDim rEntries(lpcEntries - 1)

                For ii = 0 To lpcEntries - 1

                    'loop through the  allocated block of memory and
                    'copy to a RASENTRYNAME structure
                    ' note: the System.Runtime.InteropServices.Marshal.PtrToStructure takes
                    ' either a Type or an Object as the second parameter, so we use GetType to

                    rasEntry = CType(Marshal.PtrToStructure(pEntry, GetType(RASENTRYNAME)), RASENTRYNAME)
                    rEntries(ii) = rasEntry.szEntryName
                    Debug.WriteLine(rasEntry.szPhonebookPath)

                    ' increment the pointer
                    pEntry = New IntPtr(pEntry.ToInt32 + RasEntryNameLen)

                Next

            End If

            Marshal.FreeHGlobal(pArray)

            If nnn <> RASERROR.SUCCESS Then
                Throw New RASException(nnn, GetRASErrorDescription(nnn))
            End If

            Return rEntries

        End Function

        Public Sub CreatePhoneBookEntry(ByVal Handle As IntPtr, ByVal PhoneBook As String)
            RasCreatePhonebookEntry(Handle, PhoneBook)
        End Sub

        Public Sub EditPhoneBookEntry(ByVal Handle As IntPtr, ByVal PhoneBook As String, ByVal EntryName As String)
            RasEditPhonebookEntry(Handle, PhoneBook, EntryName)
        End Sub

        Public Sub InternetDial(ByVal Handle As IntPtr, ByVal EntryName As String)
            Dim lpdwConnection As Integer
            Dim rValue As Integer
            rValue = InternetDial(Handle, EntryName, 1, lpdwConnection, 0)
            If rValue <> 0 Then
                Throw New RAS.RASException(rValue, RAS.RASException.GetRASErrorDescription(rValue))
            End If
        End Sub

    End Class

    Public Class RASException
        Inherits Exception

        Private eCode As RASERROR

        Public Sub New(ByVal ErrorCode As RASERROR, ByVal message As String)
            MyBase.New(message)
            eCode = ErrorCode
        End Sub

        Public ReadOnly Property ErrorCode() As RASERROR
            Get
                Return eCode
            End Get
        End Property

        Public Enum RASERROR
            ' RAS Control Error Codes
            '
            ' These error codes are returned by the LastError property and
            ' passed as an argument to the LastError event. These are the
            ' same codes returned by the RAS library
            '
            SUCCESS = 0
            ERROR_INVALID_PORT_HANDLE = 601
            ERROR_PORT_ALREADY_OPEN = 602
            ERROR_BUFFER_TOO_SMALL = 603
            ERROR_WRONG_INFO_SPECIFIED = 604
            ERROR_CANNOT_SET_PORT_INFO = 605
            ERROR_PORT_NOT_CONNECTED = 606
            ERROR_EVENT_INVALID = 607
            ERROR_DEVICE_DOES_NOT_EXIST = 608
            ERROR_DEVICETYPE_DOES_NOT_EXIST = 609
            ERROR_INVALID_BUFFER = 610
            ERROR_ROUTE_NOT_AVAILABLE = 611
            ERROR_ROUTE_NOT_ALLOCATED = 612
            ERROR_INVALID_COMPRESSION_SPECIFIED = 613
            ERROR_OUT_OF_BUFFERS = 614
            ERROR_PORT_NOT_FOUND = 615
            ERROR_ASYNC_REQUEST_PENDING = 616
            ERROR_ALREADY_DISCONNECTING = 617
            ERROR_PORT_NOT_OPEN = 618
            ERROR_PORT_DISCONNECTED = 619
            ERROR_NO_ENDPOINTS = 620
            ERROR_CANNOT_OPEN_PHONEBOOK = 621
            ERROR_CANNOT_LOAD_PHONEBOOK = 622
            ERROR_CANNOT_FIND_PHONEBOOK_ENTRY = 623
            ERROR_CANNOT_WRITE_PHONEBOOK = 624
            ERROR_CORRUPT_PHONEBOOK = 625
            ERROR_CANNOT_LOAD_STRING = 626
            ERROR_KEY_NOT_FOUND = 627
            ERROR_DISCONNECTION = 628
            ERROR_REMOTE_DISCONNECTION = 629
            ERROR_HARDWARE_FAILURE = 630
            ERROR_USER_DISCONNECTION = 631
            ERROR_INVALID_SIZE = 632
            ERROR_PORT_NOT_AVAILABLE = 633
            ERROR_CANNOT_PROJECT_CLIENT = 634
            ERROR_UNKNOWN = 635
            ERROR_WRONG_DEVICE_ATTACHED = 636
            ERROR_BAD_STRING = 637
            ERROR_REQUEST_TIMEOUT = 638
            ERROR_CANNOT_GET_LANA = 639
            ERROR_NETBIOS_ERROR = 640
            ERROR_SERVER_OUT_OF_RESOURCES = 641
            ERROR_NAME_EXISTS_ON_NET = 642
            ERROR_SERVER_GENERAL_NET_FAILURE = 643
            ERROR_AUTH_INTERNAL = 645
            ERROR_RESTRICTED_LOGON_HOURS = 646
            ERROR_ACCT_DISABLED = 647
            ERROR_PASSWD_EXPIRED = 648
            ERROR_NO_DIALIN_PERMISSION = 649
            ERROR_SERVER_NOT_RESPONDING = 650
            ERROR_FROM_DEVICE = 651
            ERROR_UNRECOGNIZED_RESPONSE = 652
            ERROR_MACRO_NOT_FOUND = 653
            ERROR_MACRO_NOT_DEFINED = 654
            ERROR_MESSAGE_MACRO_NOT_FOUND = 655
            ERROR_DEFAULTOFF_MACRO_NOT_FOUND = 656
            ERROR_FILE_COULD_NOT_BE_OPENED = 657
            ERROR_DEVICENAME_TOO_LONG = 658
            ERROR_DEVICENAME_NOT_FOUND = 659
            ERROR_NO_RESPONSES = 660
            ERROR_NO_COMMAND_FOUND = 661
            ERROR_WRONG_KEY_SPECIFIED = 662
            ERROR_UNKNOWN_DEVICE_TYPE = 663
            ERROR_ALLOCATING_MEMORY = 664
            ERROR_PORT_NOT_CONFIGURED = 665
            ERROR_DEVICE_NOT_READY = 666
            ERROR_READING_INI_FILE = 667
            ERROR_NO_CONNECTION = 668
            ERROR_BAD_USAGE_IN_INI_FILE = 669
            ERROR_READING_SECTIONNAME = 670
            ERROR_READING_DEVICETYPE = 671
            ERROR_READING_DEVICENAME = 672
            ERROR_READING_USAGE = 673
            ERROR_READING_MAXCONNECTBPS = 674
            ERROR_READING_MAXCARRIERBPS = 675
            ERROR_LINE_BUSY = 676
            ERROR_VOICE_ANSWER = 677
            ERROR_NO_ANSWER = 678
            ERROR_NO_CARRIER = 679
            ERROR_NO_DIALTONE = 680
            ERROR_IN_COMMAND = 681
            ERROR_WRITING_SECTIONNAME = 682
            ERROR_WRITING_DEVICETYPE = 683
            ERROR_WRITING_DEVICENAME = 684
            ERROR_WRITING_MAXCONNECTBPS = 685
            ERROR_WRITING_MAXCARRIERBPS = 686
            ERROR_WRITING_USAGE = 687
            ERROR_WRITING_DEFAULTOFF = 688
            ERROR_READING_DEFAULTOFF = 689
            ERROR_EMPTY_INI_FILE = 690
            ERROR_AUTHENTICATION_FAILURE = 691
            ERROR_PORT_OR_DEVICE = 692
            ERROR_NOT_BINARY_MACRO = 693
            ERROR_DCB_NOT_FOUND = 694
            ERROR_STATE_MACHINES_NOT_STARTED = 695
            ERROR_STATE_MACHINES_ALREADY_STARTED = 696
            ERROR_PARTIAL_RESPONSE_LOOPING = 697
            ERROR_UNKNOWN_RESPONSE_KEY = 698
            ERROR_RECV_BUF_FULL = 699
            ERROR_CMD_TOO_LONG = 700
            ERROR_UNSUPPORTED_BPS = 701
            ERROR_UNEXPECTED_RESPONSE = 702
            ERROR_INTERACTIVE_MODE = 703
            ERROR_BAD_CALLBACK_NUMBER = 704
            ERROR_INVALID_AUTH_STATE = 705
            ERROR_WRITING_INITBPS = 706
            ERROR_INVALID_WIN_HANDLE = 707
            ERROR_NO_PASSWORD = 708
            ERROR_NO_USERNAME = 709
            ERROR_CANNOT_START_STATE_MACHINE = 710
            ERROR_GETTING_COMMSTATE = 711
            ERROR_SETTING_COMMSTATE = 712
            ERROR_COMM_FUNCTION = 713
            ERROR_CONFIGURATION_PROBLEM = 714
            ERROR_X25_DIAGNOSTIC = 715
            ERROR_TOO_MANY_LINE_ERRORS = 716
            ERROR_OVERRUN = 717
            ERROR_ACCT_EXPIRED = 718
            ERROR_CHANGING_PASSWORD = 719
            ERROR_NO_ACTIVE_ISDN_LINES = 720
            ERROR_NO_ISDN_CHANNELS_AVAILABLE = 721
        End Enum

        Private Declare Auto Function RasGetErrorString Lib "rasapi32.dll" (ByVal uErrorValue As Integer, ByVal lpszErrorString As String, ByVal cBufSize As Integer) As Integer
        Private Declare Auto Function FormatMessage Lib "kernel32" (ByVal dwFlags As Integer, ByVal lpSource As Object, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByVal lpBuffer As String, ByVal nSize As Integer, ByVal Arguments As Integer) As Integer

        Friend Shared Function GetRASErrorDescription(ByVal rtn As Long) As String
            Dim strError As String, i As Long
            strError = New String(Chr(0), 512)
            If rtn > 600 Then
                RasGetErrorString(rtn, strError, 512&)
            Else
                FormatMessage(&H1000, 0&, rtn, 0&, strError, 512, 0&)
            End If
            i = InStr(strError, Chr(0))
            If i > 1 Then
                Return Left$(strError, i - 1)
            End If
        End Function

    End Class

End Namespace
0
 
LVL 5

Expert Comment

by:ajitanand
Comment Utility
No probs. :)

This is because you are not using it the correct way. You need to place this class in a project first (say MyProject).

Thereafter the project's root namespace (say YourProjectRootNameSpace) will be the parent namespace of these Class as well.

The name of your Project's Root NameSpace can be obtained from your project properties in Visual Studio.

You need to replace the Imports statement from

"Imports RAS.RASException"
to
"YourProjectRootNameSpace.RAS.RASException".

In my example below, the Root Namespace of my project is RAS

------------------------------------------------------------
Option Strict Off

Imports System
Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.StructLayoutAttribute
Imports RAS.RASManagement.RASException

Namespace RASManagement
    Public Class RasManager

        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_MaxCallbackNumber As Integer = RAS_MaxPhoneNumber

        Public Delegate Sub Callback(ByVal unMsg As System.UInt32, ByVal rasconnstate As Integer, ByVal dwError As Integer)

        <StructLayout(LayoutKind.Sequential, Pack:=4, CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
        Private 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
            '#if (WINVER >= 0x401)
            Public dwSubEnTry As Integer
            Public dwCallbackId As Integer
            '#end if
        End Structure

        <StructLayout(LayoutKind.Sequential, CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
        Private Structure RASENTRYNAME
            Public dwSize As Int32
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxEnTryName + 1)> _
            Public szEntryName As String
            Public dwFlags As Int32
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH + 1)> _
            Public szPhonebookPath As String
        End Structure

        Private Declare Auto Function APIRasEnumEntries Lib "rasapi32.dll" Alias "RasEnumEntries" ( _
            ByVal reserved As String, _
            ByVal lpszPhonebook As String, _
            ByVal lpRasEntryName As IntPtr, _
            ByRef lpcb As Integer, _
            ByRef lpcEntries As Integer _
        ) As Integer


        Private 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 Declare Auto Function RasCreatePhonebookEntry Lib "rasapi32.dll" (ByVal hwnd As IntPtr, ByVal lpszPhonebook As String) As Integer
        Private Declare Auto Function RasEditPhonebookEntry Lib "rasapi32.dll" (ByVal hwnd As IntPtr, ByVal lpszPhonebook As String, ByVal lpszEntryName As String) As Integer
        Private Declare Auto Function InternetDial Lib "wininet.dll" (ByVal hwndParent As IntPtr, ByVal strEntryName As String, ByVal dwFlags As Integer, ByRef lpdwConnection As Integer, ByVal dwReserved As Integer) As Integer

        Private RasDialParam As RASDIALPARAMS
        Private Connection As Integer

        Public Enum RASCONNSTATE
            RAS_UNUSED = -1
            RASCS_OpenPort = 0
            RASCS_PortOpened = 1
            RASCS_ConnectDevice = 2
            RASCS_DeviceConnected = 3
            RASCS_AllDevicesConnected = 4
            RASCS_Authenticate = 5
            RASCS_AuthNotify
            RASCS_AuthRetry
            RASCS_AuthCallback
            RASCS_AuthChangePassword
            RASCS_AuthProject
            RASCS_AuthLinkSpeed
            RASCS_AuthAck
            RASCS_ReAuthenticate
            RASCS_Authenticated = 14
            RASCS_PrepareForCallback = 15
            RASCS_WaitForModemReset = 16
            RASCS_WaitForCallback = 17
            RASCS_Projected = 18
            '#if (WINVER >= 0x400)
            RASCS_StartAuthentication ' Windows 95 only
            RASCS_CallbackComplete ' Windows 95 only
            RASCS_LogonNetwork ' Windows 95 only
            '#endif
            RASCS_SubEntryConnected
            RASCS_SubEntryDisconnected
            RASCS_Interactive = &H1000
            RASCS_PAUSED = 4096
            RASCS_RetryAuthentication = 4097
            RASCS_CallbackSetByCaller = 4098
            RASCS_PasswordExpired = 4099
            '#if (WINVER >= 0x500)
            RASCS_InvokeEapUI
            '#endif
            RASCS_Connected = 8192
            RASCS_DONE = &H2000&
            RASCS_Disconnected = 8193
        End Enum

        <StructLayout(LayoutKind.Sequential, Pack:=1)> _
        Friend Structure RASCONNSTATUS
            Public dwSize As Integer
            Public rasconnstate As rasconnstate
            Public dwError As Integer
            Public szDeviceType As String
            Public szDeviceName As String
        End Structure

        Public Sub New()

            Connection = 0
            RasDialParam = New RASDIALPARAMS()
            RasDialParam.dwSize = Marshal.SizeOf(RasDialParam)

        End Sub

        Public Property UserName() As String
            Get
                Return RasDialParam.szUserName
            End Get
            Set(ByVal Value As String)
                RasDialParam.szUserName = Value
            End Set
        End Property

        Public Property Password() As String
            Get
                Return RasDialParam.szPassword
            End Get
            Set(ByVal Value As String)
                RasDialParam.szPassword = Value
            End Set
        End Property

        Public Property EntryName() As String
            Get
                Return RasDialParam.szEnTryName
            End Get
            Set(ByVal Value As String)
                RasDialParam.szEnTryName = Value
            End Set
        End Property

        Public Function Connect(ByVal rasDialFunc As Callback) As Integer
            RasDialParam.szEnTryName += Chr(0)
            RasDialParam.szUserName += Chr(0)
            RasDialParam.szPassword += Chr(0)
            Return RasDial(0, Nothing, RasDialParam, 0, rasDialFunc, Connection)
        End Function

        Public Function ConnectSync() As RASCONNSTATE
            RasDialParam.szEnTryName += Chr(0)
            RasDialParam.szUserName += Chr(0)
            RasDialParam.szPassword += Chr(0)
            RasDial(0, Nothing, RasDialParam, 0, Nothing, Connection)
            Return Connection
        End Function

        Public Function GetRasEntries() As String()

            'Dim entries() As RASENTRYNAME
            Dim RasEntryNameLen As Integer = Marshal.SizeOf(GetType(RASENTRYNAME))
            Dim lpCB As Integer = RasEntryNameLen
            Dim lpcEntries As Integer

            ' allocate memory from the unmanaged native heap
            ' as we cannot pass out an array of marshalled structures ByRef
            ' this needs to be freed when we have finished
            ' Start with a single item in the array

            Dim pArray As IntPtr = Marshal.AllocHGlobal(RasEntryNameLen)
            ' the following line sets RASENTRYNAME.dwSize
            Marshal.WriteInt32(pArray, RasEntryNameLen)

            Dim nnn As RASERROR = APIRasEnumEntries(Nothing, Nothing, pArray, lpCB, lpcEntries)
            If (nnn = RASERROR.ERROR_BUFFER_TOO_SMALL) Then
                'free the previous allocation
                Marshal.FreeHGlobal(pArray)
                'allocate another block of memory using the size returned in lpCB
                pArray = Marshal.AllocHGlobal(lpCB)
                'copy the size of a RASENTRYNAME structure to the first four bytes
                Marshal.WriteInt32(pArray, RasEntryNameLen)
                'call the api again
                nnn = APIRasEnumEntries(Nothing, Nothing, pArray, lpCB, lpcEntries)
            End If

            Dim rEntries() As String

            If (nnn = RASERROR.SUCCESS) Then

                Dim ii As Integer
                Dim rasEntry As RASENTRYNAME
                Dim pEntry As IntPtr = pArray

                ReDim rEntries(lpcEntries - 1)

                For ii = 0 To lpcEntries - 1

                    'loop through the  allocated block of memory and
                    'copy to a RASENTRYNAME structure
                    ' note: the System.Runtime.InteropServices.Marshal.PtrToStructure takes
                    ' either a Type or an Object as the second parameter, so we use GetType to

                    rasEntry = CType(Marshal.PtrToStructure(pEntry, GetType(RASENTRYNAME)), RASENTRYNAME)
                    rEntries(ii) = rasEntry.szEntryName
                    Debug.WriteLine(rasEntry.szPhonebookPath)

                    ' increment the pointer
                    pEntry = New IntPtr(pEntry.ToInt32 + RasEntryNameLen)

                Next

            End If

            Marshal.FreeHGlobal(pArray)

            If nnn <> RASERROR.SUCCESS Then
                Throw New RASException(nnn, GetRASErrorDescription(nnn))
            End If

            Return rEntries

        End Function

        Public Sub CreatePhoneBookEntry(ByVal Handle As IntPtr, ByVal PhoneBook As String)
            RasCreatePhonebookEntry(Handle, PhoneBook)
        End Sub

        Public Sub EditPhoneBookEntry(ByVal Handle As IntPtr, ByVal PhoneBook As String, ByVal EntryName As String)
            RasEditPhonebookEntry(Handle, PhoneBook, EntryName)
        End Sub

        Public Sub InternetDial(ByVal Handle As IntPtr, ByVal EntryName As String)
            Dim lpdwConnection As Integer
            Dim rValue As Integer
            rValue = InternetDial(Handle, EntryName, 1, lpdwConnection, 0)
            If rValue <> 0 Then
                Throw New RASManagement.RASException(rValue, RASManagement.RASException.GetRASErrorDescription(rValue))
            End If
        End Sub

    End Class

    Public Class RASException
        Inherits Exception

        Private eCode As RASERROR

        Public Sub New(ByVal ErrorCode As RASERROR, ByVal message As String)
            MyBase.New(message)
            eCode = ErrorCode
        End Sub

        Public ReadOnly Property ErrorCode() As RASERROR
            Get
                Return eCode
            End Get
        End Property

        Public Enum RASERROR
            ' RAS Control Error Codes
            '
            ' These error codes are returned by the LastError property and
            ' passed as an argument to the LastError event. These are the
            ' same codes returned by the RAS library
            '
            SUCCESS = 0
            ERROR_INVALID_PORT_HANDLE = 601
            ERROR_PORT_ALREADY_OPEN = 602
            ERROR_BUFFER_TOO_SMALL = 603
            ERROR_WRONG_INFO_SPECIFIED = 604
            ERROR_CANNOT_SET_PORT_INFO = 605
            ERROR_PORT_NOT_CONNECTED = 606
            ERROR_EVENT_INVALID = 607
            ERROR_DEVICE_DOES_NOT_EXIST = 608
            ERROR_DEVICETYPE_DOES_NOT_EXIST = 609
            ERROR_INVALID_BUFFER = 610
            ERROR_ROUTE_NOT_AVAILABLE = 611
            ERROR_ROUTE_NOT_ALLOCATED = 612
            ERROR_INVALID_COMPRESSION_SPECIFIED = 613
            ERROR_OUT_OF_BUFFERS = 614
            ERROR_PORT_NOT_FOUND = 615
            ERROR_ASYNC_REQUEST_PENDING = 616
            ERROR_ALREADY_DISCONNECTING = 617
            ERROR_PORT_NOT_OPEN = 618
            ERROR_PORT_DISCONNECTED = 619
            ERROR_NO_ENDPOINTS = 620
            ERROR_CANNOT_OPEN_PHONEBOOK = 621
            ERROR_CANNOT_LOAD_PHONEBOOK = 622
            ERROR_CANNOT_FIND_PHONEBOOK_ENTRY = 623
            ERROR_CANNOT_WRITE_PHONEBOOK = 624
            ERROR_CORRUPT_PHONEBOOK = 625
            ERROR_CANNOT_LOAD_STRING = 626
            ERROR_KEY_NOT_FOUND = 627
            ERROR_DISCONNECTION = 628
            ERROR_REMOTE_DISCONNECTION = 629
            ERROR_HARDWARE_FAILURE = 630
            ERROR_USER_DISCONNECTION = 631
            ERROR_INVALID_SIZE = 632
            ERROR_PORT_NOT_AVAILABLE = 633
            ERROR_CANNOT_PROJECT_CLIENT = 634
            ERROR_UNKNOWN = 635
            ERROR_WRONG_DEVICE_ATTACHED = 636
            ERROR_BAD_STRING = 637
            ERROR_REQUEST_TIMEOUT = 638
            ERROR_CANNOT_GET_LANA = 639
            ERROR_NETBIOS_ERROR = 640
            ERROR_SERVER_OUT_OF_RESOURCES = 641
            ERROR_NAME_EXISTS_ON_NET = 642
            ERROR_SERVER_GENERAL_NET_FAILURE = 643
            ERROR_AUTH_INTERNAL = 645
            ERROR_RESTRICTED_LOGON_HOURS = 646
            ERROR_ACCT_DISABLED = 647
            ERROR_PASSWD_EXPIRED = 648
            ERROR_NO_DIALIN_PERMISSION = 649
            ERROR_SERVER_NOT_RESPONDING = 650
            ERROR_FROM_DEVICE = 651
            ERROR_UNRECOGNIZED_RESPONSE = 652
            ERROR_MACRO_NOT_FOUND = 653
            ERROR_MACRO_NOT_DEFINED = 654
            ERROR_MESSAGE_MACRO_NOT_FOUND = 655
            ERROR_DEFAULTOFF_MACRO_NOT_FOUND = 656
            ERROR_FILE_COULD_NOT_BE_OPENED = 657
            ERROR_DEVICENAME_TOO_LONG = 658
            ERROR_DEVICENAME_NOT_FOUND = 659
            ERROR_NO_RESPONSES = 660
            ERROR_NO_COMMAND_FOUND = 661
            ERROR_WRONG_KEY_SPECIFIED = 662
            ERROR_UNKNOWN_DEVICE_TYPE = 663
            ERROR_ALLOCATING_MEMORY = 664
            ERROR_PORT_NOT_CONFIGURED = 665
            ERROR_DEVICE_NOT_READY = 666
            ERROR_READING_INI_FILE = 667
            ERROR_NO_CONNECTION = 668
            ERROR_BAD_USAGE_IN_INI_FILE = 669
            ERROR_READING_SECTIONNAME = 670
            ERROR_READING_DEVICETYPE = 671
            ERROR_READING_DEVICENAME = 672
            ERROR_READING_USAGE = 673
            ERROR_READING_MAXCONNECTBPS = 674
            ERROR_READING_MAXCARRIERBPS = 675
            ERROR_LINE_BUSY = 676
            ERROR_VOICE_ANSWER = 677
            ERROR_NO_ANSWER = 678
            ERROR_NO_CARRIER = 679
            ERROR_NO_DIALTONE = 680
            ERROR_IN_COMMAND = 681
            ERROR_WRITING_SECTIONNAME = 682
            ERROR_WRITING_DEVICETYPE = 683
            ERROR_WRITING_DEVICENAME = 684
            ERROR_WRITING_MAXCONNECTBPS = 685
            ERROR_WRITING_MAXCARRIERBPS = 686
            ERROR_WRITING_USAGE = 687
            ERROR_WRITING_DEFAULTOFF = 688
            ERROR_READING_DEFAULTOFF = 689
            ERROR_EMPTY_INI_FILE = 690
            ERROR_AUTHENTICATION_FAILURE = 691
            ERROR_PORT_OR_DEVICE = 692
            ERROR_NOT_BINARY_MACRO = 693
            ERROR_DCB_NOT_FOUND = 694
            ERROR_STATE_MACHINES_NOT_STARTED = 695
            ERROR_STATE_MACHINES_ALREADY_STARTED = 696
            ERROR_PARTIAL_RESPONSE_LOOPING = 697
            ERROR_UNKNOWN_RESPONSE_KEY = 698
            ERROR_RECV_BUF_FULL = 699
            ERROR_CMD_TOO_LONG = 700
            ERROR_UNSUPPORTED_BPS = 701
            ERROR_UNEXPECTED_RESPONSE = 702
            ERROR_INTERACTIVE_MODE = 703
            ERROR_BAD_CALLBACK_NUMBER = 704
            ERROR_INVALID_AUTH_STATE = 705
            ERROR_WRITING_INITBPS = 706
            ERROR_INVALID_WIN_HANDLE = 707
            ERROR_NO_PASSWORD = 708
            ERROR_NO_USERNAME = 709
            ERROR_CANNOT_START_STATE_MACHINE = 710
            ERROR_GETTING_COMMSTATE = 711
            ERROR_SETTING_COMMSTATE = 712
            ERROR_COMM_FUNCTION = 713
            ERROR_CONFIGURATION_PROBLEM = 714
            ERROR_X25_DIAGNOSTIC = 715
            ERROR_TOO_MANY_LINE_ERRORS = 716
            ERROR_OVERRUN = 717
            ERROR_ACCT_EXPIRED = 718
            ERROR_CHANGING_PASSWORD = 719
            ERROR_NO_ACTIVE_ISDN_LINES = 720
            ERROR_NO_ISDN_CHANNELS_AVAILABLE = 721
        End Enum

        Private Declare Auto Function RasGetErrorString Lib "rasapi32.dll" (ByVal uErrorValue As Integer, ByVal lpszErrorString As String, ByVal cBufSize As Integer) As Integer
        Private Declare Auto Function FormatMessage Lib "kernel32" (ByVal dwFlags As Integer, ByVal lpSource As Object, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByVal lpBuffer As String, ByVal nSize As Integer, ByVal Arguments As Integer) As Integer

        Friend Shared Function GetRASErrorDescription(ByVal rtn As Long) As String
            Dim strError As String, i As Long
            strError = New String(Chr(0), 512)
            If rtn > 600 Then
                RasGetErrorString(rtn, strError, 512&)
            Else
                FormatMessage(&H1000, 0&, rtn, 0&, strError, 512, 0&)
            End If
            i = InStr(strError, Chr(0))
            If i > 1 Then
                Return Left$(strError, i - 1)
            End If
        End Function

    End Class

End Namespace
----------------------------------------------------
Step 1. Save the above in a file say RASManagement.vb in your project
----------------------------------------------------


Step 2. Save the following in a file as ConnectionChecker.vb in your project
----------------------------------------------------
Public Class ConnectionChecker
        Private Declare Auto Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Integer, ByVal dwReserved As Integer) As Boolean

        Public Function IsInternetConnection() As Boolean
            Dim flag As Integer = 1
            Return InternetGetConnectedState(flag, 0)
        End Function

    End Class



---------------------------------------------------
Step 3. Use code like below to call it:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            'Check if connected to the Internet, if not then dial the default connection
            Dim MyObject As ConnectionChecker = New ConnectionChecker()
            If Not MyObject.IsInternetConnection Then
                Try
                    'Simple way to dial a connection
                    Dim Manager As New RASManagement.RasManager()
                    Manager.InternetDial(Me.Handle, "")

                Catch Ex As RASManagement.RASException
                    If Ex.ErrorCode = RASManagement.RASException.RASERROR.ERROR_USER_DISCONNECTION Then
                        Exit Sub
                    Else
                        Throw Ex 'Let the global exception handler take care of this
                    End If
                End Try
            End If

        Catch ex2 As Exception

        End Try
    End Sub
----------------------------------------------------


If still confusions, let me know, I will upload a sample project somewhere and give a link here.

rgds,
Ajit Anand
0
 

Author Comment

by:scorpion53061
Comment Utility
That did it.

Thank you and I apologize for my failure to catch on.

Now unto learning about all of these functions and what I can do with them!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now