Visual Basic Network Services

WNetConnectionDialog: Invoking the Map/Disconnect Drive Dialog
     
Posted:   Thursday December 15, 2005
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows NT4
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

WNetAddConnection2: Transparently Connect to Network Shares
NetShareCheck: Determine Remote Folder or Device Share Status
WNetEnumResource: Enumerating Local Network Resources
NetConnectionEnum: Enumerating Share Connection Information
NetShareEnum: Enumerating Shared Resources on Other Machines  
NetShareAdd: Create a Local or Remote Share
WNetGetUser: User, Share and Share User for Network Resources
WNetGetConnection: Get UNC Path for Mapped Drive
     
 Prerequisites
Network connection.

Two small network routines are shown enabling you to add "Map Network Drive" and "Disconnect Network Drive" functionality to your application.  Use the Open Explorer button to view changes to the disk mappings.
 BAS Module Code
None.

 Form Code
To a form add one command buttons (Command1) and set the Index property to 0 to create a control array. The code will take care of creating the remaining buttons. Add the following code to the form:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const RESOURCETYPE_DISK As Long = &H1
Private Const SW_SHOWNORMAL As Long = 1

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

Private Declare Function ShellExecute Lib "shell32.dll" _
    Alias "ShellExecuteA" _
   (ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long
    


Private Sub Form_Load()

   Dim cnt As Long
   
  'build the form
   For cnt = 0 To 3
   
      If cnt > 0 Then Load Command1(cnt)
      
      With Command1(cnt)
      
         .Move 200, 200 + (370 * cnt), 2000, 345
         .Visible = True
         
         Select Case cnt
            Case 0: .Caption = "Map Network Drive"
            Case 1: .Caption = "Disconnect Drive"
            Case 2: .Caption = "Open Explorer"
            Case 3: .Caption = "Exit"
         End Select
         
      End With
      
   Next  'cnt
         
End Sub


Private Sub Command1_Click(Index As Integer)

   Select Case Index
      Case 0: 'Map drive
              'If the function succeeds, the return value is
              'NO_ERROR (0). If the user cancels out of the
              'dialog box, it is &HFFFFFFFF (-1).
               Call WNetConnectionDialog(Me.hwnd, RESOURCETYPE_DISK)
      
      Case 1: 'Disconnect drive
              'If the function succeeds, the return value is
              'NO_ERROR (0). If the user cancels out of the
              'dialog box, it is &HFFFFFFFF (-1).
               Call WNetDisconnectDialog(Me.hwnd, RESOURCETYPE_DISK)
                     
      Case 2: 'show explorer
               Call ShellExecute(0&, "Open", _
                                     "explorer.exe", "/e,/n,c:\", _
                                     0&, SW_SHOWNORMAL)
      Case 3: 'quit
               Unload Me
      
   End Select
   
End Sub
 Comments
Save the project, making any changes necessary to the server strings used if you have a network connection on the current system. All routines will execute regardless of the connection available; all will return false or errors when the connection is attempted. Testing on a non-networked machine is perfectly safe.

 

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter