我想知道为什么会出现 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/