VB Notebook Main Page VB Notebook Files VB Notebook Function Library VB Notebook Articles VB Notebook Links VB Notebook

VBNotebook Home | Functions | Add and Remove Network Connections

Here are three routines to help you add and remove network connections and to show the Windows Map Drives dialog so that users can set up their own path.

First, in General|Declarations, include the following...
__________________________________________________________________________________________

'
' WNet Constants
'
Public Enum WNetResultConstants
    WN_SUCCESS = 0
    WN_NET_ERROR = 2
    WN_BAD_PASSWORD = 6
    WN_ERROR_BAD_DEVICE = 1200&
    WN_ERROR_CONNECTION_UNAVAIL = 1201&
    WN_ERROR_EXTENDED_ERROR = 1208&
    WN_ERROR_MORE_DATA = 234
    WN_ERROR_NOT_SUPPORTED = 50&
    WN_ERROR_NO_NET_OR_BAD_PATH = 1203&
    WN_ERROR_NO_NETWORK = 1222&
    WN_ERROR_NOT_CONNECTED = 2250&
End Enum
'
' API Calls
'

Private Declare Function GetDesktopWindow Lib "user32" () As Long

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

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

__________________________________________________________________________________________

...and now for the routines.... __________________________________________________________________________________________

Public Function AddNetworkConnection(sShareName As String, sPassword As String, _
                                     sDriveLetter
As String) As WNetResultConstants
    AddNetworkConnection
= WNetAddConnection(sShareName, sPassword, sDriveLetter)
End Function

Public Function RemoveNetworkConnection(sDriveLetter As String, _
                                        bForce
As Boolean) As WNetResultConstants
    RemoveNetworkConnection = WNetCancelConnection(sDriveLetter, CLng(bForce))
End Function

Public Sub ShowMapDrivesDialog(Optional hWndOwner As Long = 0)
    If hWndOwner = 0 Then
        hWndOwner = GetDesktopWindow()
    End If
    WNetConnectionDialog hWndOwner, 1
End Sub

__________________________________________________________________________________________

Copyright 2000-2005, J. Frank Carr