How to Dis-connect Internet programmatically (in two ways)
Posted on January 5, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | 'Internet - How to Dis-connect Internet programmatically (in two ways) 'Method 1: Option Explicit Private Sub Command1_Click() Dim lRet As Long Dim strConnectionName As String strConnectionName = "Connection through RO_100 VE Network Connection" lRet = Shell("RASDIAL " & chr ( 34) & strConnectionName & chr ( 34) & " /DISCONNECT", vbNormalFocus) End Sub 'Method 2: Option Explicit Public Const RAS_MAXENTRYNAME As Integer = 256 Public Const RAS_MAXDEVICETYPE As Integer = 16 Public Const RAS_MAXDEVICENAME As Integer = 128 Public Const RAS_RASCONNSIZE As Integer = 412 Public Const ERROR_SUCCESS = 0& Public Type RasEntryName dwSize As Long szEntryName(RAS_MAXENTRYNAME) As Byte End Type Public Type RasConn dwSize As Long hRasConn As Long szEntryName(RAS_MAXENTRYNAME) As Byte szDeviceType(RAS_MAXDEVICETYPE) As Byte szDeviceName(RAS_MAXDEVICENAME) As Byte End Type Public Declare Function RasEnumConnections Lib "rasapi32.DLL" Alias "RasEnumConnectionsA" _ (lpRasConn As Any, lpcb As Long, lpcConnections As Long) As Long Public Declare Function RasHangUp Lib "rasapi32.DLL" Alias "RasHangUpA" (ByVal hRasConn As Long) As Long Public gstrISPName As String Public ReturnCode As Long Public Function ByteToString(bytString() As Byte) As String Dim I As Integer ByteToString = "" I = 0 While bytString(I) = 0& ByteToString = ByteToString & chr ( bytString(I)) I = I + 1 Wend End Function Private Sub Command1_Click() Dim I As Long Dim lpRasConn(255) As RasConn Dim lpcb As Long Dim lpcConnections As Long Dim hRasConn As Long lpRasConn(0).dwSize = RAS_RASCONNSIZE lpcb = RAS_MAXENTRYNAME * lpRasConn(0).dwSize lpcConnections = 0 ReturnCode = RasEnumConnections(lpRasConn(0), lpcb, lpcConnections) If ReturnCode = ERROR_SUCCESS Then For I = 0 To lpcConnections - 1 If Trim(ByteToString(lpRasConn(I).szEntryName)) = Trim(gstrISPName) Then hRasConn = lpRasConn(I).hRasConn ReturnCode = RasHangUp(ByVal hRasConn) End If Next I End If End Sub |