首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

怎么自动连接网络和断开网络

2012-05-24 
如何自动连接网络和断开网络我家用的是联通的宽带,根据用户名和密码如何用代码实现连接网络和断开网络?[解

如何自动连接网络和断开网络
我家用的是联通的宽带,
根据用户名和密码如何用代码实现连接网络和断开网络?

[解决办法]
http://blog.sina.com.cn/s/blog_4c55fd6101000bm4.html

Dim ras As New RASDisplay()
ras.Disconnect() '断线 
ras.Connect("adsl") '拨号 

VB.NET code
Imports System.Runtime.InteropServicesPublic Structure RASCONN    Public dwSize As Integer    Public hrasconn As IntPtr    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=257)> _    Public szEntryName As String    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=17)> _    Public szDeviceType As String    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=129)> _    Public szDeviceName As StringEnd Structure<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _Public Structure RasStats    Public dwSize As Integer    Public dwBytesXmited As Integer    Public dwBytesRcved As Integer    Public dwFramesXmited As Integer    Public dwFramesRcved As Integer    Public dwCrcErr As Integer    Public dwTimeoutErr As Integer    Public dwAlignmentErr As Integer    Public dwHardwareOverrunErr As Integer    Public dwFramingErr As Integer    Public dwBufferOverrunErr As Integer    Public dwCompressionRatioIn As Integer    Public dwCompressionRatioOut As Integer    Public dwBps As Integer    Public dwConnectionDuration As IntegerEnd Structure<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _Public Structure RasEntryName    Public dwSize As Integer    Public szEntryName As String End StructurePublic Class RAS       <DllImport("Rasapi32.dll", EntryPoint:="RasEnumConnectionsA", SetLastError:=True)> _    Friend Shared Function RasEnumConnections(ByRef lprasconn As RASCONN, ByRef lpcb As Integer, ByRef lpcConnections As Integer) As Integer    End Function    <DllImport("rasapi32.dll", CharSet:=CharSet.Auto)> _    Friend Shared Function RasGetConnectionStatistics(ByVal hRasConn As IntPtr, <[In](), Out()> ByVal lpStatistics As RasStats) As UInteger    End Function    <DllImport("rasapi32.dll", CharSet:=CharSet.Auto)> _    Public Shared Function RasHangUp(ByVal hrasconn As IntPtr) As UInteger    End Function       <DllImport("rasapi32.dll", CharSet:=CharSet.Auto)> _    Public Shared Function RasEnumEntries(ByVal reserved As String, ByVal lpszPhonebook As String, <[In](), Out()> ByVal lprasentryname As RasEntryName(), ByRef lpcb As Integer, ByRef lpcEntries As Integer) As UInteger    End Function    <DllImport("wininet.dll", CharSet:=CharSet.Auto)> _    Public Shared Function InternetDial(ByVal hwnd As IntPtr, <[In]()> ByVal lpszConnectoid As String, ByVal dwFlags As UInteger, ByRef lpdwConnection As Integer, ByVal dwReserved As UInteger) As Integer    End Function End ClassPublic Enum DEL_CACHE_TYPE '要删除的类型。    File '表示internet临时文件     Cookie '表示Cookie  End EnumPublic Class RASDisplay    <DllImport("wininet.dll", CharSet:=CharSet.Auto)> _    Public Shared Function DeleteUrlCacheEntry(ByVal type As DEL_CACHE_TYPE) As Boolean    End Function    Private m_duration As String    Private m_ConnectionName As String    Private m_ConnectionNames As String()    Private m_TX As Double    Private m_RX As Double    Private m_connected As Boolean    Private m_ConnectedRasHandle As IntPtr    Private status As New RasStats()    Public Sub New()        m_connected = True        Dim lpras As New RAS()        Dim lprasConn As New RASCONN()        lprasConn.dwSize = Marshal.SizeOf(GetType(RASCONN))        lprasConn.hrasconn = IntPtr.Zero        Dim lpcb As Integer = 0        Dim lpcConnections As Integer = 0        Dim nRet As Integer = 0        lpcb = Marshal.SizeOf(GetType(RASCONN))        nRet = RAS.RasEnumConnections(lprasConn, lpcb, lpcConnections)        If nRet <> 0 Then            m_connected = False            Return        End If        If lpcConnections > 0 Then            Dim stats As New RasStats()            m_ConnectedRasHandle = lprasConn.hrasconn            RAS.RasGetConnectionStatistics(lprasConn.hrasconn, stats)            m_ConnectionName = lprasConn.szEntryName            Dim Hours As Integer = 0            Dim Minutes As Integer = 0            Dim Seconds As Integer = 0            Hours = ((stats.dwConnectionDuration \ 1000) \ 3600)            Minutes = ((stats.dwConnectionDuration \ 1000) \ 60) - (Hours * 60)            Seconds = ((stats.dwConnectionDuration \ 1000)) - (Minutes * 60) - (Hours * 3600)            m_duration = Hours & "   hours   " & Minutes & "   minutes   " & Seconds & "   secs"            m_TX = stats.dwBytesXmited            m_RX = stats.dwBytesRcved        Else            m_connected = False        End If        Dim lpNames As Integer = 1        Dim entryNameSize As Integer = 0        Dim lpSize As Integer = 0        Dim names As RasEntryName() = Nothing        entryNameSize = Marshal.SizeOf(GetType(RasEntryName))        lpSize = lpNames * entryNameSize        names = New RasEntryName(lpNames - 1) {}        names(0).dwSize = entryNameSize        Dim retval As UInteger = RAS.RasEnumEntries(Nothing, Nothing, names, lpSize, lpNames)        If lpNames > 1 Then            names = New RasEntryName(lpNames - 1) {}            For i As Integer = 0 To names.Length - 1                names(i).dwSize = entryNameSize            Next            retval = RAS.RasEnumEntries(Nothing, Nothing, names, lpSize, lpNames)        End If        m_ConnectionNames = New String(names.Length - 1) {}        If lpNames > 0 Then            For i As Integer = 0 To names.Length - 1                m_ConnectionNames(i) = names(i).szEntryName            Next        End If    End Sub    Public ReadOnly Property Duration() As String        Get            Return If(m_connected, m_duration, "")        End Get    End Property    Public ReadOnly Property Connections() As String()        Get            Return m_ConnectionNames        End Get    End Property    Public ReadOnly Property BytesTransmitted() As Double        Get            Return If(m_connected, m_TX, 0)        End Get    End Property    Public ReadOnly Property BytesReceived() As Double        Get            Return If(m_connected, m_RX, 0)        End Get    End Property    Public ReadOnly Property ConnectionName() As String        Get            Return If(m_connected, m_ConnectionName, "")        End Get    End Property    Public ReadOnly Property IsConnected() As Boolean        Get            Return m_connected        End Get    End Property    Public Function Connect(ByVal Connection As String) As Integer        Dim temp As Integer = 0        Dim INTERNET_AUTO_DIAL_UNATTENDED As UInteger = 2        Dim retVal As Integer = RAS.InternetDial(IntPtr.Zero, Connection, INTERNET_AUTO_DIAL_UNATTENDED, temp, 0)        Return retVal    End Function    Public Sub Disconnect()        RAS.RasHangUp(m_ConnectedRasHandle)    End SubEnd Class 

热点排行