【发布时间】:2021-07-05 18:25:13
【问题描述】:
我有以下 wstring:
std::wstring testVal("Test");
我需要把它放在这个值里面:
static const TCHAR* s_test_val;
到目前为止我已经尝试过:
static const TCHAR* s_test_val = (const wchar_t*) testVal.c_str();
static const TCHAR* s_test_val = (wchar_t*) testVal.c_str();
static const TCHAR* s_test_val = (TCHAR*) testVal.c_str();
static const TCHAR* s_test_val = (TCHAR*) testVal;
但没有成功; s_test_val 一直显示为空字符串。
【问题讨论】:
-
早在 1990 年代后期,Microsoft 有两个操作系统系列,基于 ANSI 的 Windows 9X 和基于 Unicode 的 Windows NT。为了方便程序员为两个 OS 行编写相同的代码,微软隐藏了一系列宏背后的差异,这些宏根据编译器选项在 ANSI 字符和 Unicode 字符之间来回切换,
TCHAR是其中之一那些宏。在 2000 年代初期,Windows NT 几乎用 Windows XP 彻底消灭了 Windows 9X,而且可能您在过去 15 年中使用的每台 Windows 计算机都使用 Unicode。 -
除非出于某种原因您需要支持 20 多年前的 Windows 操作系统以及现代操作系统,否则您不应该使用
TCHAR并使用宽字符和各种 ...w 函数或确保在编译时指定了 unicode。不过,如果您只是破解某些东西,您可能会为了简单起见坚持使用 8 位字符。