首页 > 解决方案 > 如何从#defined 的 ascii 字符串初始化 WCHAR 字符串。不包括 L 前缀

问题描述

在 Windows 驱动程序 C 代码中,我需要将 WCHAR 数组设置为在头文件中 #defined 的字符串。头文件指定一个 ascii 字符串。它没有为字符串指定 L 前缀。

// In the header file
#define VERS_STR "3.2.4"

// In the C file, none of the following work
WCHAR awcStr[] = LVERS_STR;    // Compiler Error: Treated as one name
WCHAR awcStr[] = L VERS_STR;   // Compiler Error: L is unknown
WCHAR awcStr[] = L(VERS_STR);  // Compiler Error
WCHAR awcStr[] = L"3.2.4";     // Compiles and runs, but I must use #define instead

我会在#define 上调用一个转换例程,但是我找不到可以使用C 代码从Windows 驱动程序调用的转换例程。

标签: unicodedriverstring-literalswchar

解决方案


可能存在 WIDE 宏,但我无法轻松检查,但这个序列实现了它:

#define WIDE2(x) L##x
#define WIDE1(x) WIDE2(x)
#define WIDE(x) WIDE1(x)

由于宏的工作方式,WIDE1 将扩展您的宏,而 WIDE2 将使用连接宏运算符添加 L。只要打电话WIDE(VERS_STR)


推荐阅读