By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,234 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,234 IT Pros & Developers. It's quick & easy.

Problems trying to map drive

P: n/a
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 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

'attempts to connect to the passed network
'connection to the specified drive.
'ErrInfo=ERROR_SUCCESS if successful.

Dim errInfo As Long

.lpRemoteName = sServer
.lpLocalName = sDrv
End With

errInfo = WNetAddConnection2(NETR, _
ThisPassword, _
ThisUser, _
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
Case Else
WnetError = "Unrecognized Error " + Str(Errcode) + "."
End Select
End Function
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a
Hi Stacey,

I changed the API function's declare :

Declare Function WNetAddConnection2 Lib "mpr.dll" Alias
"WNetAddConnection2A" (ByRef lpNetResource As NETRESOURCE, ByVal lpPassword
As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Integer

Just set first parameter 'lpNetResource" to byref, and then it begin to

You may try this on your side.


Nov 21 '05 #2

P: n/a
Hi Stracey,

Does my message answer your question? If you have further question , please
feel free to post here.


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.