none
Api WnetCancelConnection... RRS feed

  • Pergunta

  • Ola galera...


    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

            Dim res As New NETRESOURCE

            res.lpLocalName = sLocalName
            res.lpRemoteName = sRemoteName

            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
    _______________________________________________________________________________________________


    Imports System.Runtime.InteropServices
    Imports System.Text

    Public Class NETRESOURCE

    #Region "Enums"

        Public Enum RESOURCE As Integer
            CONNECTED = &H1
            GLOBALNET = &H2
            REMEMBERED = &H3
        End Enum


        Public Enum RESOURCETYPE As Integer
            ANY = &H0
            DISK = &H1
            PRINT = &H2
        End Enum


        Public Enum RESOURCEDISPLAYTYPE As Integer
            GENERIC = &H0
            DOMAIN = &H1
            SERVER = &H2
            SHARE = &H3
        End Enum


        Public Enum RESOURCEUSAGE As Integer
            CONNECTABLE = &H1
            CONTAINER = &H2
        End Enum


        Public Enum CONNECT As Integer
            UPDATE_PROFILE = &H1
        End Enum


        Public Enum WNetERROR As Integer
            SUCCESS = 0
            ACCESS_DENIED = 5&
            ALREADY_ASSIGNED = 85&
            BAD_DEVICE = 1200&
            BAD_NETPATH = 53&
            BAD_NETNAME = 67&
            BUSY = 170&
            CANCELLED = 1223&
            DEVICE_ALREADY_REMEMBERED = 1202&
            INVALID_PASSWORD = 86&
            NO_NET_OR_BAD_PATH = 1203&
            NO_NETWORK = 1222&
        End Enum

    #End Region

    #Region "P/Invoke"

        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

    #End Region

    #Region "Properties"

        Public ReadOnly Property GetData() As Byte()
            Get

                Dim yData(31) As Byte

                BitConverter.GetBytes(_dwScope).CopyTo(yData, 0)
                BitConverter.GetBytes(_dwType).CopyTo(yData, 4)
                BitConverter.GetBytes(_dwDisplayType).CopyTo(yData, 8)
                BitConverter.GetBytes(_dwUsage).CopyTo(yData, 12)

                BitConverter.GetBytes(_lpLocalName.ToInt32).CopyTo(yData, 16)
                BitConverter.GetBytes(_lpRemoteName.ToInt32).CopyTo(yData, 20)
                BitConverter.GetBytes(_lpComment.ToInt32).CopyTo(yData, 24)
                BitConverter.GetBytes(_lpProvider.ToInt32).CopyTo(yData, 28)

                Return yData

            End Get
        End Property


        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

                _lpLocalName = LocalAlloc(&H40, Value.Length * 2 + 2)

                Marshal.Copy(Encoding.Unicode.GetBytes(Value + Chr(0)), 0, _lpLocalName, Value.Length * 2 + 2)

                _LocalName = Value

            End Set
        End Property


        Public Property lpRemoteName() As String
            Get
                Return _RemoteName
            End Get
            Set(ByVal Value As String)

                If Not _lpRemoteName.Equals(IntPtr.Zero) Then
                    LocalFree(_lpRemoteName)
                End If

                _lpRemoteName = LocalAlloc(&H40, Value.Length * 2 + 2)

                Marshal.Copy(Encoding.Unicode.GetBytes(Value + Chr(0)), 0, _lpRemoteName, Value.Length * 2 + 2)

                _RemoteName = Value

            End Set
        End Property


        Public Property lpComment() As String
            Get
                Return _Comment
            End Get
            Set(ByVal Value As String)

                If Not _lpComment.Equals(IntPtr.Zero) Then
                    LocalFree(_lpComment)
                End If

                _lpComment = LocalAlloc(&H40, Value.Length * 2 + 2)

                Marshal.Copy(Encoding.Unicode.GetBytes(Value + Chr(0)), 0, _lpComment, Value.Length * 2 + 2)

                _Comment = Value

            End Set
        End Property


        Public Property lpProvider() As String
            Get
                Return _Provider
            End Get
            Set(ByVal Value As String)

                If Not _lpProvider.Equals(IntPtr.Zero) Then
                    LocalFree(_lpProvider)
                End If

                _lpProvider = LocalAlloc(&H40, Value.Length * 2 + 2)

                Marshal.Copy(Encoding.Unicode.GetBytes(Value + Chr(0)), 0, _lpProvider, Value.Length * 2 + 2)

                _Provider = Value

            End Set
        End Property

    #End Region

    #Region "Destroy"

        Protected Overrides Sub Finalize()

            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


    terça-feira, 8 de janeiro de 2008 19:34

Respostas

  • Ola...

    Sr(s),  so informando que já consegui o que eu queria... eu estava declarando a função de maneira errada segue abaixo.

    Grato

    Elton


    _________________________________________________________________________________________________


    Private Declare Function WNetCancelConnection2 Lib "CoreDLL.dll" Alias "WNetCancelConnection2W" ( _
                                   ByVal lpName As String, _
                                   ByVal dwFlags As Long, _
                                   ByVal fForce As Boolean) As Integer


    _________________________________________________________________________________________________
    segunda-feira, 14 de janeiro de 2008 13:16