Solved

Mapping Drive Letter - Error Codes

Posted on 2004-04-02
13
2,766 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.
0
Comment
Question by:escheider
  • 5
  • 3
  • 3
  • +1
13 Comments
 
LVL 48

Expert Comment

by:Mikal613
Comment Utility
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
 
 
 
0
 
LVL 48

Expert Comment

by:Mikal613
Comment Utility
what version of Windows you using?
0
 
LVL 4

Author Comment

by:escheider
Comment Utility
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.
0
 
LVL 48

Expert Comment

by:Mikal613
Comment Utility
The Above Code should do it did you try it?
0
 
LVL 4

Author Comment

by:escheider
Comment Utility
It doesn't appear that it will validate username and password and return an error code otherwise
0
 
LVL 2

Accepted Solution

by:
DaveHavard earned 50 total points
Comment Utility
Here are all the Error Codes:

'   The specified username is invalid.
     ERROR_BAD_USERNAME = 2202&

'   This network connection does not exist.
     ERROR_NOT_CONNECTED = 2250&

'   This network connection has files open or requests pending.
     ERROR_OPEN_FILES = 2401&

'   The device is in use by an active process and cannot be disconnected.
     ERROR_DEVICE_IN_USE = 2404&

'   The specified device name is invalid.
     ERROR_BAD_DEVICE = 1200&

'   The device is not currently connected but it is a remembered connection.
     ERROR_CONNECTION_UNAVAIL = 1201&

'   An attempt was made to remember a device that had previously been remembered.
     ERROR_DEVICE_ALREADY_REMEMBERED = 1202&

'   No network provider accepted the given network path.
     ERROR_NO_NET_OR_BAD_PATH = 1203&

'   The specified network provider name is invalid.
     ERROR_BAD_PROVIDER = 1204&

'   Unable to open the network connection profile.
     ERROR_CANNOT_OPEN_PROFILE = 1205&

'   The network connection profile is corrupt.
     ERROR_BAD_PROFILE = 1206&

'   Cannot enumerate a non-container.
     ERROR_NOT_CONTAINER = 1207&

'   An extended error has occurred.
     ERROR_EXTENDED_ERROR = 1208&

'   The format of the specified group name is invalid.
     ERROR_INVALID_GROUPNAME = 1209&

'   The format of the specified computer name is invalid.
     ERROR_INVALID_COMPUTERNAME = 1210&

'   The format of the specified event name is invalid.
     ERROR_INVALID_EVENTNAME = 1211&

'   The format of the specified domain name is invalid.
     ERROR_INVALID_DOMAINNAME = 1212&

'   The format of the specified service name is invalid.
     ERROR_INVALID_SERVICENAME = 1213&

'   The format of the specified network name is invalid.
     ERROR_INVALID_NETNAME = 1214&

'   The format of the specified share name is invalid.
     ERROR_INVALID_SHARENAME = 1215&

'   The format of the specified password is invalid.
     ERROR_INVALID_PASSWORDNAME = 1216&

'   The format of the specified message name is invalid.
     ERROR_INVALID_MESSAGENAME = 1217&

'   The format of the specified message destination is invalid.
     ERROR_INVALID_MESSAGEDEST = 1218&

'   The credentials supplied conflict with an existing set of credentials.
     ERROR_SESSION_CREDENTIAL_CONFLICT = 1219&

'   An attempt was made to establish a session to a Lan Manager server, but there
'   are already too many sessions established to that server.
     ERROR_REMOTE_SESSION_LIMIT_EXCEEDED = 1220&

'   The workgroup or domain name is already in use by another computer on the
'   network.
     ERROR_DUP_DOMAINNAME = 1221&

'   The network is not present or not started.
     ERROR_NO_NETWORK = 1222&


Dave
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Author Comment

by:escheider
Comment Utility
I ran a test by using the incorrect credentials and it returned an response code of 85.  Clearly outside the range you have specified.
0
 
LVL 4

Author Comment

by:escheider
Comment Utility
Here is the line that returns 85

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

ErrInfo=85
0
 
LVL 4

Author Comment

by:escheider
Comment Utility
I have found the resource I need which included additional error codes not specified by dave.  Thanks for all the input.
0
 
LVL 6

Expert Comment

by:___XXX_X_XXX___
Comment Utility
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)
0
 
LVL 6

Expert Comment

by:___XXX_X_XXX___
Comment Utility
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)
0
 
LVL 6

Expert Comment

by:___XXX_X_XXX___
Comment Utility
Constant values:
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Public Const LANG_NEUTRAL = &H0
0
 
LVL 2

Expert Comment

by:DaveHavard
Comment Utility
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
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now