Jump to content
Compatible Support Forums
Sign in to follow this  
Atreyu

NET USE Return Code?

Recommended Posts

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.

Share this post


Link to post

try this:

 

net use x: \\server\share | find "successfully"

if %errorlevel%==1 then whatever

Share this post


Link to post

thanks y'all. this sounds pretty darn good. i'll give it a try and let you know if it worked.

Share this post


Link to post

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.

Share this post


Link to post

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.

Share this post


Link to post

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×