ReactOS 0.4.17-dev-116-ga4b6fe9
comsupp.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS crt library
3 * LICENSE: BSD-2-Clause (https://spdx.org/licenses/BSD-2-Clause)
4 * PURPOSE: Compiler support for COM
5 * COPYRIGHT: Copyright 2013 Thomas Faber (thomas.faber@reactos.org)
6 * Copyright 2015 Hermès Bélusca-Maïto (hermes.belusca-maito@reactos.org)
7 */
8
9#include <stdarg.h>
10
11#define WIN32_NO_STATUS
12#define WIN32_LEAN_AND_MEAN
13#define _INC_WINDOWS
14
15#include <windef.h>
16#include <winbase.h>
17#include <comdef.h>
18
19/* comdef.h */
22
24{
25 throw _com_error(hr, perrinfo);
26}
27
29{
30 com_error_handler = phandler;
31}
32
34{
36}
37
39{
40 void *pv;
41 IErrorInfo *perrinfo = NULL;
42
44 {
45 ISupportErrorInfo *pserrinfo = static_cast<ISupportErrorInfo *>(pv);
46 if (pserrinfo->InterfaceSupportsErrorInfo(riid) == S_OK)
47 (void)GetErrorInfo(0, &perrinfo);
48 pserrinfo->Release();
49 }
50
51 com_error_handler(hr, perrinfo);
52}
53
54/* comutil.h */
56
57namespace _com_util
58{
59
61{
62 DWORD cwch;
63 BSTR wsOut(NULL);
64
65 if (!pSrc) return NULL;
66
67 /* Compute the needed size with the NULL terminator */
68 cwch = ::MultiByteToWideChar(CP_ACP, 0, pSrc, -1, NULL, 0);
69 if (cwch == 0) return NULL;
70
71 /* Allocate the BSTR (without the NULL terminator) */
72 wsOut = ::SysAllocStringLen(NULL, cwch - 1);
73 if (!wsOut)
74 {
76 return NULL;
77 }
78
79 /* Convert the string */
80 if (::MultiByteToWideChar(CP_ACP, 0, pSrc, -1, wsOut, cwch) == 0)
81 {
82 /* We failed, clean everything up */
83 cwch = ::GetLastError();
84
85 ::SysFreeString(wsOut);
86 wsOut = NULL;
87
88 ::_com_issue_error(!IS_ERROR(cwch) ? HRESULT_FROM_WIN32(cwch) : cwch);
89 }
90
91 return wsOut;
92}
93
95{
96 DWORD cb, cwch;
97 char *szOut = NULL;
98
99 if (!pSrc) return NULL;
100
101 /* Retrieve the size of the BSTR with the NULL terminator */
102 cwch = ::SysStringLen(pSrc) + 1;
103
104 /* Compute the needed size with the NULL terminator */
105 cb = ::WideCharToMultiByte(CP_ACP, 0, pSrc, cwch, NULL, 0, NULL, NULL);
106 if (cb == 0)
107 {
108 cwch = ::GetLastError();
109 ::_com_issue_error(!IS_ERROR(cwch) ? HRESULT_FROM_WIN32(cwch) : cwch);
110 return NULL;
111 }
112
113 /* Allocate the string */
114 szOut = (char*)::operator new(cb * sizeof(char));
115 if (!szOut)
116 {
118 return NULL;
119 }
120
121 /* Convert the string and NULL-terminate */
122 szOut[cb - 1] = '\0';
123 if (::WideCharToMultiByte(CP_ACP, 0, pSrc, cwch, szOut, cb, NULL, NULL) == 0)
124 {
125 /* We failed, clean everything up */
126 cwch = ::GetLastError();
127
128 ::operator delete(szOut);
129 szOut = NULL;
130
131 ::_com_issue_error(!IS_ERROR(cwch) ? HRESULT_FROM_WIN32(cwch) : cwch);
132 }
133
134 return szOut;
135}
136
137}
void WINAPI _com_issue_errorex(HRESULT hr, IUnknown *punk, REFIID riid)
Definition: comsupp.cpp:38
void WINAPI _com_issue_error(HRESULT hr)
Definition: comsupp.cpp:33
void WINAPI _set_com_error_handler(COM_ERROR_HANDLER *phandler)
Definition: comsupp.cpp:28
void WINAPI COM_ERROR_HANDLER(HRESULT, IErrorInfo *)
Definition: comsupp.cpp:20
void WINAPI _com_raise_error(HRESULT hr, IErrorInfo *perrinfo)
Definition: comsupp.cpp:23
static COM_ERROR_HANDLER * com_error_handler
Definition: comsupp.cpp:21
_variant_t vtMissing(DISP_E_PARAMNOTFOUND, VT_ERROR)
HRESULT hr
Definition: delayimp.cpp:573
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13
#define NULL
Definition: types.h:112
HRESULT WINAPI GetErrorInfo(ULONG reserved, IErrorInfo **error_info)
Definition: errorinfo.c:346
#define CP_ACP
Definition: compat.h:109
OLECHAR * BSTR
Definition: compat.h:2293
#define WideCharToMultiByte
Definition: compat.h:111
#define MultiByteToWideChar
Definition: compat.h:110
@ VT_ERROR
Definition: compat.h:2305
static MonoProfilerRuntimeShutdownBeginCallback cb
Definition: metahost.c:118
unsigned long DWORD
Definition: ntddk_ex.h:95
REFIID riid
Definition: atlbase.h:39
HRESULT InterfaceSupportsErrorInfo([in] REFIID riid)
HRESULT QueryInterface([in] REFIID riid, [out, iid_is(riid)] void **ppvObject)
ULONG Release()
#define S_OK
Definition: intsafe.h:52
#define SUCCEEDED(hr)
Definition: intsafe.h:50
BSTR WINAPI ConvertStringToBSTR(const char *pSrc)
Definition: comsupp.cpp:60
char *WINAPI ConvertBSTRToString(BSTR pSrc)
Definition: comsupp.cpp:94
UINT WINAPI SysStringLen(BSTR str)
Definition: oleaut.c:196
void WINAPI DECLSPEC_HOTPATCH SysFreeString(BSTR str)
Definition: oleaut.c:271
BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
Definition: oleaut.c:339
#define REFIID
Definition: guiddef.h:118
_In_opt_ IUnknown * punk
Definition: shlwapi.h:158
operator
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define WINAPI
Definition: msvc.h:6
static HRESULT HRESULT_FROM_WIN32(unsigned int x)
Definition: winerror.h:210
#define DISP_E_PARAMNOTFOUND
Definition: winerror.h:3616
#define IS_ERROR(stat)
Definition: winerror.h:186