如何使用Loadstring加载中文字符

How to use Loadstring to load Chinese Characters

本文关键字:中文 字符 加载 Loadstring 何使用      更新时间:2023-10-16

我有一个字符串表,它定义了一个中文字符串,像这样:

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

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

在我的字符串表中是这样的:

检查环境...

但这是屏幕上的结果:

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

默认情况下'MessageBox'函数在窄字符串上工作吗?你不需要使用'MessageBoxW'吗? 编辑:

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

在c++ 11中,引入了3种新的字面值字符串类型,它们的前缀是"u8", "u"answers"u",它们指定UTF-8, UTF-16 &分别utf - 32。据我所知,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.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来验证您要验证的字符。

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

进行编码