上一篇 | 下一篇

设定/取消 网络磁盘

发布: 2008-6-26 09:04 | 作者: admin | 来源: | 查看: 1次

使用方法:

建立连线

Function AddConnection(ByVal RemoteLocation As String, _

ByVal LocalDriver As String, _

ByVal Passwd As String, _

ByVal UserName As String) As Boolean

RemoteLocation : 为网络磁盘原始来源,格式为

\\RemoteComputer\ShareDirectory

LocalDriver : 对应本机的磁盘代号,如: "H:"

Passwd : 存取网络磁盘的Password,传Null表示不用密码

UserName : 存取网络磁盘的使用者代号

成功时传回True,否则为False

例:

Call AddConnection("\\Shih\cmias", "x:", vbNullString, "cww")

取消连线

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

ne.dwDisplayType = RESOURCEDISPLAYTYPE_GENERIC

ne.dwScope = RESOURCE_PUBLICNET

ne.dwType = RESOURCETYPE_ANY

ne.dwUsage = RESOURCEUSAGE_CONNECTABLE

ne.lpComment = vbNullString

ne.lpLocalName = LocalDriver

ne.lpProvider = vbNullString

ne.lpRemoteName = RemoteLocation

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

End Function

字号: | 推荐给好友

评分:0

我来说两句