首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows函数CredUIPromptForWindowsCredentials还返回一个错误31。

Windows函数CredUIPromptForWindowsCredentials还返回一个错误31。
EN

Stack Overflow用户
提问于 2014-09-17 08:06:18
回答 1查看 2.8K关注 0票数 4

当我使用函数CredUIPromptForWindowsCredentials显示windows安全身份验证对话框时,返回结果总是31,而对话框不显示。

下面的代码怎么了?

代码语言:javascript
复制
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);               
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-17 17:07:28

31是ERROR_GEN_FAILURE。如果你读了文档,有一条评论说:

我不知道为什么,但似乎CredUIPromptForWindowsCredentialsA总是返回ERROR_GEN_FAILURE (0x1E)。只有Unicode版本才能工作。

实际上,您正在调用CredUIPromptForWindowsCredentials()的Ansi版本(从将char*数据分配给CREDUI_INFO结构这一事实就可以看出这一点)。尝试调用Unicode版本。

另外,您没有将值赋值给credui.hwndParent字段,也没有在填充它之前将credui归零,因此hwndParent有一个不确定的值。必须指定有效的HWND。如果没有,可以使用NULL

此外,您还将临时string中的一个string指针分配给credui.pszMessageTextstring超出了作用域,在调用CredUIPromptForWindowsCredentials()之前就被销毁了。您需要使用局部变量来保存消息文本,直到使用完CredUIPromptForWindowsCredentials()为止。

试试这个:

代码语言:javascript
复制
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);
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25885369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档