An LPCSTR is a 32-bit pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters.
You can get the length of a string e.g. with the oldschool C-style function strlen. LPCTSTR s = "foobar"; std::cout << "Length of s is " << strlen(s); Or since you are apparently working with MFC, you could as well instantiate a CString object by the LPCTSTR variable and get the length from the CString.
Solution 4. CString p; m_editbox->GetWindowText(p); CWND *c = FindWindow(NULL,p); Also the title is no lucky choice, because I think the problem is not the conversion from CString to LPCTSTR . The call to FindWindow() is correct, when it is inside a CWnd derived class.
An LPCWSTR is a 32-bit pointer to a constant string of 16-bit Unicode characters, which MAY be null-terminated.
To answer the first part of your question:
LPCSTR
is a pointer to a const string (LP means Long Pointer)
LPCTSTR
is a pointer to a const TCHAR
string, (TCHAR
being either a wide char or char depending on whether UNICODE is defined in your project)
LPTSTR
is a pointer to a (non-const) TCHAR
string
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.
This is a great codeproject article describing C++ strings (see 2/3 the way down for a chart comparing the different types)
Quick and dirty:
LP
== Long Pointer. Just think pointer or char*
C
= Const, in this case, I think they mean the character string is a const, not the pointer being const.
STR
is string
the T
is for a wide character or char (TCHAR) depending on compiler options.
From What does the letter "T" in LPTSTR stand for?: archive
What does the letter "T" in LPTSTR stand for?
October 17th, 2006
The “T” in LPTSTR comes from the “T” in TCHAR. I don’t know for certain, but it seems pretty likely that it stands for “text”. By comparison, the “W” in WCHAR probably comes from the C language standard, where it stands for “wide”.
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 Constant)
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 Constant)
UNICODE
defineTCHAR
: alias of WCHAR
if UNICODE is defined; otherwise CHAR
LPTSTR
: null-terminated string of TCHAR
(Long Pointer)
LPCTSTR
: constant null-terminated string of TCHAR
(Long Pointer Constant)
So:
Item | 8-bit (Ansi) | 16-bit (Wide) | Varies |
---|---|---|---|
character | CHAR |
WCHAR |
TCHAR |
string | LPSTR |
LPWSTR |
LPTSTR |
string (const) | LPCSTR |
LPCWSTR |
LPCTSTR |
TCHAR
→ Text Char (archive.is)
Why is the default 8-bit codepage called "ANSI"?
From Unicode and Windows XP
by Cathy Wissink
Program Manager, Windows Globalization
Microsoft Corporation
May 2002
Despite the underlying Unicode support on Windows NT 3.1, code page support continued to be necessary for many of the higher-level applications and components included in the system, explaining the pervasive use of the “A” [ANSI] versions of the Win32 APIs rather than the “W” [“wide” or Unicode] versions. (The term “ANSI” as used to signify Windows code pages is a historical reference, but is nowadays a misnomer that continues to persist in the Windows community. The source of this comes from the fact that the Windows code page 1252 was originally based on an ANSI draft, which became ISO Standard 8859-1. However, in adding code points to the range reserved for control codes in the ISO standard, the Windows code page 1252 and subsequent Windows code pages originally based on the ISO 8859-x series deviated from ISO. To this day, it is not uncommon to have the development community, both within and outside of Microsoft, confuse the 8859-1 code page with Windows 1252, as well as see “ANSI” or “A” used to signify Windows code page support.)
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
LPWSTR
LPCWSTR
The rest are meant to support ANSI platforms or dual compilations. Those are not as relevant today as they used to be.
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 LVITEM
struct has an LPTSTR
, i.e. a mutable T-string pointer, not an LPCTSTR
. What you are doing is
1) convert string
(a CString
at a guess) into an LPCTSTR
(which in practise means getting the address of its character buffer as a read-only pointer)
2) convert that read-only pointer into a writeable pointer by casting away its const
-ness.
It depends what dispinfo
is used for whether or not there is a chance that your ListView
call will end up trying to write through 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!
If it is a CString
you 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.
99% of the time this will be unnecessary and treating the LPCTSTR
as an LPTSTR
will work... but one day, when you least expect it...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With