ReactOS  0.4.14-dev-552-g2fad488
comsupp.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS crt library
3  * LICENSE: GPLv2+ - See COPYING in the top level directory
4  * PURPOSE: Compiler support for COM
5  * PROGRAMMER: Thomas Faber (thomas.faber@reactos.org)
6  */
7 
8 #include <stdarg.h>
9 
10 #define WIN32_NO_STATUS
11 #define _INC_WINDOWS
12 
13 #include <windef.h>
14 #include <winbase.h>
15 #include <comdef.h>
16 
17 /* comdef.h */
20 
22 {
23  throw _com_error(hr, perrinfo);
24 }
25 
27 {
28  com_error_handler = phandler;
29 }
30 
32 {
34 }
35 
37 {
38  void *pv;
39  IErrorInfo *perrinfo = NULL;
40 
41  if (SUCCEEDED(punk->QueryInterface(riid, &pv)))
42  {
43  ISupportErrorInfo *pserrinfo = static_cast<ISupportErrorInfo *>(pv);
44  if (pserrinfo->InterfaceSupportsErrorInfo(riid) == S_OK)
45  (void)GetErrorInfo(0, &perrinfo);
46  pserrinfo->Release();
47  }
48 
49  com_error_handler(hr, perrinfo);
50 }
51 
52 /* comutil.h */
54 
55 namespace _com_util
56 {
57 
58 BSTR WINAPI ConvertStringToBSTR(const char *pSrc)
59 {
60  DWORD cwch;
61  BSTR wsOut(NULL);
62 
63  if (!pSrc) return NULL;
64 
65  /* Compute the needed size with the NULL terminator */
66  cwch = ::MultiByteToWideChar(CP_ACP, 0, pSrc, -1, NULL, 0);
67  if (cwch == 0) return NULL;
68 
69  /* Allocate the BSTR (without the NULL terminator) */
70  wsOut = ::SysAllocStringLen(NULL, cwch - 1);
71  if (!wsOut)
72  {
74  return NULL;
75  }
76 
77  /* Convert the string */
78  if (::MultiByteToWideChar(CP_ACP, 0, pSrc, -1, wsOut, cwch) == 0)
79  {
80  /* We failed, clean everything up */
81  cwch = ::GetLastError();
82 
83  ::SysFreeString(wsOut);
84  wsOut = NULL;
85 
86  ::_com_issue_error(!IS_ERROR(cwch) ? HRESULT_FROM_WIN32(cwch) : cwch);
87  }
88 
89  return wsOut;
90 }
91 
93 {
94  DWORD cb, cwch;
95  char *szOut = NULL;
96 
97  if (!pSrc) return NULL;
98 
99  /* Retrieve the size of the BSTR with the NULL terminator */
100  cwch = ::SysStringLen(pSrc) + 1;
101 
102  /* Compute the needed size with the NULL terminator */
103  cb = ::WideCharToMultiByte(CP_ACP, 0, pSrc, cwch, NULL, 0, NULL, NULL);
104  if (cb == 0)
105  {
106  cwch = ::GetLastError();
107  ::_com_issue_error(!IS_ERROR(cwch) ? HRESULT_FROM_WIN32(cwch) : cwch);
108  return NULL;
109  }
110 
111  /* Allocate the string */
112  szOut = (char*)::operator new(cb * sizeof(char));
113  if (!szOut)
114  {
116  return NULL;
117  }
118 
119  /* Convert the string and NULL-terminate */
120  szOut[cb - 1] = '\0';
121  if (::WideCharToMultiByte(CP_ACP, 0, pSrc, cwch, szOut, cb, NULL, NULL) == 0)
122  {
123  /* We failed, clean everything up */
124  cwch = ::GetLastError();
125 
126  ::operator delete(szOut);
127  szOut = NULL;
128 
129  ::_com_issue_error(!IS_ERROR(cwch) ? HRESULT_FROM_WIN32(cwch) : cwch);
130  }
131 
132  return szOut;
133 }
134 
135 }
BSTR WINAPI ConvertStringToBSTR(const char *pSrc)
Definition: comsupp.cpp:58
#define REFIID
Definition: guiddef.h:118
void WINAPI _com_issue_error(HRESULT hr)
Definition: comsupp.cpp:31
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92
#define WideCharToMultiByte
Definition: compat.h:101
HRESULT hr
Definition: shlfolder.c:183
BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
Definition: oleaut.c:339
REFIID riid
Definition: precomp.h:44
#define CP_ACP
Definition: compat.h:99
operator
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
OLECHAR * BSTR
Definition: compat.h:1942
#define DISP_E_PARAMNOTFOUND
Definition: winerror.h:2513
_variant_t vtMissing(DISP_E_PARAMNOTFOUND, VT_ERROR)
char *WINAPI ConvertBSTRToString(BSTR pSrc)
Definition: comsupp.cpp:92
ULONG Release()
smooth NULL
Definition: ftsmooth.c:416
HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
Definition: errorinfo.c:417
LONG HRESULT
Definition: typedefs.h:77
#define WINAPI
Definition: msvc.h:8
unsigned long DWORD
Definition: ntddk_ex.h:95
void WINAPI _com_raise_error(HRESULT hr, IErrorInfo *perrinfo)
Definition: comsupp.cpp:21
UINT WINAPI SysStringLen(BSTR str)
Definition: oleaut.c:196
void WINAPI _set_com_error_handler(COM_ERROR_HANDLER *phandler)
Definition: comsupp.cpp:26
void WINAPI COM_ERROR_HANDLER(HRESULT, IErrorInfo *)
Definition: comsupp.cpp:18
#define S_OK
Definition: intsafe.h:59
#define IS_ERROR(stat)
Definition: winerror.h:74
HRESULT InterfaceSupportsErrorInfo([in] REFIID riid)
void WINAPI DECLSPEC_HOTPATCH SysFreeString(BSTR str)
Definition: oleaut.c:271
void WINAPI _com_issue_errorex(HRESULT hr, IUnknown *punk, REFIID riid)
Definition: comsupp.cpp:36
#define MultiByteToWideChar
Definition: compat.h:100
HRESULT QueryInterface([in] REFIID riid, [out, iid_is(riid)] void **ppvObject)
static HMODULE MODULEINFO DWORD cb
Definition: module.c:32
static COM_ERROR_HANDLER * com_error_handler
Definition: comsupp.cpp:19
#define SUCCEEDED(hr)
Definition: intsafe.h:57
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13