0

我最近问了这个问题,但由于信息不足而被删除。我会尝试再次询问,希望能提供更多信息。

我正在尝试调用 Win32 API 函数CredUIPromptForWindowsCredentialsW(),但过去两天一直失败。

我还阅读了一些帖子,例如:

VBA:Unicode 字符串和 Windows API

从 Visual Basic 调用 API 的数据类型转换

如何将 VBA 中的 Windows API 声明转换为 64 位

我最后的代码是我认为应该为调用定义所有参数的方式,但是这会返回错误代码 31:

ERROR_GEN_FAILURE

31 (0x1F)

连接到系统的设备不工作。

Public Declare PtrSafe Function CredUIPromptForWindowsCredentials Lib "credui" Alias "CredUIPromptForWindowsCredentialsW" ( _
ByRef pUiInfo As CREDUI_INFO, _
ByVal dwAuthError As LongPtr, _
ByRef pulAuthPackage As LongPtr, _
ByVal pvInAuthBuffer As Long, _
ByRef ulInAuthBufferSize As LongPtr, _
ByVal ppvOutAuthBuffer As Long, _
ByRef pulOutAuthBufferSize As LongPtr, _
ByVal iSave As Long, _
ByVal dwFlags As Long) _
As Long

Public Declare PtrSafe Function CredUnPackAuthenticationBuffer Lib "credui" Alias "CredUnPackAuthenticationBufferW" ( _
ByRef dwFlags As LongPtr, _
ByRef pAuthBuffer As LongPtr, _
ByRef cbAuthBuffer As LongPtr, _
ByRef pszUserName As LongPtr, _
ByRef pcchMaxUserName As LongPtr, _
ByRef pszDomainName As LongPtr, _
ByRef pcchMaxDomainName As LongPtr, _
ByRef pszPassword As LongPtr, _
ByRef pcchMaxPassword As LongPtr) _
As LongPtr

Public Enum CREDUI_FLAGS
            INCORRECT_PASSWORD = &H1
            DO_NOT_PERSIST = &H2
            REQUEST_ADMINISTRATOR = &H4
            EXCLUDE_CERTIFICATES = &H8
            REQUIRE_CERTIFICATE = &H10
            SHOW_SAVE_CHECK_BOX = &H40
            ALWAYS_SHOW_UI = &H80
            REQUIRE_SMARTCARD = &H100
            PASSWORD_ONLY_OK = &H200
            VALIDATE_USERNAME = &H400
            COMPLETE_USERNAME = &H800
            PERSIST = &H1000
            SERVER_CREDENTIAL = &H4000
            EXPECT_CONFIRMATION = &H20000
            GENERIC_CREDENTIALS = &H40000
            USERNAME_TARGET_CREDENTIALS = &H80000
            KEEP_USERNAME = &H100000
End Enum

Private Const BUFFER_SIZE As Integer = &H100
Private Const ERROR_CANCELLED As Integer = &H4C7
Private Const CREDUIWIN_GENERIC As Integer = &H1
Private Const CREDUIWIN_CHECKBOX As Integer = &H2
Private Const CREDUIWIN_ENUMERATE_CURRENT_USER As Integer = &H200
Private Const CREDUIWIN_IN_CRED_ONLY As Integer = &H20
Private Const CREDUIWIN_AUTHPACKAGE_ONLY As Integer = &H10
Private Const CREDUIWIN_ENUMERATE_ADMINS As Integer = &H100
Private Const CRED_PACK_PROTECTED_CREDENTIALS As Integer = &H1
Private Const CRED_PACK_GENERIC_CREDENTIALS As Integer = &H4
Private Const MAX_USER_NAME As Integer = 100
Private Const MAX_PASSWORD As Integer = 100
Private Const MAX_DOMAIN As Integer = 100

Public Type CREDUI_INFO
    cbSize As Long
    hwndParent As Long
    pszMessageText As LongPtr
    pszCaptionText As LongPtr
    hbmBanner As Long
End Type

Public Function GetCredentials()

    Dim pUiInfo As CREDUI_INFO

    Dim pulAuthPackage As LongPtr
    Dim iSave As Long
    Dim result As Long
    Dim dwFlags As Long
    Dim ulInAuthBufferSize As LongPtr
    Dim pulOutAuthBufferSize As LongPtr
    Dim pvInAuthBuffer As Long
    Dim ppvOutAuthBuffer As Long

    pUiInfo.cbSize = LenB(pUiInfo)
    pUiInfo.hwndParent = 0
    pUiInfo.pszMessageText = StrPtr("message")
    pUiInfo.pszCaptionText = StrPtr("title")

    pulAuthPackage = 0
    iSave = 0
    dwFlags = CREDUIWIN_CHECKBOX + CREDUIWIN_ENUMERATE_CURRENT_USER + CREDUIWIN_ENUMERATE_ADMINS
    ppvOutAuthBuffer = 0
    pvInAuthBuffer = 0
    ulInAuthBufferSize = 0
    pulOutAuthBufferSize = 0

    result = CredUIPromptForWindowsCredentials( _
    pUiInfo, _
    0, _
    authPackage, _
    pvInAuthBuffer, _
    ulInAuthBufferSize, _
    ppvOutAuthBuffer, _
    pulOutAuthBufferSize, _
    iSave, _
    dwFlags)

    MsgBox result

End Function

编辑:此代码现在返回 31 个错误代码,感谢评论部分指出我的声明中的一些更改。它不再崩溃,但错误 31 是我现在收到的。

4

1 回答 1

