Adding a network connection to your computer

This can be easily done by using map network drive in Network Neighbourhood or My Computer but it can be useful to be able to do this in Visual Basic.  I will now show you how to do this using Windows API.  Here is the example: Also includes how to delete a connection.

Option Explicit

Declare Function WNetAddConnection Lib "mpr.dll" Alias _
"WNetAddConnectionA" (ByVal lpszNetPath As String, _
ByVal lpszPassword As String, ByVal lpszLocalName _
As String) As Long

Declare Function WNetCancelConnection Lib "mpr.dll" _
Alias "WNetCancelConnectionA" (ByVal lpszName _
As String, ByVal bForce As Long) As Long

Const WN_SUCCESS = 0 'The function was successful.
Const WN_NET_ERROR = 2 'An error occurred on the network.
Const WN_BAD_PASSWORD = 6 'The password was invalid.

Function AddConnection(MyShareName As String, _
MyPWD As String, UseLetter As String) As Integer

On Local Error GoTo AddConnection1_Err

AddConnection = WNetAddConnection(MyShareName, _
MyPWD, UseLetter)
AddConnection_End:

Exit Function

AddConnection_Err:
AddConnection = Err
MsgBox Error$

Resume AddConnection_End

End Function

Function CancelConnection(DriveLetter As String, _
Force As Integer) As Integer

On Local Error GoTo CancelConnection_Err
CancelConnection = WNetCancelConnection(DriveLetter, _
Force)
CancelConnection_End:

Exit Function

CancelConnection_Err:
CancelConnection = Err
MsgBox Error$

Resume CancelConnection_End

End Function
varible = AddConnection(<SharePath>, _
<Password>, <DriveLetter>)
varible = CancelConnection(<SharePath, <Force>)

Run the project.


Vertical Blinds | Information Merchant Rates | Blinds | Florida Bank Foreclosures | Loans Lender