| Adding and Removing Network Connections I have had a huge response to a Tip that I posted on VB-World early in 1998 that the code does not work, so below you will find the correct listings. Call either of the two functions. The share name is the path on the server (i.e., "\\MW\Public") and the mapped drive is the local drive letter that the share path is mapped to: (i.e., "P:"). Entering a username or password is optional, but if you don't include a username then the default username will be used. If there is no password then leave it as NULL. Option ExplicitPrivate 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 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 CONNECT_UPDATE_PROFILE = &H1 Const RESOURCETYPE_DISK = &H1 Const RESOURCE_CONNECTED = &H1 Const RESOURCE_GLOBALNET = &H2 Const RESOURCEDISPLAYTYPE_SHARE = &H3 Const RESOURCEUSAGE_CONNECTABLE = &H1 ' ///////////////////////// ' // ' Winnet32 Error Codes // ' // ' ///////////////////////// ' Connection Successful Const NO_ERROR = 0 ' The specified username is invalid. Const ERROR_BAD_USERNAME = 2202& ' This network connection does not exist. Const ERROR_NOT_CONNECTED = 2250& ' This network connection has files open or requests pending. Const ERROR_OPEN_FILES = 2401& ' The device is in use by an active process and cannot be disconnected. Const ERROR_DEVICE_IN_USE = 2404& ' The specified device name is invalid. Const ERROR_BAD_DEVICE = 1200& ' The device is not currently connected but it is a remembered connection. Const ERROR_CONNECTION_UNAVAIL = 1201& ' An attempt was made to remember a device that had previously been remembered. Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202& ' No network provider accepted the given network path. Const ERROR_NO_NET_OR_BAD_PATH = 1203& ' The specified network provider name is invalid. Const ERROR_BAD_PROVIDER = 1204& ' Unable to open the network connection profile. Const ERROR_CANNOT_OPEN_PROFILE = 1205& ' The network connection profile is corrupt. Const ERROR_BAD_PROFILE = 1206& ' Cannot enumerate a non-container. Const ERROR_NOT_CONTAINER = 1207& ' An extended error has occurred. Const ERROR_EXTENDED_ERROR = 1208& ' The format of the specified group name is invalid. Const ERROR_INVALID_GROUPNAME = 1209& ' The format of the specified computer name is invalid. Const ERROR_INVALID_COMPUTERNAME = 1210& ' The format of the specified event name is invalid. Const ERROR_INVALID_EVENTNAME = 1211& ' The format of the specified domain name is invalid. Const ERROR_INVALID_DOMAINNAME = 1212& ' The format of the specified service name is invalid. Const ERROR_INVALID_SERVICENAME = 1213& ' The format of the specified network name is invalid. Const ERROR_INVALID_NETNAME = 1214& ' The format of the specified share name is invalid. Const ERROR_INVALID_SHARENAME = 1215& ' The format of the specified password is invalid. Const ERROR_INVALID_PASSWORDNAME = 1216& ' The format of the specified message name is invalid. Const ERROR_INVALID_MESSAGENAME = 1217& ' The format of the specified message destination is invalid. Const ERROR_INVALID_MESSAGEDEST = 1218& ' The credentials supplied conflict with an existing set of credentials. Const ERROR_SESSION_CREDENTIAL_CONFLICT = 1219& ' An attempt was made to establish a session to a Lan Manager server, but there ' are already too many sessions established to that server. Const ERROR_REMOTE_SESSION_LIMIT_EXCEEDED = 1220& ' The workgroup or domain name is already in use by another computer on the ' network. Const ERROR_DUP_DOMAINNAME = 1221& ' The network is not present or not started. Const ERROR_NO_NETWORK = 1222& Public Function AddConnection(sServerPath As String, sMap2Drive As String, Optional sUserName As String = "", Optional sPassword As String = "") As Long Dim tNetwork As NETRESOURCE Dim lTemp As Long With tNetwork .dwScope = RESOURCE_GLOBALNET .dwType = RESOURCETYPE_DISK .dwDisplayType = RESOURCEDISPLAYTYPE_SHARE .dwUsage = RESOURCEUSAGE_CONNECTABLE .lpLocalName = sMap2Drive .lpRemoteName = sServerPath End With lTemp = WNetAddConnection2(tNetwork, sPassword, sUserName, CONNECT_UPDATE_PROFILE) AddConnection = lTemp End Function Public Function RemoveConnection(sMappedDrive) As Long Dim lTemp As Long lTemp = WNetCancelConnection2(sMappedDrive, CONNECT_UPDATE_PROFILE, False) RemoveConnection = lTemp End Function |
___________________________________________ Send Bugs, comments to the webmaster at webmaster@mwe.8m.com © 1999 MW Software, all rights reserved |