Reputation: 67
How to convert LPWSTR into BSTR ? LPWSTR is WCHAR * and BSTR is OLECHAR *, OLECHAR is again of type WCHAR, simply type casting will work ? or we need to do some extra effort ? It is showing bad conversion error/warning.
Upvotes: 4
Views: 947
Reputation: 13690
The BSTR consists of a OLECHAR[] and a length information prepended to the string. The BSTR is a pointer to the OLECHAR[] part. But the BSTR functions require the length information (capacity and curent length).
You need the SysAllocString function to allocate the whole structure.
Upvotes: 1
Reputation: 26279
You do it like this
BSTR pBstr = SysAllocString(szWCharString);
// do something with the BSTR here ...
SysFreeString(pBstr);
You have to remember to deallocate the space used for the conversion by calling SysFreeString
afterwards.
Upvotes: 3