Solved

Mapping Drive Letter - Error Codes

Posted on 2004-04-02
13
2,782 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
ID: 10741141
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
ID: 10741145
what version of Windows you using?
0
 
LVL 4

Author Comment

by:escheider
ID: 10741232
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 48

Expert Comment

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

Author Comment

by:escheider
ID: 10741371
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
ID: 10741832
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
 
LVL 4

Author Comment

by:escheider
ID: 10742871
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
ID: 10742935
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
ID: 10743230
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___
ID: 10743393
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___
ID: 10743417
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___
ID: 10743424
Constant values:
Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Public Const LANG_NEUTRAL = &H0
0
 
LVL 2

Expert Comment

by:DaveHavard
ID: 10755478
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

829 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