VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Connect/Disconnect shared network drives

by Waty Thierry (60 Submissions)
Category: Windows System Services
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Tue 13th April 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Connect/Disconnect shared network drives

Rate Connect/Disconnect shared network drives



' * Programmer Name  : Waty Thierry
' * Web Site         : www.geocities.com/ResearchTriangle/6311/
' * E-Mail           : [email protected]
' * Date             : 25/09/98
' * Time             : 11:18
' * Module Name      : class_NetUse
' * Module Filename  : NetUse.cls
' **********************************************************************
' * Comments         : Connect/Disconnect shared network drives
' *
' *
' **********************************************************************

Option Explicit

Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
Private Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" (ByVal lpszName As String, ByVal bForce As Long) As Long

Const WN_Success = &H0
Const WN_Not_Supported = &H1
Const WN_Net_Error = &H2
Const WN_Bad_Pointer = &H4
Const WN_Bad_NetName = &H32
Const WN_Bad_Password = &H6
Const WN_Bad_Localname = &H33
Const WN_Access_Denied = &H7
Const WN_Out_Of_Memory = &HB
Const WN_Already_Connected = &H34

'-- Error number and message
Public ErrorNum         As Long
Public ErrorMsg         As String

Public rc               As Long

Private Const ERROR_NO_CONNECTION = 8
Private Const ERROR_NO_DISCONNECT = 9


Private 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

'----------------------------------------------
'WNetAddConnection2

'Allows the caller to redirect (connect) a local
'device to a network resource. It is similar to
'WNetAddConnection, except that it takes a pointer
'to a NETRESOURCE structure to describe the network
'resource to connect to. It also takes the addition
'parameters lpUserID and dwFlags.

'lpNetResource
'Specifies the network resource to connect to.
'The following fields must be set when making a
'connection, the others are ignored.

'  lpRemoteName: Specifies the network resource
'                to connect to. This is limited
'                to MAX_PATH.
 
'  lpLocalName: This specifies the name of a local
'               device to be redirected, such as "F:"
'               or "LPT1". The string is treated in a
'               case insensitive manner, and may be
'               the empty string (or NULL) in which
'               case a connection to the network resource
'               is made without making a redirection.
 
'  lpProvider: Specifies the NP to connect to. If NULL
'              or empty string, Windows will try each
'              NP in turn. The caller should set
'              lpProvider only if it knows for sure
'              which network it wants. Otherwise, it
'              is preferable to let Windows determine
'              which NP the network name maps to.
'              If this is non NULL, Windows will try
'              the named NP and no other.
 
'  dwType: Specifies the type of resource to connect to.
'          It must be RESOURCETYPE_DISK or RESOURCETYPE_PRINT
'          if lpLocalName is not the empty string. It may
'          also be RESOURCETYPE_ANY if lpLocalName is the
'          empty string.
 
'lpPassword
'Specifies the password to be used in making the
'connection, normally the password associated with
'lpUserID. A NULL value or string may be passed in
'to indicate to the function to use the current
'default password.
'
'lpUserID
'This specifies the identity of the user needed to
'make the connection. If NULL, a default will be
'applied. This is used when the user wishes to connect
'to a resource, but has a different user name or
'account assigned to him for that resource. This
'identification represents a security context, and
'is NP specific.
'
'dwFlags
'This is a bit mask which may have any of the
'following bits set:
'
'  CONNECT_UPDATE_PROFILE: If the connection should
'                          be made persistent. If set,
'                          Windows automatically restores
'                          this connection when the user
'                          logs on to the network. A connection
'                          is only made persistent if the
'                          connection was successful.
Private 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
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long
Private Declare Function WNetConnectionDialog Lib "mpr.dll" (ByVal hWnd As Long, ByVal dwType As Long) As Long
Private Declare Function WNetDisconnectDialog Lib "mpr.dll" (ByVal hWnd As Long, ByVal dwType As Long) As Long

'Public Const RESOURCE_CONNECTED = &H1
'Public Const RESOURCE_REMEMBERED = &H3
'Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
'Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
'Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
'Public Const RESOURCEUSAGE_CONTAINER = &H2

Const NO_ERROR = 0
Const CONNECT_UPDATE_PROFILE = &H1
Const RESOURCETYPE_DISK = &H1
Const RESOURCETYPE_PRINT = &H2
Const RESOURCETYPE_ANY = &H0
Const RESOURCE_GLOBALNET = &H2
Const RESOURCEDISPLAYTYPE_SHARE = &H3
Const RESOURCEUSAGE_CONNECTABLE = &H1

