Signed-off-by: Yan-Jie Wang <[email protected]>
---
Changes from v1:
- Added missing inline keyword.
- Remove unnecessary includes.
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..076bd90d7 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=(char *)::operator new[](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) {
+ ::operator 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