Align behavior of ConvertBSTRToString with comsuppw.lib. On allocation failure, call _com_issue_error(E_OUTOFMEMORY). If the default handler _com_raise_error() is used, it raises a _com_error exception.
Signed-off-by: Yan-Jie Wang <[email protected]> --- This patch is identical to v3, except for the updated commit message. Please review and let me know if any further changes are needed. 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
