MrSilverSnorkel
MrSilverSnorkel

Reputation: 509

WideCharToMultiByte produces Chinese characters when writing to registery

I have this ancient bit of code for an IE BHO, and I'm trying to store some data for it.

I'm trying this approach:

// key is an LPWSTR provided as a method parameter
// sValue is an LPWSTR provided as a method parameter

HKEY hKey = NULL;

HRESULT hRes = IEGetWriteableHKCU(&hKey);

if (SUCCEEDED(hRes) && hKey)
{
    CHAR mbStr[2048] = { 0 };
    WideCharToMultiByte(CP_UTF8, 0, sValue, lstrlen(sValue), mbStr, 2048, NULL, NULL);

     // Skipping error checking for now
    RegSetKeyValue(hKey, L"A\\Key\\I\\Can\\Write\\To", key, REG_SZ, mbStr, (DWORD)strnlen_s(mbStr, 2048));

    RegCloseKey(hKey);
}

The value I enter is written, and can be successfully read. However, when I look at it in regedit, I see Chinese characters. This will make troubleshooting challenging, so I would like to have the characters written correctly.

This is obviously some sort of encoding issue, but I can't seem to find the magic bullet to solve it.

Thanks!

Upvotes: 0

Views: 427

Answers (0)

Related Questions