Try to match the behavior of `comsuppw.lib` regarding the implementation of `ConvertBSTRToString`:
When the memory allocation fails, it will call `_com_issue_error` with `E_OUTOFMEMORY` and the handler `_com_raise_error` will raise an exception `_com_error` with the error code `E_OUTOFMEMORY`. It seems that `ConvertBSTRToString` calls `operator new[](size_t)` to allocate memory rather than calling the nothrow one. Thus, we need to handle the exception in `ConvertBSTRToString` and call `_com_issue_error` with `E_OUTOFMEMORY` to match the behavior of `comsuppw.lib`. Signed-off-by: Yan-Jie Wang <[email protected]> --- mingw-w64-headers/include/comutil.h | 52 +++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/mingw-w64-headers/include/comutil.h b/mingw-w64-headers/include/comutil.h index ddb090637..82d81e6cd 100644 --- a/mingw-w64-headers/include/comutil.h +++ b/mingw-w64-headers/include/comutil.h @@ -51,8 +51,56 @@ namespace _com_util { } namespace _com_util { - BSTR WINAPI ConvertStringToBSTR(const char *pSrc); - char *WINAPI ConvertBSTRToString(BSTR pSrc); + inline BSTR WINAPI ConvertStringToBSTR(const char *pSrc){ + int wcSize; + BSTR bstr; + if(!pSrc) return NULL; + wcSize=::MultiByteToWideChar(CP_ACP,0,pSrc,-1,NULL,0); + if (wcSize==0) { + _com_issue_error(HRESULT_FROM_WIN32(GetLastError())); + return NULL; + } + bstr=::SysAllocStringLen(NULL,wcSize-1); + if(!bstr) { + _com_issue_error(E_OUTOFMEMORY); + return NULL; + } + if(::MultiByteToWideChar(CP_ACP,0,pSrc,-1,bstr,wcSize)==0) { + ::SysFreeString(bstr); + _com_issue_error(HRESULT_FROM_WIN32(::GetLastError())); + return NULL; + } + return bstr; + } + inline char *WINAPI ConvertBSTRToString(BSTR pSrc){ + int mbSize; + char *str; + if(!pSrc) return NULL; + mbSize = ::WideCharToMultiByte(CP_ACP,0,pSrc,-1,NULL,0,NULL,NULL); + if (mbSize==0) { + _com_issue_error(HRESULT_FROM_WIN32(::GetLastError())); + return NULL; + } +#if __EXCEPTIONS + try { +#endif + str=new char[mbSize]; +#if __EXCEPTIONS + } catch (...) { + str=NULL; + } +#endif + if(!str) { + _com_issue_error(E_OUTOFMEMORY); + return NULL; + } + if(::WideCharToMultiByte(CP_ACP,0,pSrc,-1,str,mbSize,NULL,NULL)==0) { + delete[] str; + _com_issue_error(HRESULT_FROM_WIN32(::GetLastError())); + return NULL; + } + return str; + } } class _bstr_t { -- 2.49.0.windows.1 _______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