Public Sub Connect(sDrive As String, sService As String, Optional sPassword As String = "")

   On Error GoTo Err_Connect
   Me.ErrorNum = 0
   Me.ErrorMsg = ""
   rc = WNetAddConnection(sService & Chr(0), sPassword & Chr(0), sDrive & Chr(0))
   If rc <> 0 Then GoTo Err_Connect

   Exit Sub

Err_Connect:
   Me.ErrorNum = rc
   Me.ErrorMsg = WnetError(rc)

End Sub

Public Sub DisConnect(sDrive As String)

   On Error GoTo Err_DisConnect
   Me.ErrorNum = 0
   Me.ErrorMsg = ""
   rc = WNetCancelConnection(sDrive + Chr(0), 0)
   If rc <> 0 Then GoTo Err_DisConnect

   Exit Sub
Err_DisConnect:
   Me.ErrorNum = rc
   Me.ErrorMsg = WnetError(rc)

End Sub

Private Function WnetError(Errcode As Long) As String

   Select Case Errcode
      Case WN_Not_Supported:
         WnetError = "Function is not supported."
      Case WN_Out_Of_Memory:
         WnetError = "Out of Memory."
      Case WN_Net_Error:
         WnetError = "An error occurred on the network."
      Case WN_Bad_Pointer:
         WnetError = "The Pointer was Invalid."
      Case WN_Bad_NetName:
         WnetError = "Invalid Network Resource Name."
      Case WN_Bad_Password:
         WnetError = "The Password was Invalid."
      Case WN_Bad_Localname:
         WnetError = "The local device name was invalid."
      Case WN_Access_Denied:
         WnetError = "A security violation occurred."
      Case WN_Already_Connected:
         WnetError = "The local device was connected to a remote resource."
      Case Else:
         WnetError = "Unrecognized Error " + Str(Errcode) + "."
   End Select

End Function

Public Function ConnectNetworkDialog() As Long
   ' *** Show the dialog to map a drive
   
   'If the function succeeds, the return value is
   'NO_ERROR (0). If the user cancels out of the
   'dialog box, it is &HFFFFFFFF.
   
   ConnectNetworkDialog = WNetConnectionDialog(0&, RESOURCETYPE_DISK)

End Function

Public Function DisconnectNetworkDialog() As Long
   ' *** Show the dialog to disconnect mapped a drive
   
   'If the function succeeds, the return value is
   'NO_ERROR (0). If the user cancels out of the
   'dialog box, it is &HFFFFFFFF.
   
   DisconnectNetworkDialog = WNetDisconnectDialog(0&, RESOURCETYPE_DISK)

End Function

Public Function ConnectPrintDialog() As Long
   ' *** Show the dialog to map a network printer, Windows
   
   'If the function succeeds, the return value is
   'NO_ERROR (0). If the user cancels out of the
   'dialog box, it is &HFFFFFFFF.
   
   ConnectPrintDialog = WNetConnectionDialog(0&, RESOURCETYPE_PRINT)

End Function

Public Function DisconnectPrintDialog() As Long
   ' *** Show the dialog to disconnect network printer
   
   'If the function succeeds, the return value is
   'NO_ERROR (0). If the user cancels out of the
   'dialog box, it is &HFFFFFFFF.
   
   DisconnectPrintDialog = WNetDisconnectDialog(0&, RESOURCETYPE_PRINT)

End Function

Public Function ConnectUserPassword(sDrive As String, sService As String, Optional sUser As String = "", Optional sPassword As String = "") As Boolean
   ' *** Connect to a network drive
   
   'attempts to connect to the passed network
   'connection to the specified drive.
   'ErrInfo=NO_ERROR if successful.
   
   Dim NETR       As NETRESOURCE
   Dim errInfo    As Long
   
   With NETR
      .dwScope = RESOURCE_GLOBALNET
      .dwType = RESOURCETYPE_DISK
      .dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
      .dwUsage = RESOURCEUSAGE_CONNECTABLE
      .lpRemoteName = sDrive
      .lpLocalName = sService
   End With
   
   errInfo = WNetAddConnection2(NETR, sPassword, sUser, CONNECT_UPDATE_PROFILE)
   
   ConnectUserPassword = errInfo = NO_ERROR
   
End Function



Download this snippet    Add to My Saved Code

Connect/Disconnect shared network drives Comments

No comments have been posted about Connect/Disconnect shared network drives. Why not be the first to post a comment about Connect/Disconnect shared network drives.

Post your comment

Subject:
Message:
0/1000 characters