We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Mapping Drive Letter - Error Codes

escheider
escheider asked
on
Medium Priority
2,901 Views
Last Modified: 2012-06-22
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.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
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.
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Sub Map_S_Drive()
  On Error Resume Next
     
  Const User = "UserName"
  Const Password = "Password"
     
  Dim NW, FSO
     
  Set FSO = CreateObject("Scripting.FileSystemObject")
     
  If not FSO.DriveExists("S:") Then
    Set NW = CreateObject("WScript.Network")
    NW.MapNetworkDrive "S:", "\\Server\share$", False, User, Password
    Set NW = Nothing
  End If
     
  If FSO.DriveExists("S:") Then
    MsgBox("S drive is mapped.")
  Else
    MsgBox("S drive did NOT map!" & vbcrlf & vbcrlf & Err.description)
  End If    
     
  Set FSO = Nothing
End Sub
 
 
 
CERTIFIED EXPERT

Commented:
what version of Windows you using?

Author

Commented:
This application will either be running on Windows XP Home/Professional or Windows 2000.  

The most important aspect though is getting a list of the returned error codes.  The user provides the username and password information, so for instance, the credentials they use to map the drive may be invalid.
CERTIFIED EXPERT

Commented:
The Above Code should do it did you try it?

Author

Commented:
It doesn't appear that it will validate username and password and return an error code otherwise
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
I ran a test by using the incorrect credentials and it returned an response code of 85.  Clearly outside the range you have specified.

Author

Commented:
Here is the line that returns 85

ErrInfo = WNetUseConnection(FormHwnd, NetR, UserName, _
      Password, CONNECT_REDIRECT, buffer, bufferlen, success)

ErrInfo=85

Author

Commented:
I have found the resource I need which included additional error codes not specified by dave.  Thanks for all the input.
Use this function:

Public Function Fun_GetAPIErrorString(lngErrorNumber As Long) As String

On Error Resume Next

Dim Buffer As String
'Create a string buffer
Buffer = String$(200, Chr$(0)) ' Space(200)
'Format the message string
FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, lngErrorNumber, LANG_NEUTRAL, Buffer, 200, ByVal 0&
'Show the message
Fun_GetAPIErrorString = Left$(Buffer, InStr(1, Buffer, Chr$(0)) - 3)

End Function


The declaration for FormatMessage API is:
Public Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long


Usage:
MsgBox Fun_GetAPIErrorString(85)
You can use this function to get any OS error number as string (As you said, you get error with number 85 which is not in constant list above)
Constant values:
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Public Const LANG_NEUTRAL = &H0
escheider could you post the additional constants you found as I thought I had them all. It is part of a windows error class I use so would be nice to be concise, so to speak...

Thanks
Dave
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.