Convert or cast LPWST to BSTR

How to convert LPWSTR to BSTR? LPWSTR is WCHAR *, and BSTR is OLECHAR *, OLECHAR is again of the WCHAR type, just gaining a listing? or do we need to make some extra effort? It shows the wrong error / warning error.

+4
source share
2 answers

You do it like this:

BSTR pBstr = SysAllocString(szWCharString); // do something with the BSTR here ... SysFreeString(pBstr); 

You must remember to free up the space used for the conversion by calling SysFreeString afterwards.

+2
source

BSTR consists of OLECHAR [] and the length information preceding the line. BSTR is a pointer to a part of OLECHAR []. But BSTR functions require length information (capacity and length).

You need the SysAllocString function to highlight the entire structure.

+1
source

Source: https://habr.com/ru/post/1483284/


All Articles