如何使用CredUICmdLinePromptForCredentials?

问题描述 投票:0回答:1

我曾尝试编写一个使用无人使用的功能的应用程序(CredUICmdLinePromptForCredentials)。但结果是错误的,用户和传递变量以十六进制值输出。

这是我的代码:

#include <windows.h>
#include <wincred.h>
#include <iostream>
#pragma comment(lib,"Credui.lib")
int main()
{
    DWORD dwErr;
    WCHAR szUserName[CREDUI_MAX_USERNAME_LENGTH] = {NULL};
    WCHAR szPassword[CREDUI_MAX_PASSWORD_LENGTH] = {NULL};
    BOOL fSave;
    DWORD dwAuthError=1223;

    dwErr = CredUICmdLinePromptForCredentials(
        L".",
        NULL,
        dwAuthError,
        (PWSTR)&szUserName,
        CREDUI_MAX_USERNAME_LENGTH + 1,
        (PWSTR)&szPassword,
        CREDUI_MAX_PASSWORD_LENGTH + 1,
        &fSave,
        CREDUI_FLAGS_DO_NOT_PERSIST|CREDUI_FLAGS_EXCLUDE_CERTIFICATES
    );

    if (dwErr == ERROR_SUCCESS)
    {
        std::cout << "User Name: " << szUserName << std::endl;
        std::cout << "Password : " << szPassword << std::endl;
    }
    else if (dwErr == ERROR_CANCELLED)
    {
        std::cout << "User cancelled the operation." << std::endl;
    }
    else
    {
        std::cout << "Error occurred: " << dwErr << std::endl;
    }

    return 0;
}

这是结果:

用户名:000000C5402FED80

密码:000000C5402FF1B0

如果我将 ERROR_SUCCESS 的触发代码更改为:

        std::cout << "User Name: " << char(szUserName[0]) << std::endl;
        std::cout << "Password : " << char(szPassword[0]) << std::endl;

一切顺利,字符显示正确。

c++ winapi credui
1个回答
0
投票

请查看

szPassword
szUserName
。其定义如下:

    WCHAR szUserName[CREDUI_MAX_USERNAME_LENGTH] = {NULL};
    WCHAR szPassword[CREDUI_MAX_PASSWORD_LENGTH] = {NULL};

同时,我们将字符串打印为

std::cout
,仅使用 ASCII 字符串,或
char
const char
char*
const char*

应该是这样的:

        std::wcout << L"User Name: " << szUserName << std::endl;
        std::wcout << L"Password : " << szPassword << std::endl;

(因为

std::wcout
是为 Unicode 字符串设计的,而
std::cout
不是。)

© www.soinside.com 2019 - 2024. All rights reserved.