- 1 year ago
CrerdWriteW storing credentials in Mandarin on Windows
To store credentials using the CredWriteW
function in Windows and ensure that they are stored correctly in Mandarin (Chinese), you need to ensure that the credentials are encoded and passed as Unicode (UTF-16) strings.
Here's an example of how you can store credentials in Mandarin using CredWriteW
in C++:
cpp#include <windows.h>
#include <wincred.h>
int main() {
PCWSTR targetName = L"MyTarget";
PCWSTR userName = L"用户名";
PCWSTR password = L"密码";
CREDENTIAL credential = {};
credential.Type = CRED_TYPE_GENERIC;
credential.TargetName = const_cast<PWSTR>(targetName);
credential.UserName = const_cast<PWSTR>(userName);
credential.CredentialBlob = reinterpret_cast<BYTE*>(const_cast<PWSTR>(password));
credential.CredentialBlobSize = wcslen(password) * sizeof(WCHAR);
credential.Persist = CRED_PERSIST_LOCAL_MACHINE;
BOOL result = CredWriteW(&credential, 0);
if (result) {
wprintf(L"Credentials stored successfully.\n");
} else {
wprintf(L"Failed to store credentials. Error: %d\n", GetLastError());
}
return 0;
}
In this example, the targetName
, userName
, and password
variables are defined as Unicode strings (UTF-16). The CREDENTIAL
structure is populated with the necessary information, including the target name, Mandarin username, and password.
Ensure that your code uses wide character (Unicode) strings (L"..."
) to store and pass the Mandarin text correctly. The CredWriteW
function is then called to store the credentials.
Compile and run the code, and the CredWriteW
function should store the credentials in Mandarin correctly.
Remember to include the necessary headers (windows.h
and wincred.h
) and link against the appropriate library (Advapi32.lib
) when building your application.
Note that the example provided is in C++, but the same concept applies to other languages that can call Windows API functions. Adjust the code accordingly for your programming language of choice.