There is a class-based ATL :: CA2W and friends (in atlconv.h, I reckon) that do not push a line onto the stack and do not use macros. You do not need USES_CONVERSION in a function:
throw x::internal_error(ATL::CA2W(pszErrStr));
Also, since your arguments are BSTR (wchar_t *), you do not need to convert them to _bstr_t.
NOTE. The converted string lifetime is the lifetime of the CW2A object, so you will need to put it in a string class, for example:
CStringA arg = CW2A(bstrArg);
NOTE 2: pbstrValue is the output value. The _bstr_t instance will destroy the memory allocated for the BSTR. Therefore, you need to either directly use SysAllocString or disconnect the BSTR:
pbstrValue = SysAllocString(CA2W(retval));
or
pbstrValue = CComBSTR(CA2W(retval)).Detach();
NOTE 3. Explicit use of conversion operators ( .operator BSTR() ) is not required - the compiler will call the correct one for you.
NOTE 4: Since this looks like a COM call, you really don't want to throw a C ++ exception. You probably want to set an IErrorInfo object (perhaps with a helper):
if (!bResult) { SetError(CA2W(errorStr)); return E_FAIL; }
source share