我有一个程序需要将数据放在网络共享上。在某些情况下,用户无法通过其帐户访问,并且需要提供凭据才能登录。
我的想法是让程序弹出 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)。
有没有办法让这个工作?