Option Explicit' CONSTANTS Private Const NO_ERROR = 0 Private Const CONNECT_LOCALDRIVE = 256 Private Const CONNECT_REDIRECT = 128 Private Const RESOURCE_GLOBALNET = &H2 Private Const RESOURCETYPE_DISK = &H1 Private Const RESOURCEDISPLAYTYPE_SHARE = &H3 Private Const RESOURCEUSAGE_CONNECTABLE = &H1 Private Const CONNECT_UPDATE_PROFILE = 1' LOCAL VARIABLES Private MappedDrive As String' CUSTOM TYPES 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' API DECLARATIONS Private Declare Function WNetUseConnection Lib "mpr.dll" _ Alias "WNetUseConnectionA" ( _ ByVal hwndOwner As Long, _ ByRef lpNetResource As NETRESOURCE, _ ByVal lpUsername As String, _ ByVal lpPassword As String, _ ByVal dwFlags As Long, _ ByVal lpAccessName As Any, _ ByRef lpBufferSize As Long, _ ByRef lpResult As Long) _ As LongPrivate Declare Function WNetCancelConnection2 Lib "mpr.dll" _ Alias "WNetCancelConnection2A" ( _ ByVal lpName As String, _ ByVal dwFlags As Long, _ ByVal fForce As Long) _ As LongPrivate Sub Command1_Click() Dim NetR As NETRESOURCE ' NetResouce structure Dim ErrInfo As Long ' Return value from API Dim buffer As String ' Drive letter assigned to resource Dim bufferlen As Long ' Size of the buffer Dim success As Long ' Additional info about API call
' Initialize the NetResouce structure NetR.dwScope = RESOURCE_GLOBALNET NetR.dwType = RESOURCETYPE_DISK NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE NetR.lpLocalName = vbNullString NetR.lpRemoteName = txtUNC.Text ' Initialize the return buffer and buffer size buffer = Space(32) bufferlen = Len(buffer) ' Call API to map the drive ErrInfo = WNetUseConnection(Me.hWnd, NetR, txtPWD.Text, txtUser.Text, _ CONNECT_REDIRECT, buffer, bufferlen, success) ' Check if call to API failed. According to the MSDN help, there ' are some versions of the operating system that expect the userid ' as the 3rd parameter and the password as the 4th, while other ' versions of the operating system have them in reverse order, so ' if first call to API fails, try reversing these two parameters. If ErrInfo <> NO_ERROR Then ' Call API with userid and password switched ErrInfo = WNetUseConnection(Me.hWnd, NetR, txtUser.Text, _ txtPWD.Text, CONNECT_REDIRECT, buffer, bufferlen, success) End If ' Check for success If (ErrInfo = NO_ERROR) And (success = CONNECT_LOCALDRIVE) Then ' Store the mapped drive letter for later usage MappedDrive = Left$(buffer, InStr(1, buffer, ":")) ' Display the mapped drive letter MsgBox "Connect Succeeded to " & MappedDrive Else MsgBox "ERROR: " & Str(ErrInfo) & " - Connect Failed!" End If End SubPrivate Sub Command2_Click() Dim ErrInfo As Long ' Return value from API ' Call API to disconnect the drive ErrInfo = WNetCancelConnection2(MappedDrive, _ CONNECT_UPDATE_PROFILE, False) ' Check for success If ErrInfo = NO_ERROR Then MsgBox "Disconnect of '" & MappedDrive & "' Succeeded"
' Clear the mapped drive letter MappedDrive = "" Else MsgBox "ERROR: " & Str(ErrInfo) & " - Disconnect Failed!" End If End Sub
Private Const NO_ERROR = 0
Private Const CONNECT_LOCALDRIVE = 256
Private Const CONNECT_REDIRECT = 128
Private Const RESOURCE_GLOBALNET = &H2
Private Const RESOURCETYPE_DISK = &H1
Private Const RESOURCEDISPLAYTYPE_SHARE = &H3
Private Const RESOURCEUSAGE_CONNECTABLE = &H1
Private Const CONNECT_UPDATE_PROFILE = 1' LOCAL VARIABLES
Private MappedDrive As String' CUSTOM TYPES
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' API DECLARATIONS
Private Declare Function WNetUseConnection Lib "mpr.dll" _
Alias "WNetUseConnectionA" ( _
ByVal hwndOwner As Long, _
ByRef lpNetResource As NETRESOURCE, _
ByVal lpUsername As String, _
ByVal lpPassword As String, _
ByVal dwFlags As Long, _
ByVal lpAccessName As Any, _
ByRef lpBufferSize As Long, _
ByRef lpResult As Long) _
As LongPrivate Declare Function WNetCancelConnection2 Lib "mpr.dll" _
Alias "WNetCancelConnection2A" ( _
ByVal lpName As String, _
ByVal dwFlags As Long, _
ByVal fForce As Long) _
As LongPrivate Sub Command1_Click()
Dim NetR As NETRESOURCE ' NetResouce structure
Dim ErrInfo As Long ' Return value from API
Dim buffer As String ' Drive letter assigned to resource
Dim bufferlen As Long ' Size of the buffer
Dim success As Long ' Additional info about API call
' Initialize the NetResouce structure
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = vbNullString
NetR.lpRemoteName = txtUNC.Text ' Initialize the return buffer and buffer size
buffer = Space(32)
bufferlen = Len(buffer) ' Call API to map the drive
ErrInfo = WNetUseConnection(Me.hWnd, NetR, txtPWD.Text, txtUser.Text, _
CONNECT_REDIRECT, buffer, bufferlen, success) ' Check if call to API failed. According to the MSDN help, there
' are some versions of the operating system that expect the userid
' as the 3rd parameter and the password as the 4th, while other
' versions of the operating system have them in reverse order, so
' if first call to API fails, try reversing these two parameters.
If ErrInfo <> NO_ERROR Then
' Call API with userid and password switched
ErrInfo = WNetUseConnection(Me.hWnd, NetR, txtUser.Text, _
txtPWD.Text, CONNECT_REDIRECT, buffer, bufferlen, success)
End If ' Check for success
If (ErrInfo = NO_ERROR) And (success = CONNECT_LOCALDRIVE) Then
' Store the mapped drive letter for later usage
MappedDrive = Left$(buffer, InStr(1, buffer, ":")) ' Display the mapped drive letter
MsgBox "Connect Succeeded to " & MappedDrive
Else
MsgBox "ERROR: " & Str(ErrInfo) & " - Connect Failed!"
End If
End SubPrivate Sub Command2_Click()
Dim ErrInfo As Long ' Return value from API ' Call API to disconnect the drive
ErrInfo = WNetCancelConnection2(MappedDrive, _
CONNECT_UPDATE_PROFILE, False) ' Check for success
If ErrInfo = NO_ERROR Then
MsgBox "Disconnect of '" & MappedDrive & "' Succeeded"
' Clear the mapped drive letter
MappedDrive = ""
Else
MsgBox "ERROR: " & Str(ErrInfo) & " - Disconnect Failed!"
End If
End Sub
shikari 我看看先;load 邮箱贴出来了,有6M,应该足够;DGZ01 怎么在VB里用?谢谢你们先!请继续关注!