Question

Any ideas on how to make a BSTR out of an LPCOLESTR? Silly thing to get hung up on..

Was it helpful?

Solution

An LPCOLESTR is just a const wchar_t*, so you can use SysAllocString() to create a BSTR:

LPCOLESTR olestr = ...;
BSTR bstr = SysAllocString(olestr);

Be sure to call SysFreeString() when you're done with your BSTR. See also the MSDN documentation on BSTRs

OTHER TIPS

The difference between BSTR and LPCOLESTR is that BSTR has the length of the string prefixed before the string, LPCOLESTR hasn't.

A BSTR doesn't necessarily have an ending \0 marking end of string, since the length of the string is prefixed, to convert I usually use the class CComBSTR (atlcomcli.h), the ctor takes either BSTR or LPCOLESTR as argument and there is a member BSTR() to get the BSTR representation:

CComBSTR b( yourolestring )
// b.BSTR()

CComBSTR will handle the allocating/freeing so no risk of memory leak.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top