I need to convert a CString
instance into a properly allocated BSTR
and pass that BSTR
into a COM method. To have code that compiles and works indentically for both ANSI and Unicode I use CString::AllocSysString()
to convert whatever format CString
to a Unicode BSTR.
Since noone owns the returned BSTR I need to take care of it and release it after the call is done in the most exception-safe manner posible and with as little code as possible.
Currently I use ATL::CComBSTR
for lifetime management:
ATL::CComBSTR converted;
converted.Attach( sourceString.AllocSysString() ); //simply attaches to BSTR, doesn't reallocate it
interface->CallMethod( converted );
what I don't like here is that I need two separate statements to just construct the ATL::CComBSTR
bound to the convertion result.
Is there a better way to accomplish the same task?