escheider
asked on
Mapping Drive Letter - Error Codes
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.
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
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
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.
what version of Windows you using?
ASKER
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.
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.
The Above Code should do it did you try it?
ASKER
It doesn't appear that it will validate username and password and return an error code otherwise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I ran a test by using the incorrect credentials and it returned an response code of 85. Clearly outside the range you have specified.
ASKER
Here is the line that returns 85
ErrInfo = WNetUseConnection(FormHwnd , NetR, UserName, _
Password, CONNECT_REDIRECT, buffer, bufferlen, success)
ErrInfo=85
ErrInfo = WNetUseConnection(FormHwnd
Password, CONNECT_REDIRECT, buffer, bufferlen, success)
ErrInfo=85
ASKER
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(lngE rrorNumber 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)
Public Function Fun_GetAPIErrorString(lngE
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
'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
Public Const FORMAT_MESSAGE_FROM_SYSTEM
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
Thanks
Dave
On Error Resume Next
Const User = "UserName"
Const Password = "Password"
Dim NW, FSO
Set FSO = CreateObject("Scripting.Fi
If not FSO.DriveExists("S:") Then
Set NW = CreateObject("WScript.Netw
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