5

我有一个程序需要将数据放在网络共享上。在某些情况下,用户无法通过其帐户访问,并且需要提供凭据才能登录。

我的想法是让程序弹出 Windows 凭据屏幕,以便用户可以登录(比如当用户通过 Windows 资源管理器打开共享时)。

我找到了WNetUseConnection允许在不映射共享的情况下登录共享的 API,并且它具有提供提示的选项。

这是我正在使用的代码:

Public Class frmMain

    Private Const CONNECT_INTRERACTIVE = &H8
    Private Const CONNECT_PROMPT = &H10
    Private Const RESOURCETYPE_DISK = &H1

    Private Structure NETRESOURCE
        Public dwScope As Long
        Public dwType As Long
        Public dwDisplayType As Long
        Public dwUsage As Long
        Public lpLocalName As String
        Public lpRemoteName As String
        Public lpComment As String
        Public lpProvider As String
    End Structure

    Private Declare Function WNetUseConnection Lib "mpr.dll" _
        Alias "WNetUseConnectionA" ( _
        ByVal hwndOwner As Long, _
        ByRef lpNetResource As NETRESOURCE, _
        ByVal lpUsername As String, _
        ByVal lpPassword As String, _
        ByVal dwFlags As Long, _
        ByVal lpAccessName As String, _
        ByRef lpBufferSize As Long, _
        ByRef lpResult As Long) _
   As Long

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim resource As New NETRESOURCE
        Dim success As Long
        Dim ErrInfo As Long

        With resource
            .dwType = RESOURCETYPE_DISK
            .lpLocalName = vbNullString
            .lpRemoteName = "\\server\folder\"
            .lpProvider = vbNullString
        End With

        ErrInfo = WNetUseConnection(Me.Handle, resource, "", "", CONNECT_INTRERACTIVE Or CONNECT_PROMPT, vbNull, vbNull, success)

        Console.WriteLine(ErrInfo)

        Dim errorMessage As String
        errorMessage = New Win32Exception().Message

        Console.WriteLine(errorMessage)
End Sub

我回来的错误是:

句柄无效(代码 2091649073639)。

有没有办法让这个工作?

4

1 回答 1

7

您发布的代码似乎有几个问题:

  1. 您的 P/Invoke 类型需要调整。以下是本机 Windows 类型及其相应的 VB .Net 对应物:

    • HWND->IntPtr
    • LPDWORD->IntPtr
    • DWORD-> UInt32(我也见过Integer用过)

    UsingIntPtr允许运行时使用 32 位或 64 位指针,具体取决于机器架构。DWORDs 始终是 32 位的,即使在 64 位机器上也是如此。另请参阅:Visual C++:32 位和 64 位代码的 DWORD 有多大?

  2. P/Invoke 签名的用户名和密码参数颠倒了。密码参数实际上应该是第一个。在您的情况下这并不重要,因为系统会提示用户输入此信息。但是,当您稍后尝试传递这些值时可能很重要......

  3. 远程共享不应有尾部反斜杠

    .lpRemoteName = "\\server\folder\"

    应该

    .lpRemoteName = "\\server\folder"

  4. 最后,我必须添加buffer参数才能使代码正常工作。没有它,WNetUseConnection函数总是返回ERROR_MORE_DATA,这意味着缓冲区太小。

这是我使用的完整代码(在 Windows 8 64 位、Windows 7 32 位上测试):

Imports System.ComponentModel
Imports System.Text

Public Class frmMain
    Private Const CONNECT_INTRERACTIVE = &H8
    Private Const CONNECT_PROMPT = &H10
    Private Const RESOURCETYPE_DISK = &H1

    Private Structure NETRESOURCE
        Public dwScope As UInt32
        Public dwType As UInt32
        Public dwDisplayType As UInt32
        Public dwUsage As UInt32
        Public lpLocalName As String
        Public lpRemoteName As String
        Public lpComment As String
        Public lpProvider As String
    End Structure

    Private Declare Function WNetUseConnection Lib "mpr.dll" _
        Alias "WNetUseConnectionA" ( _
        ByVal hwndOwner As IntPtr, _
        ByRef lpNetResource As NETRESOURCE, _
        ByVal lpPassword As String, _
        ByVal lpUsername As String, _
        ByVal dwFlags As UInt32, _
        ByVal lpAccessName As StringBuilder, _
        ByRef lpBufferSize As IntPtr, _
        ByRef lpResult As IntPtr) _
   As UInt32

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim resource As New NETRESOURCE
        Dim success As Long
        Dim ErrInfo As Long
        Const BUFFERSIZE As Integer = 1024
        Dim buffer As New StringBuilder(BUFFERSIZE)

        With resource
            .dwType = RESOURCETYPE_DISK
            .lpLocalName = vbNullString
            .lpRemoteName = "\\server\folder"
            .lpProvider = vbNullString
        End With

        ErrInfo = WNetUseConnection(Me.Handle, resource, "", "", CONNECT_INTRERACTIVE Or CONNECT_PROMPT, buffer, BUFFERSIZE, success)

        If ErrInfo > 0 Then
            Dim winExcept As New Win32Exception()
            LogMsg(winExcept.Message)
        Else
            LogMsg("all good")
        End If

    End Sub

    Private Sub LogMsg(ByVal msg As String)
        System.Diagnostics.Debug.WriteLine(msg)
        MsgBox(msg)
    End Sub

End Class
于 2013-11-13T02:51:39.660 回答