我最近问过这个问题但由于信息不足而被删除。我将尝试再次提出更多信息。
我试图调用Win32 API函数CredUIPromptForWindowsCredentialsW()
但过去两天都失败了。
我还阅读了一些帖子,例如:
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
我的代码最后是我认为应该为调用定义所有参数,但是这会返回错误代码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是我现在收到的。
'
' 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,上面的完成代码。