Estou tento problemas em cancelar a conexao...achei um codigo que mostra como criar a conneccao... porem nao consigo finaliza-la... segue codigo abaixo ..
Obrigado
Elton
Classe CMapearRede __________________________________________________________________________________________ imports System. Public Class CMapearRede
'Mapear unidade de rede Visual Basic Private Declare Function WNetAddConnection3 Lib "CoreDLL.dll" Alias "WNetAddConnection3W" ( _ ByVal hwndOwner As IntPtr, _ ByVal lpNetResource() As Byte, _ ByVal lpPassword As String, _ ByVal lpUserName As String, _ ByVal dwFlags As NETRESOURCE.CONNECT _ ) As NETRESOURCE.WNetERROR
Private Declare Function GetLastError Lib "CoreDLL.dll" () As NETRESOURCE.WNetERROR
Public Function MapDrive(ByVal sRemoteName As String, ByVal sLocalName As String, ByVal sUsername As String, ByVal sPassword As String) As NETRESOURCE.WNetERROR
If WNetAddConnection3(IntPtr.Zero, res.GetData, sPassword, sUsername, 0) = NETRESOURCE.WNetERROR.SUCCESS Then Return NETRESOURCE.WNetERROR.SUCCESS Else Return GetLastError() End If
End Function
End Class
Classe Netresource _______________________________________________________________________________________________
Private Declare Function LocalAlloc Lib "coredll" ( _ ByVal flags As Int32, _ ByVal cb As Int32 _ ) As IntPtr
Private Declare Function LocalFree Lib "coredll" ( _ ByVal p As IntPtr _ ) As IntPtr Private Declare Function LocalAlloc Lib "coredll" ( _ ByVal lpName As String, _ ByVal fForce As Boolean _ ) As IntPtr ) #End Region
#Region "Provate variables"
Private _dwScope As RESOURCE Private _dwType As RESOURCETYPE Private _dwDisplayType As RESOURCEDISPLAYTYPE Private _dwUsage As RESOURCEUSAGE Private _LocalName As String = String.Empty Private _RemoteName As String = String.Empty Private _Comment As String = String.Empty Private _Provider As String = String.Empty Private _lpLocalName As IntPtr = IntPtr.Zero Private _lpRemoteName As IntPtr = IntPtr.Zero Private _lpComment As IntPtr = IntPtr.Zero Private _lpProvider As IntPtr = IntPtr.Zero
Public Property dwScope() As RESOURCE Get Return _dwScope End Get Set(ByVal Value As RESOURCE) _dwScope = Value End Set End Property
Public Property dwType() As RESOURCETYPE Get Return _dwType End Get Set(ByVal Value As RESOURCETYPE) _dwType = Value End Set End Property
Public Property dwDisplayType() As RESOURCEDISPLAYTYPE Get Return _dwDisplayType End Get Set(ByVal Value As RESOURCEDISPLAYTYPE) _dwDisplayType = Value End Set End Property
Public Property dwUsage() As RESOURCEUSAGE Get Return _dwUsage End Get Set(ByVal Value As RESOURCEUSAGE) _dwUsage = Value End Set End Property
Public Property lpLocalName() As String Get Return _LocalName End Get Set(ByVal Value As String)
If Not _lpLocalName.Equals(IntPtr.Zero) Then LocalFree(_lpLocalName) End If
If Not _lpLocalName.Equals(IntPtr.Zero) Then LocalFree(_lpLocalName) End If
If Not _lpRemoteName.Equals(IntPtr.Zero) Then LocalFree(_lpRemoteName) End If
If Not _lpComment.Equals(IntPtr.Zero) Then LocalFree(_lpComment) End If
If Not _lpProvider.Equals(IntPtr.Zero) Then LocalFree(_lpProvider) End If
MyBase.Finalize()
End Sub
#End Region End Class __________________________________________________________________________________________________
Form
Public Class frmMapearUnidadeDeRede Dim Mp As New LMapearRede.CMapearRede Dim Caminho As String, Unidade As String, Senha As String, Usuario As String Dim teste
Private Sub btnMapear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMapear.Click Caminho = "\\cminfo\generico" Unidade = "Inventario" Senha = "judy12" Usuario = "elton"
If Caminho = "" Then MsgBox("Digite um caminho válido!") ElseIf Unidade = "" Then MsgBox("Digite uma unidade válida!") ElseIf Senha = "" Then MsgBox("Digite uma senha válida!") ElseIf Usuario = "" Then MsgBox("Digite um usuário válido!") End If
Try teste = Mp.MapDrive(Caminho, Unidade, Usuario, Senha)
'LMapearRede.CMapearRede.WNetAddConnection(Caminho, Senha, Unidade) Select Case teste Case 0 MsgBox("Conexão Realizada... Preparando para enviar os dados") Case 53 'The network path was not found. MsgBox("O caminho da rede não foi encontrado") Case 87 MsgBox("Parametro incorreto") Case 85 ' not connected due to existing connection on specified drive letter MsgBox("Não conectou devido a uma conecção existente na unidade especificado!") Case 2 'WN_NET_ERROR = 2 ' An error occurred on the network. MsgBox("Um erro ocorreu na rede") Case 6 'WN_BAD_PASSWORD = 6 ' The password was invalid. MsgBox("Senha Inválida") Case Else MsgBox(teste) End Select Catch ex As Exception MsgBox("Não foi possivel mapear a unidade de rede" & teste) End Try
End Sub
Private Sub btnEnviar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnviar.Click IO.File.Copy("\my documents\invs1011.txt", "\network\inventario\invs1011.txt") End Sub End Class
Private Declare Function WNetCancelConnection2 Lib "CoreDLL.dll" Alias "WNetCancelConnection2W" ( _ ByVal lpName As String, _ ByVal dwFlags As Long, _ ByVal fForce As Boolean) As Integer