We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 17,890 times

Related Categories

Disconnect from the Internet

To disconnect from the internet, we use the rasapi32.dll file. First, we enumerate (loop through), all the active connections using the RasEnumConnections API call, and then call RasHangUp to disconnect. This code shows you how:

'Module Code
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 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 Sub HangUp()
    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

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


'Form Code
Private Sub Command1_Click()
    Call HangUp
End Sub

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Posted by illuminati1618 on 23 Mar 2005

    I am getting the same problem , Please Help.. Did anyone found a solution yet

  • Posted by jeikul on 14 Feb 2005

    I got the same problem, anybody can help ?

  • what happend in VB.Net

    Posted by phuonghuy2912 on 25 Oct 2004

    First thanks for your code.
    I'm in trouple with this proplem - API RASConnections.
    You know, your code write in Vb6 and a convert it into VB.Net but the result not perfect, when I debug I realize th...

  • My dilapitated connection thanks you

    Posted by necrostatic on 25 Jul 2004

    Aside from ERROR_SUCCESS not being defined (const ERROR_SUCCESS = 0), this code was exactly what I was after, and it allows me to extract information about active connections that I didn't think could...

  • Posted by mujanoojapoojawhatda on 03 Jul 2002

    No offence to the kool Mr Crowley but tuhsan has written a Tutorial which you may be interested in:

    http://www.developerfusion.com/show/1920/3/ [Connecting/Disconnecting]

    http://www.developerfus...