I recently asked this question but deleted due to insufficient information. I will attempt to ask it again hopefully with more information.
I am trying to call the Win32 API function CredUIPromptForWindowsCredentialsW()
but have been failing for the past two days.
I have also read a few posts, such as:
VBA: Unicode Strings and the Windows API
Data type conversions for API calls from Visual Basic
How to convert Windows API declarations in VBA for 64-bit
My code at the end is how I think all of the parameters should be defined for the call, however this returns error code 31:
ERROR_GEN_FAILURE
31 (0x1F)
A device attached to the system is not functioning.
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
EDIT: This code now returns 31 error code, with thanks to the comment section for pointing out some changes in my declarations. It no longer crashes word, but the Error 31 is what I receive now.