API Tip

Map Network Drives Easily

           

 As far as I know, there isn't a single control that comes with VB that would enable you to call up drive mapping dialog. However you should have no problem doing it yourself. Just use two API functions - WNetConnectionDialog and WNetDisconnectDialog. The first one let's you map a network drive (makes a network hard drive look and feel as one of your local hard drives) and the second one disconnects network resources (hard drives). 


Picture 1: Calling WNetConnectionDialog results in this dialog box

Let's have a close look at the functions:

Public Declare Function WNetConnectionDialog Lib "mpr.dll" Alias "WNetConnectionDialog" (ByVal hwnd As Long, ByVal dwType As Long) As Long

Public Declare Function WNetDisconnectDialog Lib "mpr.dll" Alias "WNetDisconnectDialog" (ByVal hwnd As Long, ByVal dwType As Long) As Long

Of course each previous function has to be declared in one line in a module as public or in a form code section as private. hwnd parameter is a handle to the window which is calling this function. You get the handle by calling  formname.hWnd. You don't necessarily have to provide this value. You can pass 0 instead. dwType must be RESOURCETYPE_DISK. Declare the constanst like this:

Public Const RESOURCETYPE_DISK = &H1 ' connect to network hard drives

That's all API declarations you need. 

In the following example I added two command buttons, cmdConnect and cmdDisconnect for adding or removing network resources when the buttons are clicked:

Private Sub cmdConnect_Click()

     Dim lTemp As Long

     'show me "Map Network Drive" dialog.
     lTemp = WNetConnectionDialog(0, RESOURCETYPE_DISK)

End Sub

Private Sub cmdDisconnect_Click()

     Dim lTemp As Long

     'lets disconnect some network drives:
     lTemp = WNetDisconnectDialog(frmNetExample.hwnd, RESOURCETYPE_DISK)

End Sub

By clicking cmdConnect, a user of your app can map network hard drive and by clicking cmdDisconnect, a user can disconnect network hard drive. Simple and powerful!

Good luck and be sure to save your work before you mess around with Windows (r) API.

Api Tips - Home

          

Written By Laimonas Simutis. 2001. laijerrad@yahoo.com

Written By Laimonas Simutis. 2002. laijerrad@yahoo.com