Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How many string types do you guys need?

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
US
What's with LPWSTR, LPCSTR, LPCTSTR etc?

I want a lousy string...

Actually, in this particular instance I want

Code:
ShellExecute(NULL, "open", "C:\\boo\\foo\\yar.exe", NULL, NULL, SW_SHOWNORMAL);

But it says open needs to be a LPWSTR, and the executable path needs to be something else... and that's fine, I can cast them easily, but when I'm looking at example code online no one is doing that, so I was wondering, how are they doing it?
 
LPWSTR = WCHAR*
LPCSTR = CONST CHAR*
LPCTSTR = CONST TCHAR*

as you see, they are all pointers to char type (except that WCHAR is 16bit, CHAR is 8bit and TCHAR is either 8bit or 16bit, depending on _UNICODE define.

"open" is 5 8bit characters, so if you're writing a program that uses unicode (16bit) API, you should either use
Code:
L"open"
which is a unicode (16bit) string or
Code:
_T("open")
which is 8bit or 16bit string depending on _UNICODE define presence.


------------------
When you do it, do it right.
 
BSTR is mostly for COM usage.
Btw, BSTR = WCHAR*
and it seems it needs additional allocation and deallocation to be used.

------------------
When you do it, do it right.
 
I've mostly wedged things into my current project using TEXT()... which I believe is equivalent to L"", but I'm not sure.

saying "open" is a char[4], but still not a LPCSTR because it's not the pointer... right?

What are the people writing examples doing that allows them to not do any of that?
 
In my version of winnt.h, TEXT(str) is basically just a macro for L"str".
 
Btw, BSTR = WCHAR*

BSTR!=WCHAR* :


#if defined(_WIN32) && !defined(OLE2ANSI)
typedef WCHAR OLECHAR;
#else
typedef char OLECHAR;
#endif

typedef OLECHAR* BSTR;


So BSTR is either WCHAR* or char*. Furthermore when used for what it is actually to be used for (COM strings), BSTR!=OLECHAR*, since the BSTR is not necessarily NULL terminated, can contain embedded NULLs and is being preceded by a 4-byte length descripter.

Greetings,
Rick
 
What are the people writing examples doing that allows them to not do any of that?

They just use a non-Unicode API. Almost all WinAPI functions that have string parameters have 2 versions - one for 8bit strings, and another for 16bit strings (Unicode).

In my version of winnt.h, TEXT(str) is basically just a macro for L"str"

perform Find Next. TEXT() is same to _T(). The difference is that TEXT() requires UNICODE define, and _T() requires _UNICODE define (note the underscore).

saying "open" is a char[4], but still not a LPCSTR because it's not the pointer... right?

first, "open" is char[5] (it holds terminating null too). LPCSTR is const CHAR*, so it is equivalent to char[5]. Although, you specified that the compiler needed LPWSTR, which is WCHAR*, so you need L"open". If you use TEXT("open") or _T("open"), make sure you have defined either UNICODE or _UNICODE accordingly.

------------------
When you do it, do it right.
 
dEVooXiAm said:
perform Find Next. TEXT() is same to _T().
I didn't even find any reference to _T in winnt.h
My TEXT macro is defined like this:
Code:
#ifdef  UNICODE                     // r_winnt
...
#  define __TEXT(quote) L##quote    // r_winnt
#else   /* UNICODE */               // r_winnt
...
#  define __TEXT(quote) quote       // r_winnt
#endif /* UNICODE */                // r_winnt
#define TEXT(quote) __TEXT(quote)   // r_winnt
 
_T macro from <tchar.h>:
MSDN said:
Use the _T macro to code literal strings generically, so they compile as Unicode strings under Unicode or as ANSI strings (including MBCS) without Unicode.
...
Tip The _T macro is identical to the _TEXT macro.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top