AllAPI Network - The KPD-Team

Allapi Network

 API List

API Resources
 Tips & Tricks
 VB Tutorials
 Error Lookup
Misc Stuff
 VB examples
 VB Tools
 VB Links
 Top Downloads
This Site
 Search Engine
 Contact Form

Donate to

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.

Start a new Standard-EXE project, form1 is created by default
Add a standard module and add a command button to form1
Type the following in the standard module.

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)

Exit Function

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, _

Exit Function

CancelConnection = Err
MsgBox Error$

Resume CancelConnection_End

End Function

to add a connection call by:
varible = AddConnection(<SharePath>, <Password>, <DriveLetter>)

To cancel a connection type:
varible = CancelConnection(<SharePath, <Force>)

Run the project.



Copyright © 1998-2007, The Team - Privacy statement
Did you find a bug on this page? Tell us!
This site is located at