如何使用Loadstring加载中文字符

时间:2013-04-10 21:12:22

标签: c++ arrays string unicode wchar

我有一个字符串表,用中文定义一个字符串:

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);

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

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

检查环境...

但这就是屏幕上的结果:

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

2 个答案:

答案 0 :(得分:0)

'MessageBox'函数是否通过deault对窄字符串起作用?你不需要使用'MessageBoxW'吗?

编辑:

要检查的几件事。 L“...”字符串的编码是实现定义的。该标准未提及wchar_t的字符编码;确保你使用的是与windows预期相同的编码。 (如果我没记错的话,Windows预计UTF-16 - 但我很可能在这方面错了。)

在C ++ 11中,引入了3个新的文字字符串类型,它们的前缀是“u8”,“u”和“U”,它们指定了UTF-8,UTF-16和UT。 UTF-32,分别。除了§2.14.3中提到的内容之外,C ++ 11仍然无法保证编码“L”前缀,除了§2.14.3中提到的内容:

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.1P5陈述:

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进行验证,以便验证字符。

答案 1 :(得分:0)

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

相关问题