VB and VBA Users Source Code: Map or disconnect a network drive or resource
[
Home
|
Contents
|
Search
|
Reply
| Previous |
Next
]
VB/VBA Source Code
Map or disconnect a network drive or resource
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, March 06, 2001
Hits:
7093
Category:
Networks
Article:
Below are three different methods of mapping and removing network drives. Note, method 1, does not require the developer to specify a drive letter for the mapping. Option Explicit 'CONSTANTS Private Const WN_SUCCESS = 0 ' The function was successful. Private Const WN_NET_ERROR = 2 ' An error occurred on the network. Private Const WN_BAD_PASSWORD = 6 ' The password was invalid. 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 Private Const CONNECT_UPDATE_RECENT = &H2 'ERROR CONSTANTS 'Success Private Const ERROR_SUCCESS = 0 'An unexpected network error occurred. Private Const ERROR_UNEXP_NET_ERR = 59& Private Const ERROR_ACCESS_DENIED = 5& Private Const ERROR_ALREADY_ASSIGNED = 85& Private Const ERROR_BAD_DEV_TYPE = 66& Private Const ERROR_BAD_DEVICE = 1200& Private Const ERROR_BAD_NET_NAME = 67& Private Const ERROR_BAD_PROFILE = 1206& Private Const ERROR_BAD_PROVIDER = 1204& Private Const ERROR_BUSY = 170& Private Const ERROR_CANNOT_OPEN_PROFILE = 1205& Private Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202& Private Const ERROR_BAD_NETPATH = 53& Private Const ERROR_EXTENDED_ERROR = 1208& Private Const ERROR_INVALID_PASSWORD = 86& Private Const ERROR_NO_NET_OR_BAD_PATH = 1203& Private Const ERROR_NO_NETWORK = 1222& Private Const ERROR_SESSION_CREDENTIAL_CONFLICT = 1219 Private Const ERROR_BAD_USERNAME = 2202& Private Const ERROR_ACCOUNT_LOCKED = 1909 'logon failure: unknown user name or bad password. Private Const ERROR_LOGON_FAILURE = 1326& 'Logon failure: user account restriction. Private Const ERROR_ACCOUNT_RESTRICTION = 1327& 'Logon failure: account logon time restriction violation. Private Const ERROR_INVALID_LOGON_HOURS = 1328& 'Logon failure: user not allowed to log on to this computer. Private Const ERROR_INVALID_WORKSTATION = 1329& 'Logon failure: the specified account password has expired. Private Const ERROR_PASSWORD_EXPIRED = 1330& Private Const ERROR_ACCOUNT_DISABLED = 1331& '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 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 Declare Function GetActiveWindow Lib "user32" () As Long Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long Private Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" (ByVal lpszName As String, ByVal bForce As Long) As Long 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 '----------------METHOD 1------------------------ 'Purpose : Connects to a network resource using the next free mapped drive letter. 'Inputs : sSharePath The path to the network resource. ' [sDriveLetter] See outputs ' [sPassword] The password to be used to make a connection. ' [sUserName] If sUserName is empty, uses the default user name. Note, the sUserName parameter ' is specified when users want to connect to a network resource for which they have ' been assigned a user name or account other than the default user name or account. 'Outputs : [sDriveLetter] Empty if failed, else the letter of the new mapping. ' Returns zero on success, else returns an error number. 'Author : Andrew Baker 'Date : 14/01/2001 12:20 'Notes : 'Revisions : Function ConnectionAdd(ByVal sSharePath As String, Optional ByRef sDriveLetter As String, Optional sPassword As String = vbNullString, Optional sUserName As String = vbNullString) As Long Const clMaxLen As Long = 32 Dim tNetResource As NETRESOURCE 'NetResouce structure Dim sBuffer As String * clMaxLen 'Drive letter assigned to resource Dim lSuccess As Long 'Set Variables tNetResource.dwScope = RESOURCE_GLOBALNET tNetResource.dwType = RESOURCETYPE_DISK tNetResource.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE tNetResource.dwUsage = RESOURCEUSAGE_CONNECTABLE tNetResource.lpLocalName = vbNullString tNetResource.lpRemoteName = sSharePath 'Call API to map the drive ConnectionAdd = WNetUseConnection(GetActiveWindow, tNetResource, sPassword, sUserName, CONNECT_REDIRECT, sBuffer, clMaxLen, lSuccess) If ConnectionAdd <> NO_ERROR Then '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?! So try reversing these two parameters. ConnectionAdd = WNetUseConnection(GetActiveWindow, tNetResource, sUserName, sPassword, CONNECT_REDIRECT, sBuffer, clMaxLen, lSuccess) End If If (ConnectionAdd = NO_ERROR) And (lSuccess = CONNECT_LOCALDRIVE) Then 'Success 'Return the mapped drive letter sDriveLetter = Left$(sBuffer, InStr(1, sBuffer, ":")) Else 'Failed sDriveLetter = "" End If End Function 'Purpose : Deletes a connection to a network resource 'Inputs : sDriveLetter The name of a local device (eg. "Z:" or "LPT1") ' [bForce] Specifies whether the disconnection is to occur even if there are open ' files or jobs on the connection. If this parameter is FALSE, the function ' fails if there are open files or jobs. 'Outputs : Returns zero on success, else returns an error number ' else returns an error description. 'Author : Andrew Baker 'Date : 14/01/2001 12:20 'Notes : 'Revisions : Function ConnectionDelete(ByVal sDriveLetter As String, Optional bForce As Boolean = False) As Long On Error Resume Next sDriveLetter = Right$(sDriveLetter, 1) & ":" ConnectionDelete = WNetCancelConnection2(sDriveLetter, CONNECT_UPDATE_PROFILE, bForce) On Error GoTo 0 End Function '----------------METHOD 2------------------------ 'Purpose : Connects to a network resource 'Inputs : sDriveLetter The name of a local device (eg. "Z:" or "LPT1") ' sSharePath The path to the network resource. ' [sPassword] The password to be used to make a connection. 'Outputs : Returns zero on success, else returns an error number 'Author : Andrew Baker 'Date : 14/01/2001 12:20 'Notes : 'Revisions : Function ConnectionAdd2(sDriveLetter As String, sSharePath As String, Optional sPassword As String = vbNullString) As Long On Error Resume Next ConnectionAdd2 = WNetAddConnection(ByVal sSharePath, ByVal sPassword, ByVal sDriveLetter) On Error GoTo 0 End Function 'Purpose : Deletes a connection to a network resource 'Inputs : sDriveLetter The name of a local device (eg. "Z:" or "LPT1") ' [bForce] Specifies whether the disconnection is to occur even if there are open ' files or jobs on the connection. If this parameter is FALSE, the function ' fails if there are open files or jobs. 'Outputs : Returns zero on success, else returns an error number 'Author : Andrew Baker 'Date : 14/01/2001 12:20 'Notes : 'Revisions : Function ConnectionDelete2(ByVal sDriveLetter As String, Optional bForce As Boolean = False) As Long On Error Resume Next sDriveLetter = Right$(sDriveLetter, 1) & ":" ConnectionDelete2 = WNetCancelConnection(sDriveLetter, bForce) On Error GoTo 0 End Function '----------------METHOD 3------------------------ 'Purpose : Connects to a network resource 'Inputs : sSharePath The path to the network resource. ' [sDriveLetter] See outputs ' [sPassword] The password to be used to make a connection. ' [sUserName] If sUserName is empty, uses the default user name. Note, the sUserName parameter ' is specified when users want to connect to a network resource for which they have ' been assigned a user name or account other than the default user name or account. 'Outputs : Returns zero on success, else returns an error number. 'Author : Andrew Baker 'Date : 14/01/2001 12:20 'Notes : 'Revisions : Function ConnectionAdd3(ByVal sSharePath As String, Optional ByRef sDriveLetter As String, Optional sPassword As String = vbNullString, Optional sUserName As String = vbNullString, Optional sComment As String) As Long Dim tNetR As NETRESOURCE Dim lRetVal As Long tNetR.dwScope = RESOURCE_GLOBALNET tNetR.dwType = RESOURCETYPE_DISK tNetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE tNetR.dwUsage = RESOURCEUSAGE_CONNECTABLE tNetR.lpLocalName = sDriveLetter tNetR.lpRemoteName = sSharePath tNetR.lpComment = sComment ConnectionAdd3 = WNetAddConnection2(tNetR, sPassword, sUserName, CONNECT_UPDATE_PROFILE) End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder