S
Stacey Levine
I have been unsuccessfully trying to map network drives using user/password
credentials. After spending lots of time looking around.. it seems like the
way to go would be to use WNetAddConnection2.. but when I do.. i always
get a 487 error message. Any ideas would be greatly appreaciated.
The code below gets called with something along the lines of :
ConnectThisNetworkDrive("\\MyComp\S", "Z:", ThisUser, ThisPassword)
Private Structure NETRESOURCE
Dim dwScope As Integer
Dim dwType As Integer
Dim dwDisplayType As Integer
Dim dwUsage As Integer
Dim lpLocalName As String
Dim lpRemoteName As String
Dim lpComment As String
Dim lpProvider As String
End Structure
Private Const ERROR_SUCCESS As Int16 = 0
Private Const CONNECT_UPDATE_PROFILE As Long = &H1
Private Const RESOURCETYPE_DISK As Long = &H1
Private Const RESOURCETYPE_PRINT As Long = &H2
Private Const RESOURCETYPE_ANY As Long = &H0
Private Const RESOURCE_GLOBALNET As Long = &H2
Private Const RESOURCEDISPLAYTYPE_SHARE As Long = &H3
Private Const RESOURCEUSAGE_CONNECTABLE As Long = &H1
Private Const WN_Success As Long = &H0
Private Const WN_Not_Supported As Long = &H1
Private Const WN_Net_Error As Long = &H2
Private Const WN_Bad_Pointer As Long = &H4
Private Const WN_Bad_NetName As Long = &H32
Private Const WN_Bad_Password As Long = &H6
Private Const WN_Bad_Localname As Long = &H33
Private Const WN_Access_Denied As Long = &H7
Private Const WN_Out_Of_Memory As Long = &HB
Private Const WN_Already_Connected As Long = &H34
Private Declare Function WNetAddConnection2 Lib "mpr" _
Alias "WNetAddConnection2A" _
(ByVal lpNetResource As NETRESOURCE, _
ByVal lpPassword As String, _
ByVal lpUserName As String, _
ByVal dwFlags As Long) As Long
Private Function ConnectThisNetworkDrive(ByVal sServer As String, ByVal
sDrv As String, ByVal ThisUser As String, ByVal ThisPassword As String) As
Boolean
'attempts to connect to the passed network
'connection to the specified drive.
'ErrInfo=ERROR_SUCCESS if successful.
Dim NETR As NETRESOURCE
Dim errInfo As Long
With NETR
.dwScope = RESOURCE_GLOBALNET
.dwType = RESOURCETYPE_DISK
.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
.dwUsage = RESOURCEUSAGE_CONNECTABLE
.lpRemoteName = sServer
.lpLocalName = sDrv
End With
errInfo = WNetAddConnection2(NETR, _
ThisPassword, _
ThisUser, _
CONNECT_UPDATE_PROFILE)
If errInfo <> 0 Then
MessageBox.Show("Error Mapping Drive : " & WnetError(errInfo),
"Drive map error.", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End If
ConnectThisNetworkDrive = errInfo = ERROR_SUCCESS
End Function
Private Function WnetError(ByVal 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
credentials. After spending lots of time looking around.. it seems like the
way to go would be to use WNetAddConnection2.. but when I do.. i always
get a 487 error message. Any ideas would be greatly appreaciated.
The code below gets called with something along the lines of :
ConnectThisNetworkDrive("\\MyComp\S", "Z:", ThisUser, ThisPassword)
Private Structure NETRESOURCE
Dim dwScope As Integer
Dim dwType As Integer
Dim dwDisplayType As Integer
Dim dwUsage As Integer
Dim lpLocalName As String
Dim lpRemoteName As String
Dim lpComment As String
Dim lpProvider As String
End Structure
Private Const ERROR_SUCCESS As Int16 = 0
Private Const CONNECT_UPDATE_PROFILE As Long = &H1
Private Const RESOURCETYPE_DISK As Long = &H1
Private Const RESOURCETYPE_PRINT As Long = &H2
Private Const RESOURCETYPE_ANY As Long = &H0
Private Const RESOURCE_GLOBALNET As Long = &H2
Private Const RESOURCEDISPLAYTYPE_SHARE As Long = &H3
Private Const RESOURCEUSAGE_CONNECTABLE As Long = &H1
Private Const WN_Success As Long = &H0
Private Const WN_Not_Supported As Long = &H1
Private Const WN_Net_Error As Long = &H2
Private Const WN_Bad_Pointer As Long = &H4
Private Const WN_Bad_NetName As Long = &H32
Private Const WN_Bad_Password As Long = &H6
Private Const WN_Bad_Localname As Long = &H33
Private Const WN_Access_Denied As Long = &H7
Private Const WN_Out_Of_Memory As Long = &HB
Private Const WN_Already_Connected As Long = &H34
Private Declare Function WNetAddConnection2 Lib "mpr" _
Alias "WNetAddConnection2A" _
(ByVal lpNetResource As NETRESOURCE, _
ByVal lpPassword As String, _
ByVal lpUserName As String, _
ByVal dwFlags As Long) As Long
Private Function ConnectThisNetworkDrive(ByVal sServer As String, ByVal
sDrv As String, ByVal ThisUser As String, ByVal ThisPassword As String) As
Boolean
'attempts to connect to the passed network
'connection to the specified drive.
'ErrInfo=ERROR_SUCCESS if successful.
Dim NETR As NETRESOURCE
Dim errInfo As Long
With NETR
.dwScope = RESOURCE_GLOBALNET
.dwType = RESOURCETYPE_DISK
.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
.dwUsage = RESOURCEUSAGE_CONNECTABLE
.lpRemoteName = sServer
.lpLocalName = sDrv
End With
errInfo = WNetAddConnection2(NETR, _
ThisPassword, _
ThisUser, _
CONNECT_UPDATE_PROFILE)
If errInfo <> 0 Then
MessageBox.Show("Error Mapping Drive : " & WnetError(errInfo),
"Drive map error.", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End If
ConnectThisNetworkDrive = errInfo = ERROR_SUCCESS
End Function
Private Function WnetError(ByVal 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