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...




Topic Options
#111911 - 10/03/02 04:32 PM NET USE Return Code?
Atreyu Offline
enthusiast

Registered: 07/04/01
Posts: 297
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.

Top
Advertisement
#111991 - 10/04/02 11:47 PM Re: NET USE Return Code?
adamvjackson Offline
Pooh-Bah

Registered: 08/26/02
Posts: 2174
Loc: Asheville, NC
You mean like an errorlevel 1, etc. codes?

Top
#112017 - 10/05/02 09:32 AM Re: NET USE Return Code?
Davros Offline
enthusiast

Registered: 03/21/02
Posts: 347
Loc: Houston, Texas
try this:

net use x: \\server\share | find "successfully"
if %errorlevel%==1 then whatever

Top
#112116 - 10/06/02 06:56 PM Re: NET USE Return Code?
Atreyu Offline
enthusiast

Registered: 07/04/01
Posts: 297
thanks y'all. this sounds pretty darn good. i'll give it a try and let you know if it worked.

Top
#112117 - 10/06/02 06:59 PM Re: NET USE Return Code?
Atreyu Offline
enthusiast

Registered: 07/04/01
Posts: 297
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.

Top
#112733 - 10/14/02 11:18 PM Re: NET USE Return Code?
Atreyu Offline
enthusiast

Registered: 07/04/01
Posts: 297
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.

Top


Forums
Windows Support Forums
Everything New Technology
Legacy OS
Hardware
Software
Games
Networking
Customization & Tweaking
Security

Linux Support Forums
Everything Linux
Linux Hardware
Linux Software
Linux Games
Linux Networking
Linux Customization & Tweaking
Linux Security

Apple Support Forums
Everything Apple
Recent Topics
x86 OS, RAM, & Virtual Machines
by Myke
12/22/09 08:16 PM
Ram Question
by JohnnyAshes
12/21/09 09:50 PM
NEWBIE needs help with REALTEK
by SerryJW
12/21/09 06:09 AM
What version of Linux is this?
by DxxLinux
12/15/09 07:59 PM
Anything like HyperCam?
by Luckycharm8989
12/11/09 02:08 PM
Who's Online
0 Registered (), 142 Guests and 41 Spiders online.
Key: Admin, Global Mod, Mod
Forum Stats
91388 Members
24 Forums
59236 Topics
189761 Posts

Max Online: 1079 @ 03/12/08 01:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22