0

我有一个字符串表,它定义了一个中文字符串,如下所示:

STRINGTABLE
    LANGAUGE 0x0C04, 0x03
BEGIN
    1000    "检查环境..."
    ...
END

我正在尝试将该字符串加载到 wchar_t 缓冲区中,如下所示:

#define UNICODE
#define _UNICODE
wchar_t buffer[512];
LoadString(DLL_HANDLE, (UINT) msg_num, buffer, 512);
MessageBox(NULL, buffer, NULL, NULL);

但是,加载到缓冲区中的字符串与我的字符串表中的字符串不同。

在我的字符串表中看起来像这样:

检查环境...

但这就是它在屏幕上的结果:

環境をãƒã‚§ãƒƒã‚¯ä¸­...
4

2 回答 2

0

MSDN 文档指出格式应类似于 IDS_CHINESESTRING L"\x5e2e\x52a9". 这不是最正式的描述。我将其解释为说明 unicode 字符串必须以转义码为前缀L并使用\uxxxx转义码进行编码

于 2013-04-11T08:09:45.097 回答
0

'MessageBox' 功能是否默认适用于窄字符串?你不需要使用'MessageBoxW'吗?

编辑:

有几件事要检查。L"..." 字符串的编码是实现定义的。该标准没有提及 ; 字符的编码wchar_t。确保您使用的编码与 Windows 预期的相同。(如果我没记错的话,windows 需要 UTF-16 - 但我很可能是错的)。

在 C++11 中,引入了 3 种新的文字字符串类型,它们的前缀是“u8”、“u”和“U”,分别指定 UTF-8、UTF-16 和 UTF-32。除了第 2.14.3 节中提到的内容外,C++11 仍然不保证对“L”前缀的编码做出任何保证:

A character literal that begins with the letter L, such as L’x’, is a wide-character literal. A wide-character
literal has type wchar_t.23 The value of a wide-character literal containing a single c-char has value equal
to the numerical value of the encoding of the c-char in the execution wide-character set, unless the c-char
has no representation in the execution wide-character set, in which case the value is implementation-defined.
[ Note: The type wchar_t is able to represent all members of the execution wide-character set (see 3.9.1).
—end note ]. The value of a wide-character literal containing multiple c-chars is implementation-defined.

参考§3.9.1 P5 状态:

Type wchar_t is a distinct type whose values can represent distinct codes for all members of the largest
extended character set specified among the supported locales (22.3.1). Type wchar_t shall have the same
size, signedness, and alignment requirements (3.11) as one of the other integral types, called its underlying
type. Types char16_t and char32_t denote distinct types with the same size, signedness, and alignment as
uint_least16_t and uint_least32_t, respectively, in <stdint.h>, called the underlying types.

同样,没有提到编码。Windows 可能期望与您的资源字符串使用的编码不同,因此存在差异。

您可以通过使用带有“\Uxxxxxxx”编码转义符的 L"" 字符串文字调用 MessageBox 来验证您的字符。

于 2013-04-10T23:06:36.167 回答