【问题标题】:How do I initialize a WCHAR string from an ascii string that is #defined. L prefix is not included如何从#defined 的 ascii 字符串初始化 WCHAR 字符串。不包括 L 前缀
【发布时间】:2019-09-10 04:13:06
【问题描述】:

在 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 驱动程序调用的转换例程。

【问题讨论】:

    标签: unicode driver string-literals wchar


    【解决方案1】:

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

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

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

    【讨论】:

    • 这让我朝着正确的方向开始。它需要 2 个级别的宏才能让预处理器按照我需要的方式处理。
    • 我刚刚在我的文件中添加了#defines,它运行良好,但是是否有包含这些宏的标准 Windows/C 包含文件?我找不到它。
    猜你喜欢
    • 2011-05-04
    • 1970-01-01
    • 2013-05-20
    • 2022-11-17
    • 2018-07-31
    • 2015-09-16
    • 2017-05-02
    • 2013-04-29
    • 2011-07-24
    相关资源
    最近更新 更多