将字符串宏/常量转换为宽字符/Unicode

发布于 2024-11-23 18:15:57 字数 1050 浏览 5 评论 0原文

我有一个使用第 3 方库的 Unicode Win32 应用程序,其中一些库将其版本信息的常量提供为#define(窄)字符串。例如, libpng 具有以下内容:

#define PNG_LIBPNG_VER_STRING "1.5.4"
#define PNG_HEADER_VERSION_STRING \
 " libpng version 1.5.4 - July 7, 2011\n"

我正在附加各种 将库版本信息静态链接到我的“关于”框,以便于进行版本跟踪,并且将此常量转换为宽字符串似乎很简单。

我的第一次尝试是 TEXT(PNG_HEADER_VERSION_STRING),但失败了,因为

#define __TEXT(quote) L##quote

.. 并且 LPNGHEADER_VERSION_STRING 当然不存在。

因此,我尝试了双重包装宏的几种组合,以及各种 ## 技巧来尝试将 L 前缀添加到宏的常量中,但未能成功。我错过了一些简单的事情吗?您将如何处理:

#define VERSIONSTR "Test V1.2.3"
const char* ver= VERSIONSTR;
const wchar* wver = _T(VERSIONSTR); // fails, should be L"Test V1.2.3"
#define VERSIONSTRW _T(VERSIONSTR);  // fails also

以编程方式,而不是简单地添加重复的 L"Test V1.2.3" 并使其与第 3 方库保持同步。

我知道如果我是为 Unicode 构建的,我可以在运行时转换它,但我认为肯定有一种快速的方法来重新定义这个常量。

---更新---

我因为对包含结构做了一些非常愚蠢的事情而错过了情节。修复此问题使双重定义包装器能够正常工作。我这方面很愚蠢。

I have a Unicode Win32 application that uses 3rd party libraries, some of which provide constants for their version information as #defined (narrow) strings. For instance, libpng has the following:

#define PNG_LIBPNG_VER_STRING "1.5.4"
#define PNG_HEADER_VERSION_STRING \
 " libpng version 1.5.4 - July 7, 2011\n"

I'm appending the various statically linked libraries version information to my About Box for easy version tracking, and it seemed like it would be simple to convert this constant into a wide string.

My first attempt was TEXT(PNG_HEADER_VERSION_STRING), but that fails as

#define __TEXT(quote) L##quote

.. and LPNGHEADER_VERSION_STRING doesn't exist of course.

So I tried several combinations of double wrapping macros, and all sorts of ## tricks to attempt to add the L prefix to a macro'ed constant, but wasn't able to. Am I missing something simple? How would you handle:

#define VERSIONSTR "Test V1.2.3"
const char* ver= VERSIONSTR;
const wchar* wver = _T(VERSIONSTR); // fails, should be L"Test V1.2.3"
#define VERSIONSTRW _T(VERSIONSTR);  // fails also

programmatically, without simply adding a duplicate L"Test V1.2.3" and having to keep it in sync with the 3rd party library.

I know I can just convert it at runtime if I'm building for Unicode, but I thought surely there was a quick way to re-define this constant.

---UPDATE---

I missed the plot by doing something really stupid with my include structure. Fixing that allowed the double define wrapper to function as it should. Stupid on my part.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

☆獨立☆ 2024-11-30 18:15:57

诀窍是使用两个宏。当宏被扩展时,参数在被替换到替换列表之前被宏扩展。因此,WIDEN(VERSIONSTR) 变为 WIDEN2("Test V1.2.3")

#define WIDEN(quote) WIDEN2(quote)
#define WIDEN2(quote) L##quote

#define VERSIONSTR "Test V1.2.3"
#define VERSIONSTRW WIDEN(VERSIONSTR)

The trick is to use two macros. When a macro is expanded, the arguments are macro-expanded before being substituted into the replacement list. So WIDEN(VERSIONSTR) becomes WIDEN2("Test V1.2.3").

#define WIDEN(quote) WIDEN2(quote)
#define WIDEN2(quote) L##quote

#define VERSIONSTR "Test V1.2.3"
#define VERSIONSTRW WIDEN(VERSIONSTR)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文