|
|
|
|
|
Dicas
|
|
Visual Basic (Redes/Comunicações)
|
|
|
Título da Dica: Mapeando e removendo uma unidade de rede via API
|
|
|
|
Postada em 7/7/2003 por cacá
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 Const WN_NET_ERROR = 2 Const WN_BAD_PASSWORD = 6
Function AddConnection(MyShareName As String, MyPWD As String, UseLetter As String) As Integer
On Error GoTo AddConnection_Err AddConnection = WNetAddConnection(MyShareName, MyPWD, UseLetter) If AddConnection = WN_SUCCESS Then MsgBox "Unidade " & UseLetter & " mapeada com êxito", vbInformation ElseIf AddConnection = WN_NET_ERROR Then MsgBox "Não foi possível mapear a unidade " & UseLetter, vbInformation ElseIf AddConnection = WN_BAD_PASSWORD Then MsgBox "Não foi possível mapear a unidade " & UseLetter & ". Senha incorreta!", vbInformation End If 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 Error GoTo CancelConnection_Err CancelConnection = WNetCancelConnection(DriveLetter, Force) If CancelConnection = WN_SUCCESS Then MsgBox "Unidade " & DriveLetter & " desmapeada com êxito", vbInformation Else MsgBox "Não foi possível desmapear a unidade " & DriveLetter, vbInformation End If CancelConnection_End: Exit Function CancelConnection_Err: CancelConnection = Err MsgBox Error$ Resume CancelConnection_End End Function
'Utilizando os procedimentos na prática:
'Para mapear uma unidade: AddConnection(<SharePath>, <Password>, <DriveLetter>) AddConnection "\\Pentium233\C", "", "H:"
'Para remover a unidade: CancelConnection(<SharePath, <Force>) CancelConnection "H:", 0
|
|
|
|
|