0
'
' CREDUIAPI DWORD CredUIPromptForWindowsCredentialsW(
'  PCREDUI_INFOW pUiInfo,_
'  DWORD         dwAuthError,
'  ULONG         *pulAuthPackage,
'  LPCVOID       pvInAuthBuffer,
'  ULONG         ulInAuthBufferSize,
'  LPVOID        *ppvOutAuthBuffer,
'  ULONG         *pulOutAuthBufferSize,
'  BOOL          *pfSave,
'  DWORD dwFlags
');
'
Public Declare PtrSafe Function CredUIPromptForWindowsCredentials Lib "credui" Alias "CredUIPromptForWindowsCredentialsW" ( _
ByRef pUiInfo As CREDUI_INFO, _
ByVal dwAuthError As Long, _
ByRef pulAuthPackage As LongPtr, _
ByVal pvInAuthBuffer As Any, _
ByVal ulInAuthBufferSize As Long, _
ByRef ppvOutAuthBuffer As Any, _
pulOutAuthBufferSize As LongPtr, _
ByRef iSave As Long, _
ByVal dwFlags As Long) _
As Long

Public Declare PtrSafe Function CredUnPackAuthenticationBuffer Lib "credui" Alias "CredUnPackAuthenticationBufferW" ( _
ByRef dwFlags As LongPtr, _
ByRef pAuthBuffer As LongPtr, _
ByRef cbAuthBuffer As LongPtr, _
ByRef pszUserName As LongPtr, _
ByRef pcchMaxUserName As LongPtr, _
ByRef pszDomainName As LongPtr, _
ByRef pcchMaxDomainName As LongPtr, _
ByRef pszPassword As LongPtr, _
ByRef pcchMaxPassword As LongPtr) _
As LongPtr

Public Enum CREDUI_FLAGS
            INCORRECT_PASSWORD = &H1
            DO_NOT_PERSIST = &H2
            REQUEST_ADMINISTRATOR = &H4
            EXCLUDE_CERTIFICATES = &H8
            REQUIRE_CERTIFICATE = &H10
            SHOW_SAVE_CHECK_BOX = &H40
            ALWAYS_SHOW_UI = &H80
            REQUIRE_SMARTCARD = &H100
            PASSWORD_ONLY_OK = &H200
            VALIDATE_USERNAME = &H400
            COMPLETE_USERNAME = &H800
            PERSIST = &H1000
            SERVER_CREDENTIAL = &H4000
            EXPECT_CONFIRMATION = &H20000
            GENERIC_CREDENTIALS = &H40000
            USERNAME_TARGET_CREDENTIALS = &H80000
            KEEP_USERNAME = &H100000
End Enum

Private Const BUFFER_SIZE As Integer = &H100
Private Const ERROR_CANCELLED As Integer = &H4C7
Private Const CREDUIWIN_GENERIC As Integer = &H1
Private Const CREDUIWIN_CHECKBOX As Integer = &H2
Private Const CREDUIWIN_ENUMERATE_CURRENT_USER As Integer = &H200
Private Const CREDUIWIN_IN_CRED_ONLY As Integer = &H20
Private Const CREDUIWIN_AUTHPACKAGE_ONLY As Integer = &H10
Private Const CREDUIWIN_ENUMERATE_ADMINS As Integer = &H100
Private Const CRED_PACK_PROTECTED_CREDENTIALS As Integer = &H1
Private Const CRED_PACK_GENERIC_CREDENTIALS As Integer = &H4
Private Const MAX_USER_NAME As Integer = 100
Private Const MAX_PASSWORD As Integer = 100
Private Const MAX_DOMAIN As Integer = 100

'typedef struct _CREDUI_INFOA {
'  DWORD   cbSize;
'  HWND    hwndParent;
'  PCSTR   pszMessageText;
'  PCSTR   pszCaptionText;
'  HBITMAP hbmBanner;
'} CREDUI_INFOA, *PCREDUI_INFOA;
Public Type CREDUI_INFO
    cbSize As Long
    hwndParent As LongPtr
    pszMessageText As LongPtr
    pszCaptionText As LongPtr
    hbmBanner As Long
End Type

Public Function GetCredentials()

    Dim pUiInfo As CREDUI_INFO

    Dim pulAuthPackage As LongPtr
    Dim iSave As Long
    Dim result As Long
    Dim dwFlags As Long
    Dim ulInAuthBufferSize As Long
    Dim pulOutAuthBufferSize As LongPtr
    Dim pvInAuthBuffer As LongPtr
    Dim ppvOutAuthBuffer As LongPtr

    pUiInfo.cbSize = LenB(pUiInfo)
    pUiInfo.hwndParent = 0
    pUiInfo.pszMessageText = StrPtr("message")
    pUiInfo.pszCaptionText = StrPtr("title")

    pulAuthPackage = 0
    iSave = 0
    dwFlags = CREDUIWIN_CHECKBOX + CREDUIWIN_ENUMERATE_CURRENT_USER + CREDUIWIN_ENUMERATE_ADMINS

    ppvOutAuthBuffer = 0
    pvInAuthBuffer = 0

    ulInAuthBufferSize = 0
    pulOutAuthBufferSize = 0

    result = CredUIPromptForWindowsCredentials( _
    pUiInfo, _
    0, _
    authPackage, _
    pvInAuthBuffer, _
    ulInAuthBufferSize, _
    ppvOutAuthBuffer, _
    pulOutAuthBufferSize, _
    iSave, _
    dwFlags)

    MsgBox result

End Function

感谢 Anders,我现在可以使用它,最后的更改是将 HWND 设置为 LongPtr,上面的已完成代码。

于 2019-04-02T19:06:26.123 回答