![]()
BSTR bstrMyBeaster = SysAllocString (L"Tring, Tring!");
WCHAR* pwszMyWCharString = L"Tring, Tring!";
USES_CONVERSION;
LPSTR pszCharStringFromBSTR = OLE2A (bstrMyBeaster);
LPSTR pszCharStringFromLPWSTR = W2A (pwszMyWCharString);
// ...
SysFreeString (bstrMyBeaster);
I think "USES_CONVERSION;" is very convenient and easy to convert between CComBSTR and Char. But this article also tell us it may happen "Stack overflow" when your source BSTR is over 1MB. The root cause is because use "_alloca".
I think to avoid stack overflow like this, just use static conversion not "USE_CONVERSION;".
Hi Evan,
Thank you for reading my article.
Just dropped in to let you know that stack overflows caused by USES_CONVERSION macros are not only applicable when the string is over 1 MB, but, even for smaller strings when used in loops - as the memory allocated on the stack accumulates loop-after-loop, and finally flows over.
Avoid this by using WideCharToMultiByte.
An easy option available to those developing on VC++ 7.0 is conversion class CW2A.
Sample to using the same is also in my FAQ:
http://www.codeguru.com/forum/showthread.php?t=337247
Best Regards,
Siddhartha
====
Siddhartha Rao
Microsoft MVP - Visual C++
Codeguru Moderator