【发布时间】:2018-10-12 20:02:25
【问题描述】:
我不知道为什么我不能用 CredUnPackAuthenticationBufferW 解压 CredUIPromptForWindowsCredentials 中使用的身份验证缓冲区,我总是收到 ERROR_INSUFFICIENT_BUFFER 错误。 感谢您的帮助。
std::wstring caption = L"Caption";
std::wstring msg= L"Msg";
CREDUI_INFOW credui = {};
credui.cbSize = sizeof(credui);
credui.hwndParent = nullptr;
credui.pszMessageText = msg.c_str();
credui.pszCaptionText = caption.c_str();
credui.hbmBanner = nullptr;
ULONG authPackage = 0;
LPVOID outCredBuffer = nullptr;
ULONG outCredSize = 0;
BOOL save = false;
LPWSTR pszUserName = nullptr;
DWORD pcchlMaxUserName = 0;
LPWSTR pszDomainName = nullptr;
DWORD pcchMaxDomainName = 0;
LPWSTR pszPassword = nullptr;
DWORD pcchMaxPassword = 0;
DWORD result = CredUIPromptForWindowsCredentialsW(&credui,
0,
&authPackage,
nullptr,
0,
&outCredBuffer,
&outCredSize,
&save,
CREDUIWIN_ENUMERATE_ADMINS);
std::cout <<CredUnPackAuthenticationBufferW(CRED_PACK_PROTECTED_CREDENTIALS
,outCredBuffer
,outCredSize
,pszUserName
,&pcchlMaxUserName
,pszDomainName
,&pcchMaxDomainName
,pszPassword
,&pcchMaxPassword) << std::endl;
std::cout << GetLastError() << std::endl; // out put 122 == ERROR_INSUFFICIENT_BUFFER
【问题讨论】:
-
当然你必须得到
ERROR_INSUFFICIENT_BUFFER,因为你传递了0大小的缓冲区。您需要为pszUserName、pszPassword和pszDomainName分配一些缓冲区。如果大小不足 - 使用返回值(pcchlMaxUserName、pcchMaxDomainName、pcchMaxPassword) - 重新分配缓冲区并再次调用 api