C++ LPCSTR、LPCTSTR 和 LPTSTR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/321413/
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-08-27 14:37:50  来源:igfitidea点击:

LPCSTR, LPCTSTR and LPTSTR

c++windowsvisual-c++mfc

提问by nothingMaster

What the difference between LPCSTR, LPCTSTRand LPTSTR?

什么区别LPCSTRLPCTSTRLPTSTR

Why do we need to do this to convert a string into a LV/ _ITEMstructure variable pszText:

为什么我们需要这样做才能将字符串转换为LV/_ITEM结构变量pszText

LV_DISPINFO dispinfo;  
dispinfo.item.pszText = LPTSTR((LPCTSTR)string);

回答by John Sibly

To answer the first part of your question:

要回答问题的第一部分:

LPCSTRis a pointer to a const string (LP means Long Pointer)

LPCSTR是一个指向 const 字符串的指针(LP 表示Long Pointer

LPCTSTRis a pointer to a const TCHARstring, (TCHARbeing either a wide char or char depending on whether UNICODE is defined in your project)

LPCTSTR是指向const TCHAR字符串的指针,(TCHAR根据您的项目中是否定义了 UNICODE,可以是宽字符或字符)

LPTSTRis a pointer to a (non-const) TCHARstring

LPTSTR是指向(非常量)TCHAR字符串的指针

In practice when talking about these in the past, we've left out the "pointer to a" phrase for simplicity, but as mentioned by lightness-races-in-orbit they are all pointers.

在实践中,过去在谈论这些时,为了简单起见,我们省略了“指向 a 的指针”短语,但正如 lightness-races-in-orbit 所提到的,它们都是指针。

This is a great codeproject articledescribing C++ strings (see 2/3 the way down for a chart comparing the different types)

这是一篇很好的代码项目文章,描述了 C++ 字符串(参见 2/3 向下的图表比较不同类型)

回答by Tim

Quick and dirty:

又快又脏:

LP== Long Pointer. Just think pointer or char*

LP==大号Pointer。想想指针或字符*

C= Const, in this case, I think they mean the character string is a const, not the pointer being const.

C= Const,在这种情况下,我认为他们的意思是字符串是一个常量,而不是指针是常量。

STRis string

STR字符串

the Tis for a wide character or char (TCHAR) depending on compile options.

T是用于根据编译选项宽字符或字符(TCHAR)。

回答by Ian Boyd

8-bit AnsiStrings

8 位 AnsiStrings

  • char: 8-bit character - underlying C/C++ data type
  • CHAR: alias of char- Windows data type
  • LPSTR: null-terminated string of CHAR(Long Pointer)
  • LPCSTR: constant null-terminated string of CHAR(Long Pointer)
  • char: 8 位字符 - 底层 C/C++ 数据类型
  • CHAR: 别名char- Windows 数据类型
  • LPSTR:的空终止字符串CHAR大号Pointer)
  • LPCSTR:恒定空终止字符串CHAR大号Pointer)

16-bit UnicodeStrings

16 位 UnicodeStrings

  • wchar_t: 16-bit character - underlying C/C++ data type
  • WCHAR: alias of wchar_t- Windows data type
  • LPWSTR: null-terminated string of WCHAR(Long Pointer)
  • LPCWSTR: constant null-terminated string of WCHAR(Long Pointer)
  • wchar_t: 16 位字符 - 底层 C/C++ 数据类型
  • WCHAR: 别名wchar_t- Windows 数据类型
  • LPWSTR:的空终止字符串WCHAR大号Pointer)
  • LPCWSTR:恒定空终止字符串WCHAR大号Pointer)

depending on UNICODEdefine

取决于UNICODE定义

  • TCHAR: alias of WCHARif UNICODE is defined; otherwise CHAR
  • LPTSTR: null-terminated string of TCHAR(Long Pointer)
  • LPCTSTR: constant null-terminated string of TCHAR(Long Pointer)
  • TCHAR:WCHAR如果定义了 UNICODE 的别名;除此以外CHAR
  • LPTSTR:的空终止字符串TCHAR大号Pointer)
  • LPCTSTR:恒定空终止字符串TCHAR大号Pointer)

So

所以

| Item              | 8-bit        | 16-bit      | Varies          |
|-------------------|--------------|-------------|-----------------|
| character         | CHAR         | WCHAR       | TCHAR           |
| string            | LPSTR        | LPWSTR      | LPTSTR          |
| string (const)    | LPCSTR       | LPCWSTR     | LPCTSTR         |

Bonus Reading

奖励阅读

TCHARText Char(archive.is)

TCHAR文本字符( archive.is)

回答by JaredPar

Adding to John and Tim's answer.

添加到约翰和蒂姆的答案。

Unless you are coding for Win98, there are only two of the 6+ string types you should be using in your application

除非您为 Win98 编码,否则您应该在应用程序中使用的 6 种以上字符串类型中只有两种

  • LPWSTR
  • LPCWSTR
  • LPWSTR
  • LPCWSTR

The rest are meant to support ANSI platforms or dual compilations. Those are not as relevant today as they used to be.

其余的旨在支持 ANSI 平台或双重编译。这些在今天不像过去那样重要。

回答by AAT

To answer the second part of your question, you need to do things like

要回答问题的第二部分,您需要执行以下操作

LV_DISPINFO dispinfo;  
dispinfo.item.pszText = LPTSTR((LPCTSTR)string);

because MS's LVITEMstruct has an LPTSTR, i.e. a mutableT-string pointer, not an LPCTSTR. What you are doing is

因为 MS 的LVITEMstruct 有一个LPTSTR,即一个可变的T 字符串指针,而不是一个LPCTSTR。你正在做的是

1) convert string(a CStringat a guess) into an LPCTSTR(which in practise means getting the address of its character buffer as a read-only pointer)

1) 将string(a CStringat a guess) 转换为 an LPCTSTR(实际上意味着将其字符缓冲区的地址作为只读指针获取)

2) convert that read-only pointer into a writeable pointer by casting away its const-ness.

2) 通过丢弃它的const-ness将该只读指针转换为可写指针。

It depends what dispinfois used for whether or not there is a chance that your ListViewcall will end up trying to writethrough that pszText. If it does, this is a potentially very bad thing: after all you were given a read-only pointer and then decided to treat it as writeable: maybe there is a reason it was read-only!

这取决于dispinfo您的ListView电话是否有可能最终尝试通过该内容写入内容pszText。如果是这样,这可能是一件非常糟糕的事情:毕竟你得到了一个只读指针,然后决定把它当作可写的:也许它是只读的!

If it is a CStringyou are working with you have the option to use string.GetBuffer()-- that deliberately gives you a writeable LPTSTR. You then have to remember to call ReleaseBuffer()if the string does get changed. Or you can allocate a local temporary buffer and copy the string into there.

如果它是CString你与你的工作需要使用的选项string.GetBuffer()-故意给你写LPTSTR。然后,您必须记住ReleaseBuffer()在字符串确实发生更改时调用。或者您可以分配一个本地临时缓冲区并将字符串复制到那里。

99% of the time this will be unnecessary and treating the LPCTSTRas an LPTSTRwill work... but one day, when you least expect it...

在 99% 的情况下,这将是不必要的,并且将其LPCTSTR视为LPTSTR可行的……但是有一天,当您最不期望它时……