c++ - 为什么在 win32 中有不同的 TEXT like macros for same thing?

标签 c++ windows winapi

我想知道为什么会出现 T、TEXT、_TEXT、__TEXT 或 __T 等宏,而它们最终都做同样的事情。

如果定义了 UNICODE,则将“字符串”映射到 L“字符串”。

感谢您的回答。在更实际的方法中,有人可以向我解释下面给出的代码的行为吗?

#include <stdio.h>
#include <conio.h>
#include <tchar.h>   // For _T and _TEXT
#include <windows.h> // For __TEXT 

int __cdecl main ()

{

  printf ("%s", _TEXT(__FILE__ ));  // Works fine

  printf ("%s", _T(__FILE__));      // Works fine

  printf ("%s", __TEXT(__FILE__ )); // error C2065: 'L__FILE__': undeclared identifier

  _getwch();

}

更新: 我认为我的代码与 C 预处理器标记化有关。我为此发布了一个单独的问题。谢谢。

最佳答案

因为“神秘”事物经常出现这种情况,Raymond Chen gives some information (强调):

So what's with all these different ways of saying the same thing? There's actually a method behind the madness.

The plain versions without the underscore affect the character set the Windows header files treat as default. So if you define UNICODE, then GetWindowText will map to GetWindowTextW instead of GetWindowTextA, for example. Similarly, the TEXT macro will map to L"..." instead of "...".

The versions with the underscore affect the character set the C runtime header files treat as default. So if you define _UNICODE, then _tcslen will map to wcslen instead of strlen, for example. Similarly, the _TEXT macro will map to L"..." instead of "...". What about _T? Okay, I don't know about that one. Maybe it was just to save somebody some typing.

关于c++ - 为什么在 win32 中有不同的 TEXT like macros for same thing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8314552/

相关文章:

c++ - 在打印类似于 "%q"的字符时如何避免在 vsprintf_s() 崩溃

python - 在 Windows 中终止子进程,访问被拒绝

c - 滚动条范围的值是多少?

c# - 使用连接到 LDAP 服务器的生物识别系统验证窗口用户

c++ - 如何将从模板参数派生的编译时信息添加到模板?

android - 找不到 JNI 方法

c++ - 还在获取重复值?

c# - 有没有办法将任意数据关联到 Windows 进程?

c# - 广播 Windows HWND_BROADCAST 消息

python - 如何使用 hinstance 确定 win32api.ShellExecute 是否成功?