Bueno, podés usar las siguientes API:
Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
Te paso un ejemplo de las dos funciones
WNetAddConnection
Código:
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
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 AddConnection_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
Private Sub Form_Load()
'to add a connection call by:
variable = AddConnection(<SharePath>, <Password>, <DriveLetter>)
'To cancel a connection type:
varible = CancelConnection(<SharePath, <Force>)
End Sub
WNetAddConnection2
Código:
'Create a form with:
' • Two commands (Command1, Command2)
' • One Drive (Drive1)
' • One label (Label1)
Const RESOURCETYPE_DISK = &H1
Private Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End Type
Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long
Dim theNetResource As NETRESOURCE
Dim UserName As String
Dim UserPassword As String
Private Sub Form_Load()
'example created by Tim Derdelinckx ([email protected])
'visit his site at http://www.allgeier.be
theNetResource.lpRemoteName = "\\SERVER\ShareName"
theNetResource.lpLocalName = "Z:"
UserName = "Username"
UserPassword = "Password"
theNetResource.dwType = RESOURCETYPE_DISK
Label1.Caption = ""
Command1.Caption = "Add Connection"
Command2.Caption = "Cancel Connection"
End Sub
Private Sub Command1_Click()
Dim Result&
Result = WNetAddConnection2(theNetResource, UserPassword, UserName, 0)
If Result = 0 Then
Label1.Caption = "Connection Established."
Drive1.Drive = theNetResource.lpLocalName
Drive1.Refresh
Else
Label1.Caption = "Connection not possible."
MsgBox "Connection not possible." & vbCrLf & "(Maybe the drive is allready used or the share doesn't exist)"
End If
End Sub
Private Sub Command2_Click()
Dim Result&
Result = WNetCancelConnection2(theNetResource.lpLocalName, 0, 0)
If Result = 0 Then
Label1.Caption = "Connection Closed."
Else
Label1.Caption = "Close connection not possible."
End If
Drive1.Refresh
End Sub
Saludos.