步骤:新建一窗体form,建立两个框架frame1和frame2在frame1上建立四个标签、四个text控件和一个按钮,在frame2上建立一个标签、一个text控件和一个按钮。
代码如下
Option Explicit
Private Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End Type
Const NO_ERROR = 0
Const CONNECT_UPDATE_PROFILE = &H1
Const RESOURCETYPE_DISK = &H1
Const RESOURCETYPE_PRINT = &H2
Const RESOURCETYPE_ANY = &H0
Const RESOURCE_CONNECTED = &H1
Const RESOURCE_REMEMBERED = &H3
Const RESOURCE_GLOBALNET = &H2
Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
Const RESOURCEDISPLAYTYPE_GENERIC = &H0
Const RESOURCEDISPLAYTYPE_SERVER = &H2
Const RESOURCEDISPLAYTYPE_SHARE = &H3
Const RESOURCEUSAGE_CONNECTABLE = &H1
Const RESOURCEUSAGE_CONTAINER = &H2
Private Declare Function WNetAddConnection2 Lib “mpr.dll” Alias _
“WNetAddConnection2A” _
lpNetResource As NETRESOURCE _
ByVal lpPassword As String _
ByVal lpUserName As String _
ByVal dwFlags As Long As Long
Private Declare Function WNetCancelConnection2 Lib “mpr.dll” Alias _
“WNetCancelConnection2A” _
ByVal lpName As String _
ByVal dwFlags As Long _
ByVal fForce As Long As Long
Private Sub cmdConnect_Click
Dim NetR As NETRESOURCE
Dim ErrInfo As Long
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = Text10.Text
NetR.lpRemoteName = Text11.Text
ErrInfo = WNetAddConnection2NetR Text13.Text Text12.Text CONNECT_UPDATE_PROFILE
If ErrInfo = NO_ERROR Then
MsgBox “Net connection successful” vbInformation
Else
MsgBox “ERROR ” & StrErrInfo & “ - Net connection unsuccessful” _
vbExclamation
End If
End Sub
Private Sub cmdDisconnect_Click
Dim ErrInfo As Long
Dim strLocalName As String
strLocalName = Text14.Text
ErrInfo = WNetCancelConnection2strLocalName CONNECT_UPDATE_PROFILE False
If ErrInfo = NO_ERROR Then
MsgBox “Net disconnection successful” vbInformation
Else
MsgBox “ERROR ” & StrErrInfo & “ - Net disconnection unsuccessful” _
vbExclamation
End If
End Sub