Windows C++:LPCTSTR 与 const TCHAR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2991406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:36:36  来源:igfitidea点击:

Windows C++: LPCTSTR vs const TCHAR

c++windowsstringwinapitchar

提问by user353297

In my application i'm declaring a string variable near the top of my code to define the name of my window class which I use in my calls to RegisterClassEx, CreateWindowEx etc.. Now, I know that an LPCTSTR is a typedef and will eventually follow down to a TCHAR (well a CHAR or WCHAR depending on whether UNICODE is defined), but I was wondering whether it would be better to use this:

在我的应用程序中,我在代码顶部附近声明了一个字符串变量来定义我在调用 RegisterClassEx、CreateWindowEx 等时使用的窗口类的名称。现在,我知道 LPCTSTR 是一个 typedef 并且最终会遵循 TCHAR(以及 CHAR 或 WCHAR,具体取决于是否定义了 UNICODE),但我想知道使用它是否更好:

static LPCTSTR szWindowClass = TEXT("MyApp");

Or this:

或这个:

static const TCHAR szWindowClass[] = TEXT("MyApp");

I personally prefer the use of the LPCTSTR as coming from a JavaScript, PHP, C# background I never really considered declaring a string as an array of chars.

我个人更喜欢使用 LPCTSTR,因为它来自 JavaScript、PHP、C# 背景,我从未真正考虑过将字符串声明为字符数组。

But are there actually any advantages of using one over the other, or does it in fact not even make a difference as to which one I choose?

但实际上使用一种比另一种有什么优势吗,或者实际上对于我选择哪一种没有任何影响?

Thank you, in advanced, for your answers.

先谢谢你的回答。

回答by Mark Ransom

The two declarations are not identical. The first creates a pointer, the second an array of TCHAR. The difference might not be apparent, because an array will decompose into a pointer if you try to use it, but you'll notice it instantly if you try to put them into a structure for example.

这两个声明并不相同。第一个创建一个指针,第二个创建一个 TCHAR 数组。差异可能并不明显,因为如果您尝试使用数组,它会分解为一个指针,但例如,如果您尝试将它们放入一个结构中,您会立即注意到它。

The equivalent declaration to LPCTSTR is:

LPCTSTR 的等效声明是:

static const TCHAR * szWindowClass = TEXT("MyApp");

The "L" in LPCTSTR stands for "Long", which hasn't been relevant since 16-bit Windows programming and can be ignored.

LPCTSTR 中的“L”代表“Long”,自 16 位 Windows 编程以来就不再相关,可以忽略。

回答by Hernán

Since Unicode strings are native from Windows NT, unless you want your application to run on ANSI-native Windows 9x, always use wide-character strings (WCHARor wchar_ttypes).

由于 Unicode 字符串是 Windows NT 本机的,除非您希望您的应用程序在 ANSI 本机 Windows 9x 上运行,否则请始终使用宽字符串(WCHARwchar_t类型)。

Relative to your question, both forms may seemequal; but altough both are expected to be allocated in the constant string section of your executable, string literals are not necessarily modifiablewhen used as array initializers.

相对于你的问题,这两种形式似乎是平等的;但是尽管两者都应该在可执行文件的常量字符串部分中分配,但是当用作数组初始值设定项时,字符串文字不一定是可修改的

e.g: (from C Faq, 16.6) The following code can crash:

例如:(来自 C Faq,16.6)以下代码可能会崩溃:

char *p = "HELLO";
p[0] = 'H';

So it's better always to use:

所以最好总是使用:

char a[] = "HELLO";

Again, from C Faq:A string literal can be used in two slightly different ways. As an array initializer (as in the declaration of char a[]), it specifies the initial values of the characters in that array. Anywhere else, it turns into an unnamed, static array of characters, which may be stored in read-only memory, which is why you can't safely modify it. In an expression context, the array is converted at once to a pointer, as usual (see section 6), so the second declaration initializes p to point to the unnamed array's first element.

同样,来自 C Faq:字符串文字可以以两种稍微不同的方式使用。作为数组初始值设定项(如在 char a[] 的声明中),它指定该数组中字符的初始值。在其他任何地方,它都会变成一个未命名的静态字符数组,它可能存储在只读内存中,这就是您无法安全修改它的原因。在表达式上下文中,数组会像往常一样立即转换为指针(参见第 6 节),因此第二个声明将 p 初始化为指向未命名数组的第一个元素。

http://linuxdude.com/Steve_Sumit/C-faq/q1.32.html

http://linuxdude.com/Steve_Sumit/C-faq/q1.32.html

回答by Hernán

The array form is preferable for string literals of this type. The data and code involved takes up (very, very marginally) less space, and the variable can't be changed to point at a different string. (In fact, there's no variable at all, just a name for the address of the first char in the string -- it behaves very much like a literal value in this respect.)

数组形式更适合这种类型的字符串文字。所涉及的数据和代码占用的空间(非常非常少),并且变量不能更改为指向不同的字符串。(实际上,根本没有变量,只是字符串中第一个字符的地址的名称——在这方面它的行为非常类似于文字值。)