当我使用函数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); 发布于 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.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);https://stackoverflow.com/questions/25885369
复制相似问题