我有一个需要将数据放在网络共享上的程序。在某些情况下,用户无法通过其帐户进行访问,并且需要提供登录凭据。
我的想法是让程序弹出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)。
有没有办法让这个工作?
答案 0 :(得分:7)
您发布的代码似乎有几个问题:
您的P / Invoke类型需要调整。以下是本机Windows类型及其相应的VB .Net对应项:
HWND
- > IntPtr
LPDWORD
- > IntPtr
DWORD
- > UInt32
(我也看过Integer
使用过)使用IntPtr
允许运行时使用32位或64位指针,具体取决于计算机体系结构。即使在64位计算机上,DWORD
也总是32位。另见:Visual C++: How large is a DWORD with 32- and 64-bit code?
P / Invoke签名的用户名和密码参数相反。密码参数实际上应该是第一个。在您的情况下无关紧要,因为系统会提示用户输入此信息。但是,当您尝试在以后传递这些值时,它可能很重要...
远程共享不应该有反斜杠
.lpRemoteName = "\\server\folder\"
应该是
.lpRemoteName = "\\server\folder"
最后,我必须添加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