Signed-off-by: Yan-Jie Wang <[email protected]>
---
 mingw-w64-headers/include/comutil.h | 55 +++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 2 deletions(-)

diff --git a/mingw-w64-headers/include/comutil.h 
b/mingw-w64-headers/include/comutil.h
index ddb090637..dacbf154e 100644
--- a/mingw-w64-headers/include/comutil.h
+++ b/mingw-w64-headers/include/comutil.h
@@ -6,6 +6,9 @@
 #ifndef _INC_COMUTIL
 #define _INC_COMUTIL
 
+#include <winerror.h>
+#include <stringapiset.h>
+#include <errhandlingapi.h>
 #include <ole2.h>
 #include <stdio.h>
 
@@ -51,8 +54,56 @@ namespace _com_util {
 }
 
 namespace _com_util {
-  BSTR WINAPI ConvertStringToBSTR(const char *pSrc);
-  char *WINAPI ConvertBSTRToString(BSTR pSrc);
+  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;
+  }
+  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

Reply via email to