Windows API函数CredUIPromptForWindowsCredentials也返回错误31
Windows API function CredUIPromptForWindowsCredentials also returns an error of 31
当我使用函数CredUIPromptForWindowsCredentials显示windows安全身份验证对话框时,返回结果始终为31,并且不显示该对话框
下面的代码出了什么问题?
CREDUI_INFO credui;
credui.pszCaptionText = "Enter Network Password";
credui.pszMessageText = ("Enter your password to connect to: " + strDbPath).c_str();
credui.cbSize = sizeof(credui);
credui.hbmBanner = nullptr;
ULONG authPackage = 0;
LPVOID outCredBuffer = nullptr;
ULONG outCredSize = 0;
BOOL save = false;
int result = CredUIPromptForWindowsCredentials(&credui, 0, &authPackage, nullptr, 0, &outCredBuffer, &outCredSize, &save, 1);
31是ERROR_GEN_FAILURE
。如果你阅读了文档,有一条评论说:
我不知道为什么,但似乎CredUIPromptForWindowsCredentialsA总是返回ERROR_GEN_FAILURE(0x1E)。只有Unicode版本有效。
事实上,您正在调用CredUIPromptForWindowsCredentials()
的Ansi版本(通过将char*
数据分配给CREDUI_INFO
结构可以明显看出)。请尝试调用Unicode版本。
此外,您没有为credui.hwndParent
字段指定值,也没有在填充credui
之前将其清零,因此hwndParent
的值不确定。您必须指定一个有效的HWND
。如果没有,可以使用NULL
。
此外,您正在将char*
指针从临时string
分配给credui.pszMessageText
。string
超出范围并在调用CredUIPromptForWindowsCredentials()
之前被销毁。您需要使用一个本地变量来保存消息文本,直到CredUIPromptForWindowsCredentials()
使用完毕。
试试这个:
std::wstring strDbPath = ...;
std::wstring strMsg = L"Enter your password to connect to: " + strDbPath;
CREDUI_INFOW credui = {};
credui.cbSize = sizeof(credui);
credui.hwndParent = nullptr;
credui.pszMessageText = strMsg.c_str();
credui.pszCaptionText = L"Enter Network Password";
credui.hbmBanner = nullptr;
ULONG authPackage = 0;
LPVOID outCredBuffer = nullptr;
ULONG outCredSize = 0;
BOOL save = false;
int result = CredUIPromptForWindowsCredentialsW(&credui, 0, &authPackage, nullptr, 0, &outCredBuffer, &outCredSize, &save, 1);
相关文章:
- (C++)分析树以计算返回错误值的简单算术表达式
- glad 导致 glfwSwapBuffers 返回错误消息
- 我的模板类方法返回错误类型?
- 共享内存:MapViewOfFile 返回错误 5
- 使用 std::forward_list 返回错误的队列实现
- FFMPEG:sws_scale返回错误:切片参数 0, 2160 无效
- __int64 CString 返回错误的值 - C++ MFC
- C++ OpenGL 中,绘制形状返回错误
- Python os.system() 返回错误值
- 函数从指针 c++ 中获取错误的值并返回错误的答案
- LibClang clang_getArgType() 返回错误的类型
- 除法函数返回错误的值
- 当命令失败时,gzip 会在 C++ 中返回错误代码吗?
- 矩阵逆变器返回错误的反转
- openCL-创建子缓冲区返回错误代码13
- Qt SQL LIKE语句返回错误
- GetLastError()从FtpPutFile()返回错误6
- PfCreateInterface返回错误120(未实现)
- 为什么 Pa_GetStreamTime() 返回错误?
- G++ 在尝试使用 G++ 使用 openCV 进行编译后返回错误