I\'m trying to convert a wchar_t * to BSTR.
#include
#include
using namespace std;
int main()
You need to use SysAllocString (and then SysFreeString).
BSTR bstr = SysAllocString(pwsz);
// ...
SysFreeString(bstr);
A BSTR is a managed string with the characters of the string prefixed by their length. SysAllocString allocates the correct amount of storage and set up the length and contents of the string correctly. With the BSTR correctly initialized, SysStringLen should return the correct length.
If you're using C++ you might want to consider using a RAII style class (or even Microsoft's _bstr_t) to ensure that you don't forget any SysFreeString calls.