How to best convert CString to BSTR to pass it as an “in” parameter into a COM method?
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