Я недавно задал этот вопрос, но удалил из-за недостатка информации. Я попытаюсь спросить это снова, надеюсь, с дополнительной информацией.
Я пытаюсь вызвать функцию Win32 API CredUIPromptForWindowsCredentialsW()
, но последние два дня не получалось.
Я также прочитал несколько сообщений, таких как:
VBA: строки Unicode и Windows API
Преобразования типов данных для вызовов API из Visual Basic
Как преобразовать объявления Windows API в VBA для 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.