tags:

views:

307

answers:

3

What's the meaning of BSTR, LPCOLESTR, LPCWSTR, LPTSTR, LPCWCHAR, and many others if they're all just a bunch of defines that resolve to wchar_t anyway?

+2  A: 

LPTSTR and LPWSTR and similar defines are really just defines. BSTR and LPOLESTR have special meanings - they indicate the the string pointed to is allocated in a special way.

String pointed to by BSTR must be allocated with SysAllocString() family functions. String pointed to by LPOLESTR is usually to be allocated with CoTaskMemAlloc() (this should be looked up in the documentation to the COM call accepting/returning it).

If allocation/deallocation requirements for strings pointed to by BSTR and LPOLESTR are violated the program can run into undefined behaviour.

sharptooth
Only those two are special?
CannibalSmith
As I can remember, yes.
sharptooth
A: 

MSDN's page on Windows Data Types might provide clarification as to the differences between some of these data types.

LPCWSTR - Pointer to a constant null-terminated string of 16-bit Unicode characters.

LPTSTR - An LPWSTR if UNICODE is defined, an LPSTR otherwise.

Donut
+1  A: 
  • LPTSTR indicates the string buffer can be ANSI or UNICODE depending on the definition of the macro: UNICODE.
  • LPCOLESTR was invented by the OLE team because it switches its behaviour between char and wchar_t based on the definition of OLE2ANSI
  • LPCWSTR is a wchar_t string
  • BSTR is an LPOLESTR thats been allocated with SysAllocString.
  • LPCWCHAR is a pointer to a single constant wide character.

They're actually all rather different. Or at least, were at some time different. Ole was developed - and needed - wide strings while the windows API was still Win16 and didnt support wide strings natively at all.

Also, early versions of the Windows SDK didnt use wchar_t for WCHAR but unsigned short. The windows SDK on GCC gets interesting as - im led to belive that GCC 32bit has a 32bit wchar_t - on compilers with 32bit wchar_t, WCHAR would be defined as an unsigned short or some other type thats 16bits on that compiler.

Chris Becke