The implementation of `ConvertBSTRToString` is aligned with the behavoir of comsuppw.lib provided by MSVC. When 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`.
Signed-off-by: Yan-Jie Wang <[email protected]> --- Please review this patch. The header <new> need to be included for `std::nothrow`. Please confirm whether it is acceptable to include <new> in `comutil.h`. It seems that `comutil.h` in MSVC does not include <new>. mingw-w64-headers/include/comutil.h | 45 +++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/mingw-w64-headers/include/comutil.h b/mingw-w64-headers/include/comutil.h index ddb090637..df20f9818 100644 --- a/mingw-w64-headers/include/comutil.h +++ b/mingw-w64-headers/include/comutil.h @@ -8,6 +8,7 @@ #include <ole2.h> #include <stdio.h> +#include <new> #ifndef _COM_ASSERT #define _COM_ASSERT(x) ((void)0) @@ -51,8 +52,48 @@ 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; + } + str=new(::std::nothrow) char[mbSize]; + 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
