[英]CrerdWriteW storing credentials in Mandarin on Windows
I used the answer here to add credentials programmatically to Windows Credential Manager.我使用这里的答案以编程方式将凭据添加到 Windows 凭据管理器。 The code is inspired by the code in the answer.
该代码的灵感来自答案中的代码。 When I run it however, the credentials in the cred manager show up in Mandarin.
但是,当我运行它时,信用管理器中的凭据以普通话显示。 I am not sure what am I doing wrong.
我不确定我做错了什么。 Would appreciate any pointers.
将不胜感激任何指针。 TIA .
蒂亚。
For references this is the code I have作为参考,这是我的代码
#include <iostream>
#include "windows.h"
#include "wincred.h"
#pragma hdrstop
using namespace std;
int main()
{
const char* password = "testpass";
CREDENTIALW creds = { 0 };
creds.Type = CRED_TYPE_GENERIC;
creds.TargetName = (LPWSTR)("testaccount");
creds.CredentialBlobSize = strlen(password) + 1;
creds.CredentialBlob = (LPBYTE)password;
creds.Persist = CRED_PERSIST_LOCAL_MACHINE;
creds.UserName = (LPWSTR)("testuser");
BOOL result = CredWriteW(&creds, 0);
if (result != TRUE)
{
cout << "Some error occurred" << endl;
}
else
{
cout << "Stored the password successfully" << endl;
}
return 0;
}
To ensure there is no default language problem, I manually created a credential from within the credential manager for test.com and had no problems with it.为了确保没有默认语言问题,我从 test.com 的凭据管理器中手动创建了一个凭据,并且没有任何问题。 Snapshot of the Cred Manager -
信用管理器的快照 -
Appearantly, TargetName needs to refer to a mutable array, ie not a string literal.显然,TargetName 需要引用一个可变数组,即不是字符串文字。 It also needs to be a wide string, or else the characters will be interpreted wrongly, in this case resulting in Chinese characters.
它也需要是一个宽字符串,否则字符会被错误解释,在这种情况下会导致中文字符。
The solution is to define a mutable array that is initialized with a wide string, and have TargetName
point to it:解决方案是定义一个用宽字符串初始化的可变数组,并让
TargetName
指向它:
WCHAR targetName [] = L"testuser";
creds.TargetName = targetName;
This way, no suspicious cast is needed to make it compile.这样,就不需要可疑的演员表来编译它。 When you want to input non-hardcoded strings (eg from user input or a file), you need to make sure they are correctly encoded and convert appropriately.
当您想输入非硬编码字符串(例如来自用户输入或文件)时,您需要确保它们被正确编码并适当转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.