troubleshooting Question

Mapping Drive Letter - Error Codes

Avatar of escheider
escheider asked on
Visual Basic Classic
13 Comments1 Solution2918 ViewsLast Modified:
Hello experts:

Using an experts previous advice, I am using the following code to map a drive letter:

Public Function MapDrive(FormHwnd As Long, _
        LocalDrive As String, _
        RemoteDrive As String, _
        Optional UserName As String, _
        Optional Password As String) _
        As Boolean

Dim NetR As NETRESOURCE    ' NetResouce structure
Dim ErrInfo As Long        ' Return value from API
Dim buffer As String       ' Drive letter assigned to resource
Dim bufferlen As Long      ' Size of the buffer
Dim success As Long        ' Additional info about API call

' Initialize the NetResouce structure
NetR.lpLocalName = LocalDrive 'vbNullString
NetR.lpRemoteName = RemoteDrive

' Initialize the return buffer and buffer size
buffer = Space(32)
bufferlen = Len(buffer)

' Call API to map the drive
ErrInfo = WNetUseConnection(FormHwnd, NetR, Password, UserName, _
   CONNECT_REDIRECT, buffer, bufferlen, success)

' Check if call to API failed. According to the MSDN help, there
' are some versions of the operating system that expect the userid
' as the 3rd parameter and the password as the 4th, while other
' versions of the operating system have them in reverse order, so
' if first call to API fails, try reversing these two parameters.
If ErrInfo <> NO_ERROR Then
   ' Call API with userid and password switched
   ErrInfo = WNetUseConnection(FormHwnd, NetR, UserName, _
      Password, CONNECT_REDIRECT, buffer, bufferlen, success)
End If

' Check for success
If (ErrInfo = NO_ERROR) And (success = CONNECT_LOCALDRIVE) Then
   ' Store the mapped drive letter for later usage
   MappedDrive = Left$(buffer, InStr(1, buffer, ":"))

   ' Display the mapped drive letter
   'MsgBox "Connect Succeeded to " & MappedDrive
End If

End Function

My question is concerning the error codes that this api returns.  Where can I get a listing of the error codes that this api returns?  It seems that if an error is returned, no action is performed, but the application this resides in requires this drive letter to be mapped.

Any help appreciated.  If there is a better way to map the drive letter, I'll accept that as well.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 13 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros