LPCSTR, LPCTSTR and LPTSTR

What is the difference between LPCSTR , LPCTSTR and LPTSTR ?

Why do we need this to convert the string to the LV / _ITEM pszText structure variable:

 LV_DISPINFO dispinfo; dispinfo.item.pszText = LPTSTR((LPCTSTR)string); 
+86
c ++ windows visual-c ++ mfc
Nov 26 '08 at 17:01
source share
5 answers

To answer the first part of your question:

LPCSTR is a string of const

LPCTSTR is the string const TCHAR , ( TCHAR is either wide char or char depending on whether UNICODE is defined in your project)

LPTSTR - string (not const) TCHAR

This is a great article in a code article describing C ++ strings (see 2/3 the way down for a chart comparing different types)

+99
Nov 26 '08 at 17:09
source share
β€” -

Quick and dirty:

LP == L ong P ointer. Just think pointer or character *

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

STR is a string

T for a wide character or character (TCHAR) depending on the compilation options.

+75
Nov 26 '08 at 17:09
source share

8-bit AnsiStrings

  • char : 8-bit character - C / C ++ base data type
  • CHAR : char alias - Windows data type
  • LPSTR : CHAR string with a null character in CHAR ( L ong P ointer)
  • LPCSTR : constant string CHAR null character in CHAR ( L ong P ointer)

16-bit UnicodeStrings

  • wchar_t : 16-bit character - C / C ++ base data type
  • WCHAR : alias wchar_t - Windows data type
  • LPWSTR : string LPWSTR null character in WCHAR ( L ong P ointer)
  • LPCWSTR : constant string WCHAR null character in WCHAR ( L ong P ointer)

depending on UNICODE define

  • TCHAR : alias WCHAR if UNICODE is defined; otherwise CHAR
  • LPTSTR : string LPTSTR null character in TCHAR ( L ong P ointer)
  • LPCTSTR : constant string with TCHAR character in TCHAR ( L ong P ointer)

So

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

Bonus Reading

TCHAR β†’ Text Char ( archive.is )

+15
Sep 27 '17 at 21:00
source share

Adding an answer to John and Tim.

If you do not encode Win98, only two of the six string types should be in your application.

  • LPWSTR
  • LPCWSTR

The rest are designed to support ANSI or dual compilation platforms. Today they are not as relevant as before.

+3
Nov 26 '08 at 17:12
source share

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

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

since MS LVITEM struct has LPTSTR , i.e. mutable T-string pointer, not LPCTSTR . What you do is

1) convert string (a CString when guessing) to LPCTSTR (which in practice means getting the address of its character buffer as a read-only pointer)

2) convert this read-only pointer to a writable pointer, discarding its const -ness.

It depends on what dispinfo used to see if it is likely that your ListView call will end up trying to write via pszText . If so, this is very dangerous: in the end, you got a read-only pointer, and then decided to treat it as writable: there might be a reason it was read-only!

If this is the CString you are working with, you have the option to use string.GetBuffer() - this intentionally gives you the option to write LPTSTR . Then you need to remember the call to ReleaseBuffer() if the string is changed. Or you can allocate a local temporary buffer and copy the line there.

In 99% of cases this will be superfluous, and processing LPCTSTR as LPTSTR will work ... but one day, when you least expect it ...

+3
Sep 05 2018-11-11T00:
source share



All Articles