Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of BSTR, LPCOLESTR, and others?

Tags:

winapi

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?

like image 676
CannibalSmith Avatar asked Oct 22 '09 13:10

CannibalSmith


2 Answers

  • 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.

like image 148
Chris Becke Avatar answered Oct 22 '22 20:10

Chris Becke


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.

like image 41
sharptooth Avatar answered Oct 22 '22 19:10

sharptooth