簡體   English   中英

REG_SZ值另存為日語文本

[英]REG_SZ value saved as Japanese text

我正在嘗試通過RegSetValueEx將一些值寫入Windows注冊表,但是這些值以“日語形式”保存。 例如:

“整瑳湩ㅧ㌲”應為“ testing123”

在十六進制編輯器中查看文本時,該文本似乎是正確的值,但前面帶有“ FF FE”,這似乎是字節順序標記。

保存該值的代碼如下:

RegSetValueEx(
    RegistryUtils::registryKey,
    L"test",
    0,
    REG_SZ,
    (unsigned char*)config.getTestValue().c_str(),
    strlen(config.getTestValue().c_str()) + 1
);

其中config.getTestValue()返回std::string類型。

如何防止將“ FF FE”添加到所需的字符串?

RegSetValueExW的字符串數據必須是寬文本,並且size參數必須是字節數,包括結尾的零。

這很好用:

#undef UNICODE
#define UNICODE
#include <windows.h>

#include <string.h>     // strlen

namespace RegistryUtils
{
    auto const registryKey = HKEY_CURRENT_USER;
};

auto main()
    -> int
{
    wchar_t const* const s = L"blah";

    RegSetValueEx(
        RegistryUtils::registryKey,
        L"test",
        0,
        REG_SZ,
        reinterpret_cast<BYTE const*>( s ),
        sizeof(wchar_t)*(wcslen(s) + 1)
    );
}

閱讀有問題的函數的文檔是個好主意。

您正在調用RegSetValueEx()的Unicode版本,該版本需要UTF-16格式的字符串數據,但是您正在傳遞Ansi數據。 將數據放入std::wstring而不是std::string ,還要記住RegSetValueEx()是對字節而不是字符進行操作:

std::wstring value = config.getTestValueW(); // <-- for you to implement
RegSetValueEx(
    RegistryUtils::registryKey,
    L"test",
    0,
    REG_SZ,
    (BYTE*) value.c_str(),
    (value.length() + 1) * sizeof(WCHAR)
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM