cybergirl
asked on
Example of Drive Mapping
I got this piece of code from an old question that Waty answered about how to map a drive. Can anybody give me sample code or examples on how to use it to map a drive to
machine \\machinename\folder? I am confused about what the
constants are supposed to be.
ie sService and what format sDrive is supposed to be in.
Any help would be greatly appreciated.
P.S. I am making a connection from a Windows NT workstation
to an Auspex server.
Accepted Answer
From: waty
Date: Thursday, August 20 1998 - 11:49PM PDT
Here is a class to connect/disconnect to network drives. I use it in several project :
Option Explicit
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
Const WN_Success = &H0
Const WN_Not_Supported = &H1
Const WN_Net_Error = &H2
Const WN_Bad_Pointer = &H4
Const WN_Bad_NetName = &H32
Const WN_Bad_Password = &H6
Const WN_Bad_Localname = &H33
Const WN_Access_Denied = &H7
Const WN_Out_Of_Memory = &HB
Const WN_Already_Connected = &H34
'-- Error number and message
Public ErrorNum As Long
Public ErrorMsg As String
Public rc As Long
Private Const ERROR_NO_CONNECTION = 8
Private Const ERROR_NO_DISCONNECT = 9
Public Sub Connect(sDrive As String, sService As String, Optional sPassword As String = "")
On Error GoTo Err_Connect
Me.ErrorNum = 0
Me.ErrorMsg = ""
rc = WNetAddConnection(sService & Chr(0), sPassword & Chr(0), sDrive & Chr(0))
If rc <> 0 Then GoTo Err_Connect
Exit Sub
Err_Connect:
Me.ErrorNum = rc
Me.ErrorMsg = WnetError(rc)
End Sub
Public Sub DisConnect(sDrive As String)
On Error GoTo Err_DisConnect
Me.ErrorNum = 0
Me.ErrorMsg = ""
rc = WNetCancelConnection(sDriv e + Chr(0), 0)
If rc <> 0 Then GoTo Err_DisConnect
Exit Sub
Err_DisConnect:
Me.ErrorNum = rc
Me.ErrorMsg = WnetError(rc)
End Sub
Private Function WnetError(Errcode As Long) As String
Select Case Errcode
Case WN_Not_Supported:
WnetError = "Function is not supported."
Case WN_Out_Of_Memory:
WnetError = "Out of Memory."
Case WN_Net_Error:
WnetError = "An error occurred on the network."
Case WN_Bad_Pointer:
WnetError = "The Pointer was Invalid."
Case WN_Bad_NetName:
WnetError = "Invalid Network Resource Name."
Case WN_Bad_Password:
WnetError = "The Password was Invalid."
Case WN_Bad_Localname:
WnetError = "The local device name was invalid."
Case WN_Access_Denied:
WnetError = "A security violation occurred."
Case WN_Already_Connected:
WnetError = "The local device was connected to a remote resource."
Case Else:
WnetError = "Unrecognized Error " + str(Errcode) + "."
End Select
End Function
machine \\machinename\folder? I am confused about what the
constants are supposed to be.
ie sService and what format sDrive is supposed to be in.
Any help would be greatly appreciated.
P.S. I am making a connection from a Windows NT workstation
to an Auspex server.
Accepted Answer
From: waty
Date: Thursday, August 20 1998 - 11:49PM PDT
Here is a class to connect/disconnect to network drives. I use it in several project :
Option Explicit
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
Const WN_Success = &H0
Const WN_Not_Supported = &H1
Const WN_Net_Error = &H2
Const WN_Bad_Pointer = &H4
Const WN_Bad_NetName = &H32
Const WN_Bad_Password = &H6
Const WN_Bad_Localname = &H33
Const WN_Access_Denied = &H7
Const WN_Out_Of_Memory = &HB
Const WN_Already_Connected = &H34
'-- Error number and message
Public ErrorNum As Long
Public ErrorMsg As String
Public rc As Long
Private Const ERROR_NO_CONNECTION = 8
Private Const ERROR_NO_DISCONNECT = 9
Public Sub Connect(sDrive As String, sService As String, Optional sPassword As String = "")
On Error GoTo Err_Connect
Me.ErrorNum = 0
Me.ErrorMsg = ""
rc = WNetAddConnection(sService
If rc <> 0 Then GoTo Err_Connect
Exit Sub
Err_Connect:
Me.ErrorNum = rc
Me.ErrorMsg = WnetError(rc)
End Sub
Public Sub DisConnect(sDrive As String)
On Error GoTo Err_DisConnect
Me.ErrorNum = 0
Me.ErrorMsg = ""
rc = WNetCancelConnection(sDriv
If rc <> 0 Then GoTo Err_DisConnect
Exit Sub
Err_DisConnect:
Me.ErrorNum = rc
Me.ErrorMsg = WnetError(rc)
End Sub
Private Function WnetError(Errcode As Long) As String
Select Case Errcode
Case WN_Not_Supported:
WnetError = "Function is not supported."
Case WN_Out_Of_Memory:
WnetError = "Out of Memory."
Case WN_Net_Error:
WnetError = "An error occurred on the network."
Case WN_Bad_Pointer:
WnetError = "The Pointer was Invalid."
Case WN_Bad_NetName:
WnetError = "Invalid Network Resource Name."
Case WN_Bad_Password:
WnetError = "The Password was Invalid."
Case WN_Bad_Localname:
WnetError = "The local device name was invalid."
Case WN_Access_Denied:
WnetError = "A security violation occurred."
Case WN_Already_Connected:
WnetError = "The local device was connected to a remote resource."
Case Else:
WnetError = "Unrecognized Error " + str(Errcode) + "."
End Select
End Function
i think it's better WATY himself answers this question.
cybergirl,
Try this, let me know if you have questions
Regards
Dalin
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
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
Public Const CONNECT_UPDATE_PROFILE = &H1
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_GENERI C = &H0
Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
Public Const RESOURCEUSAGE_CONNECTABLE = &H1
Public Const RESOURCEUSAGE_CONTAINER = &H2
Private Sub cmdConnect_Click()
Dim NetR As NETRESOURCE
Dim ErrInfo As Long
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = "H:" ' Connect with no device if undefined
NetR.lpRemoteName = "\\AnyVolume\AnyShare"
' 'NetR.lpComment = "Optional Comment"
' 'NetR.lpProvider =' Let the OS decide
' If the UserName and Password arguments are NULL, the infor
' mation on the MSDN ' Library CD says, "The user context for
' the process provides the default user name." 'ErrInfo = WNet
' AddConnection2(NetR, 0, 0, CONNECT_UPDATE_PROFILE)
ErrInfo = WNetAddConnection2(NetR, "MyPassword", "MyUserName", CONNECT_UPDATE_PROFILE)
If ErrInfo = NO_ERROR Then
MsgBox "Net connection successful!", vbInformation, "Share Connected"
Else
MsgBox "ERROR: " & Str(ErrInfo) & " - Net connection unsuccessful!", vbExclamation, _
"Share not Connected"
End If
End Sub
Try this, let me know if you have questions
Regards
Dalin
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
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
Public Const CONNECT_UPDATE_PROFILE = &H1
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
Public Const RESOURCEDISPLAYTYPE_GENERI
Public Const RESOURCEDISPLAYTYPE_SERVER
Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
Public Const RESOURCEUSAGE_CONNECTABLE = &H1
Public Const RESOURCEUSAGE_CONTAINER = &H2
Private Sub cmdConnect_Click()
Dim NetR As NETRESOURCE
Dim ErrInfo As Long
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = "H:" ' Connect with no device if undefined
NetR.lpRemoteName = "\\AnyVolume\AnyShare"
' 'NetR.lpComment = "Optional Comment"
' 'NetR.lpProvider =' Let the OS decide
' If the UserName and Password arguments are NULL, the infor
' mation on the MSDN ' Library CD says, "The user context for
' the process provides the default user name." 'ErrInfo = WNet
' AddConnection2(NetR, 0, 0, CONNECT_UPDATE_PROFILE)
ErrInfo = WNetAddConnection2(NetR, "MyPassword", "MyUserName", CONNECT_UPDATE_PROFILE)
If ErrInfo = NO_ERROR Then
MsgBox "Net connection successful!", vbInformation, "Share Connected"
Else
MsgBox "ERROR: " & Str(ErrInfo) & " - Net connection unsuccessful!", vbExclamation, _
"Share not Connected"
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Dalin...it works really well.
Can you show me how to disconnect? I can increase the points by 25 if you want.
Can you show me how to disconnect? I can increase the points by 25 if you want.