
How To Map Drive To The Network?
Bryan, we recently had to do that in writing a VB program to access a SQL
database on a remote webserver. We tried to do a network connect, but the
easiest way was to map and unmap the drive. We put this code into a module
and added it to the project. Most of it came from utilizing the API text
viewer, and then some fine tuning after. The variables DataServer,
DriveLetter, LoginAccount and LoginPassword are defined as public variables
in the primary module.
-------
Public 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
'Public 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
Public Declare Function WNetCancelConnection2 Lib "mpr.dll" _
Alias "WNetCancelConnection2A" ( _
ByVal lpName As String, _
ByVal dwFlags As Long, _
ByVal fForce As Long) _
As Long
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
Public Const NO_ERROR = 0
Private Const CONNECT_LOCALDRIVE = 256
Private Const CONNECT_REDIRECT = 128
Public Const CONNECT_UPDATE_PROFILE = &H1
' The following includes all the constants defined for NETRESOURCE,
' not just the ones used in this example.
Public Const RESOURCETYPE_DISK = &H1
Public Const RESOURCETYPE_PRINT = &H2
Public Const RESOURCETYPE_ANY = &H0
Public Const RESOURCE_CONNECTED = &H1
Public Const RESOURCE_REMEMBERED = &H3
Public Const RESOURCE_GLOBALNET = &H2
Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
Public Const RESOURCEUSAGE_CONNECTABLE = &H1
Public Const RESOURCEUSAGE_CONTAINER = &H2
' Error Constants:
Public Const ERROR_ACCESS_DENIED = 5&
Public Const ERROR_ALREADY_ASSIGNED = 85&
Public Const ERROR_BAD_DEV_TYPE = 66&
Public Const ERROR_BAD_DEVICE = 1200&
Public Const ERROR_BAD_NET_NAME = 67&
Public Const ERROR_BAD_PROFILE = 1206&
Public Const ERROR_BAD_PROVIDER = 1204&
Public Const ERROR_BUSY = 170&
Public Const ERROR_CANCELLED = 1223&
Public Const ERROR_CANNOT_OPEN_PROFILE = 1205&
Public Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
Public Const ERROR_EXTENDED_ERROR = 1208&
Public Const ERROR_INVALID_PASSWORD = 86&
Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&
Public Function ConnectNetworkDrive(ByVal DriveLetter As String) As
Boolean
Dim NetR As NETRESOURCE
Dim ErrInfo As Long
Dim MyPass As String, MyUser As String
Dim buffer As String
Dim bufferlen As Long, success As Long
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = DriveLetter & ":"
NetR.lpRemoteName = "\\" & DataServer & "\Inet"
buffer = Space(32)
bufferlen = Len(buffer)
ErrInfo = WNetAddConnection2(NetR, LoginPassword, LoginAccount,
CONNECT_UPDATE_PROFILE)
If ErrInfo = NO_ERROR Then
ConnectNetworkDrive = True
Else
ConnectNetworkDrive = False
End If
End Function
Public Function DisconnectNetworkDrive(ByVal DriveLetter As String) As
Boolean
Dim ErrInfo As Long
Dim strLocalName As String
strLocalName = DriveLetter & ":"
ErrInfo = WNetCancelConnection2(strLocalName, CONNECT_UPDATE_PROFILE,
False)
If ErrInfo = NO_ERROR Then
DisconnectNetworkDrive = True
Else
DisconnectNetworkDrive = False
End If
End Function
Quote:
> How To Map Drive To The Network?
> Do you know which API function that I can use? I put UNC in this
> string. API function reads UNC string before it is remapped itself.
> --
> Regards,
> Bryan Parkoff