USUÁRIO:      SENHA:        SALVAR LOGIN ?    Adicione o VBWEB na sua lista de favoritos   Fale conosco 

 

  Fórum

  Visual Basic
Voltar
Autor Assunto:  Mapear unidade de rede com login+senha
Fabio-RSC
NITEROI
RJ - BRASIL
Postada em 05/07/2006 09:55 hs         
Gente,
Procurei no fórum e dicas como mapear uma unidade de rede e achei a API Wnetconnection que pode mapear utilizando uma senha, porém eu preciso utilizar um login além da senha.
Achei a API Wnetconnection2, usando o aplicativo ALLAPI, mas não consigo usar esta API.
Esta API usa uma estrutura NETRESOURCE que eu não consegui entender como funciona.
 
Se alguém souber como se usa esta API, por favor me ajude.
     
Sandro
não registrado
Postada em 05/07/2006 22:55 hs   
Olá,
Encontrei o seguinte passo-a-passo no MSDN, mas não testei:

Create a new Standard EXE project.
Add a module to the project.
Copy and paste the following Declares and Type into the module: (NOTE: If you add this to a Form module, make all entries 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
 
      Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias _
      "WNetCancelConnection2A" (ByVal lpName As String, _
      ByVal dwFlags As Long, ByVal fForce As Long) As Long
 
      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
      Public Const NO_ERROR = 0
      Public Const CONNECT_UPDATE_PROFILE = &H1
      ' The following includes all the constants defined for NETRESOURCE,
      ' not just the ones used in this example.
      Public Const RESOURCETYPE_DISK = &H1
      Public Const RESOURCETYPE_PRINT = &H2
      Public Const RESOURCETYPE_ANY = &H0
      Public Const RESOURCE_CONNECTED = &H1
      Public Const RESOURCE_REMEMBERED = &H3
      Public Const RESOURCE_GLOBALNET = &H2
      Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
      Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
      Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
      Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
      Public Const RESOURCEUSAGE_CONNECTABLE = &H1
      Public Const RESOURCEUSAGE_CONTAINER = &H2
      ' Error Constants:
      Public Const ERROR_ACCESS_DENIED = 5&
      Public Const ERROR_ALREADY_ASSIGNED = 85&
      Public Const ERROR_BAD_DEV_TYPE = 66&
      Public Const ERROR_BAD_DEVICE = 1200&
      Public Const ERROR_BAD_NET_NAME = 67&
      Public Const ERROR_BAD_PROFILE = 1206&
      Public Const ERROR_BAD_PROVIDER = 1204&
      Public Const ERROR_BUSY = 170&
      Public Const ERROR_CANCELLED = 1223&
      Public Const ERROR_CANNOT_OPEN_PROFILE = 1205&
      Public Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
      Public Const ERROR_EXTENDED_ERROR = 1208&
      Public Const ERROR_INVALID_PASSWORD = 86&
      Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&
 
Add two CommandButtons to Form1. These will be Command1 and Command2 by default.
Add the following code to Form1, substituting a valid share name for "\ServerNameShareName":

      Option Explicit
 
      Private Sub Command1_Click()
      Dim NetR As NETRESOURCE
      Dim ErrInfo As Long
      Dim MyPass, MyUser As String
 
      NetR.dwScope = RESOURCE_GLOBALNET
      NetR.dwType = RESOURCETYPE_DISK
      NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
      NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
      NetR.lpLocalName = "X:" ' If undefined, Connect with no device
      NetR.lpRemoteName = "\ServerNameShareName"   ' Your valid share
      'NetR.lpComment = "Optional Comment"
      'NetR.lpProvider =    ' Leave this undefined
      ' If the UserName and Password arguments are NULL, the user context
      ' for the process provides the default user name.
      ErrInfo = WNetAddConnection2(NetR, MyPass, MyUser, _
      CONNECT_UPDATE_PROFILE)
      If ErrInfo = NO_ERROR Then
        MsgBox "Net Connection Successful!", vbInformation, _
        "Share Connected"
      Else
        MsgBox "ERROR: " & ErrInfo & " - Net Connection Failed!", _
        vbExclamation, "Share not Connected"
      End If
      End Sub
      Private Sub Command2_Click()
      Dim ErrInfo As Long
      Dim strLocalName As String
      ' You may specify either the lpRemoteName or lpLocalName
      'strLocalName = "\ServerNameShareName"
      strLocalName = "X:"
      ErrInfo = WNetCancelConnection2(strLocalName, _
      CONNECT_UPDATE_PROFILE, False)
      If ErrInfo = NO_ERROR Then
        MsgBox "Net Disconnection Successful!", vbInformation, _
        "Share Disconnected"
      Else
        MsgBox "ERROR: " & ErrInfo & " - Net Disconnection Failed!", _
        vbExclamation, "Share not Disconnected"
      End If
      End Sub

Run the project and click on Command1. You will get a Message dialog indicating success or failure. If successful, you should be able to look in Windows Explorer and see the new connection (unless you left lpLocalName undefined, in which case the connection does not show in Explorer). Click Command2 and go to Explorer, where you should see that the connection has been removed.
     
Fabio-RSC
NITEROI
RJ - BRASIL
Postada em 06/07/2006 11:41 hs         
Muito obrigado Sandro.
É exatamante o que eu queria. Eu não estava conseguindo antes porque o exemplo que eu peguei estava errado.
     
Página(s): 1/1    


Seu Nome:

Seu eMail:

ALTERAR PARA MODO HTML
Mensagem:

[:)] = 
[:P] = 
[:(] = 
[;)] = 

HTML DESLIGADO

     
 VOLTAR

  



CyberWEB Network Ltda.    © Copyright 2000-2024   -   Todos os direitos reservados.
Powered by HostingZone - A melhor hospedagem para seu site
Topo da página