取消连线
Function CancelConnection(ByVal LocalDriver As String, _
ByVal ForceClose As Boolean) As Boolean
LocalDriver : 对应本机的磁盘代号,如: "H:"
ForceClose : True表示强迫结束连线,而不管有没有程式正与之连线
成功时传回True,否则为False
例:
Call CancelConnection("x:", True)
以下在.bas
Option Explicit
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
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
Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long
Declare Function WNetGetLastError Lib "mpr.dll" Alias "WNetGetLastErrorA" (lpError As Long, ByVal lpErrorBuf As String, ByVal nErrorBufSize As Long, ByVal lpNameBuf As String, ByVal nNameBufSize As Long) As Long
Public Const RESOURCE_PUBLICNET = &H2
Public Const RESOURCETYPE_ANY = &H0
Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
Public Const RESOURCEUSAGE_CONNECTABLE = &H1
Public Const CONNECT_UPDATE_PROFILE = &H1
Public Function AddConnection(ByVal RemoteLocation As String, ByVal LocalDriver As String, _
ByVal Passwd As String, ByVal UserName As String) As Boolean
Dim ne As NETRESOURCE, i As Long
Dim errstr As String, errpriv As String, erno As Long
i = WNetAddConnection2(ne, Passwd, UserName, 0)
If i = 0 Then
AddConnection = True
Else
AddConnection = False
errstr = String(256, 0)
errpriv = String(256, 0)
i = WNetGetLastError(erno, errstr, 256, errpriv, 256)
errstr = Left(errstr, InStr(1, errstr, Chr(0)) - 1)
MsgBox errstr, vbCritical
End If
End Function
Public Function CancelConnection(ByVal LocalDriver As String, ByVal ForceClose As Boolean) As Boolean
Dim i As Long
Dim errstr As String, errpriv As String, erno As Long
i = WNetCancelConnection2(LocalDriver, 0, IIf(ForceClose, 1, 0))
If i = 0 Then
CancelConnection = True
Else
CancelConnection = False
errstr = String(256, 0)
errpriv = String(256, 0)
i = WNetGetLastError(erno, errstr, 256, errpriv, 256)
errstr = Left(errstr, InStr(1, errstr, Chr(0)) - 1)
MsgBox errstr, vbCritical
End If
楼上"shalen520(Love will keep us alive)"的方案:采用命令行方式,优点是简单,缺点是速度慢.
调用API作映射共享不错,但是共享映射最好不要显示出来.
本人最终采用的方案是调用API,如下.
请大家指正,待大家看过之后,稍候两日,结本帖.谢谢!
'API声明:
Declare Function WNetAddConnection2 Lib "mpr.dll" _
Alias "WNetAddConnection2A" ( _
ByVal lpNetResource As NETRESOURCE(), _
ByVal lpPassword As String, _
ByVal lpUserName As String, _
ByVal dwFlags As Integer) As Integer
'结构定义:
Structure NETRESOURCE
Public dwScope As Integer
Public dwType As Integer
Public dwDisplayType As Integer
Public dwUsage As Integer
Public lpLocalName As String
Public lpRemoteName As String
Public lpComment As String
Public lpProvider As String
End Structure
'API调用:
Public Function ConnnectNet(ByVal strConnPath As String, ByVal strConnAccount As String, ByVal strConnPassword As String) As Boolean
Dim ne(1) As NETRESOURCE
Dim i As Integer
ne(0).lpRemoteName = strConnPath
i = WNetAddConnection2(ne, strConnPassword, strConnAccount, 0)
If i = 0 Then
MsgBox("成功!")
Else
MsgBox("失败!")
End If
End Function