NET USE Return Code?

I've written a VB app that occasionally maps drives. Anybody know of a way to capture the failure of executing the NET USE command so that I may present an error message to the end user? It's possible that mapping the drive will fail, as when the drive is mapped the user is prompted for their domain password.

Slack Space 1613 This topic was started by ,


data/avatar/default/avatar28.webp

295 Posts
Location -
Joined 2001-07-04
I've written a VB app that occasionally maps drives. Anybody know of a way to capture the failure of executing the NET USE command so that I may present an error message to the end user? It's possible that mapping the drive will fail, as when the drive is mapped the user is prompted for their domain password.
 
I understand one may also map drives in VB by using an API call, but I'd rather stick with NET USE. Any suggestions are appreciated.
 
This app is written in VB 6.0 SP 4.

Participate on our website and join the conversation

You have already an account on our website? Use the link below to login.
Login
Create a new user account. Registration is free and takes only a few seconds.
Register
This topic is archived. New comments cannot be posted and votes cannot be cast.

Responses to this topic


data/avatar/default/avatar35.webp

2172 Posts
Location -
Joined 2002-08-26
You mean like an errorlevel 1, etc. codes?

data/avatar/default/avatar19.webp

347 Posts
Location United States
Joined 2002-03-21
try this:
 
net use x: \\server\share | find "successfully"
if %errorlevel%==1 then whatever

data/avatar/default/avatar28.webp

295 Posts
Location -
Joined 2001-07-04
OP
thanks y'all. this sounds pretty darn good. i'll give it a try and let you know if it worked.

data/avatar/default/avatar28.webp

295 Posts
Location -
Joined 2001-07-04
OP
I guess the problem now is... how to I send that errorlevel back to my VB app. I'd like to take care of the IF/THEN logic back in my code.

data/avatar/default/avatar28.webp

295 Posts
Location -
Joined 2001-07-04
OP
I went ahead and used an API call to do it. Pretty straightforward:
 
-------------------------------
 
Created these functions:
 
---------------------------------
 
Declare Function WNetAddConnection2 Lib "mpr.dll" Alias _
"WNetAddConnection2A" (lpNetResource As NETRESOURCE, _
ByVal lpPassword As String, ByVal lpUserName As String, _
ByVal dwFlags As Long) As Long
 
Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias _
"WNetCancelConnection2A" (ByVal lpName As String, _
ByVal dwFlags As Long, ByVal fForce As Long) As Long
 
Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End Type
 
Public Const NO_ERROR = 0
Public Const CONNECT_UPDATE_PROFILE = &H1
' The following includes all the constants defined for NETRESOURCE,
' not just the ones used in this example.
Public Const RESOURCETYPE_DISK = &H1
Public Const RESOURCETYPE_PRINT = &H2
Public Const RESOURCETYPE_ANY = &H0
Public Const RESOURCE_CONNECTED = &H1
Public Const RESOURCE_REMEMBERED = &H3
Public Const RESOURCE_GLOBALNET = &H2
Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
Public Const RESOURCEUSAGE_CONNECTABLE = &H1
Public Const RESOURCEUSAGE_CONTAINER = &H2
' Error Constants:
Public Const ERROR_ACCESS_DENIED = 5&
Public Const ERROR_ALREADY_ASSIGNED = 85&
Public Const ERROR_BAD_DEV_TYPE = 66&
Public Const ERROR_BAD_DEVICE = 1200&
Public Const ERROR_BAD_NET_NAME = 67&
Public Const ERROR_BAD_PROFILE = 1206&
Public Const ERROR_BAD_PROVIDER = 1204&
Public Const ERROR_BUSY = 170&
Public Const ERROR_CANCELLED = 1223&
Public Const ERROR_CANNOT_OPEN_PROFILE = 1205&
Public Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
Public Const ERROR_EXTENDED_ERROR = 1208&
Public Const ERROR_INVALID_PASSWORD = 86&
Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&
 
--------------------------
 
Then I used them here:
 
--------------------------
 
Public Sub mapdrives(system As String, MyPass As String)
 
'On Error GoTo errorhandler
 
MyUserNoDomain = Environ("USERNAME")
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
 
If system = "dev" Then
Call dev
End If
 
If system = "simprod" Then
Call simprod
End If
 
MyUser = Domain & "\" & MyUserNoDomain
 
ErrInfo = WNetAddConnection2(NetR, Trim(MyPass), Trim(MyUser), _
CONNECT_UPDATE_PROFILE)
 
End Sub
Private Sub dev()
Domain = "xxxx"
NetR.lpLocalName = "r:"
NetR.lpRemoteName = "\\xxxx\xxxx"
End Sub
Private Sub simprod()
Domain = "xxxx"
NetR.lpLocalName = "t:"
NetR.lpRemoteName = "\\xxxx\xxxx"
End Sub
 
 
This maps the drives with the API and returns success or failure codes without the need for another file or any command executions. It's a little more freaky to look at, but it works well.
 
Thanks for all your help.