1b97a21d40550bdd8a67b293580fc87c5ea4cda5
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)
10 #define WIN32_NO_STATUS
18 typedef void WINAPI
COM_ERROR_HANDLER(HRESULT
, IErrorInfo
*);
19 static COM_ERROR_HANDLER
*com_error_handler
;
21 void WINAPI
_com_raise_error(HRESULT hr
, IErrorInfo
*perrinfo
)
23 throw _com_error(hr
, perrinfo
);
26 void WINAPI
_set_com_error_handler(COM_ERROR_HANDLER
*phandler
)
28 com_error_handler
= phandler
;
31 void WINAPI
_com_issue_error(HRESULT hr
)
33 com_error_handler(hr
, NULL
);
36 void WINAPI
_com_issue_errorex(HRESULT hr
, IUnknown
*punk
, REFIID riid
)
39 IErrorInfo
*perrinfo
= NULL
;
41 if (SUCCEEDED(punk
->QueryInterface(riid
, &pv
)))
43 ISupportErrorInfo
*pserrinfo
= static_cast<ISupportErrorInfo
*>(pv
);
44 if (pserrinfo
->InterfaceSupportsErrorInfo(riid
) == S_OK
)
45 (void)GetErrorInfo(0, &perrinfo
);
49 com_error_handler(hr
, perrinfo
);
53 _variant_t
vtMissing(DISP_E_PARAMNOTFOUND
, VT_ERROR
);
58 BSTR WINAPI
ConvertStringToBSTR(const char *pSrc
)
63 if (!pSrc
) return NULL
;
65 /* Compute the needed size with the NULL terminator */
66 cwch
= ::MultiByteToWideChar(CP_ACP
/* CP_UTF8 */, 0, pSrc
, -1, NULL
, 0);
67 if (cwch
== 0) return NULL
;
69 /* Allocate the BSTR (without the NULL terminator) */
70 wsOut
= ::SysAllocStringLen(NULL
, cwch
- 1);
73 ::_com_issue_error(HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY
));
77 /* Convert the string */
78 if (::MultiByteToWideChar(CP_ACP
/* CP_UTF8 */, 0, pSrc
, -1, wsOut
, cwch
) == 0)
80 /* We failed, clean everything up */
81 cwch
= ::GetLastError();
83 ::SysFreeString(wsOut
);
86 ::_com_issue_error(!IS_ERROR(cwch
) ? HRESULT_FROM_WIN32(cwch
) : cwch
);
92 char* WINAPI
ConvertBSTRToString(BSTR pSrc
)
97 if (!pSrc
) return NULL
;
99 /* Retrieve the size of the BSTR with the NULL terminator */
100 cwch
= ::SysStringLen(pSrc
) + 1;
102 /* Compute the needed size with the NULL terminator */
103 cb
= ::WideCharToMultiByte(CP_ACP
/* CP_UTF8 */, 0, pSrc
, cwch
, NULL
, 0, NULL
, NULL
);
106 cwch
= ::GetLastError();
107 ::_com_issue_error(!IS_ERROR(cwch
) ? HRESULT_FROM_WIN32(cwch
) : cwch
);
111 /* Allocate the string */
112 szOut
= new char[cb
];
115 ::_com_issue_error(HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY
));
119 /* Convert the string and NULL-terminate */
120 szOut
[cb
- 1] = '\0';
121 if (::WideCharToMultiByte(CP_ACP
/* CP_UTF8 */, 0, pSrc
, cwch
, szOut
, cb
, NULL
, NULL
) == 0)
123 /* We failed, clean everything up */
124 cwch
= ::GetLastError();
129 ::_com_issue_error(!IS_ERROR(cwch
) ? HRESULT_FROM_WIN32(cwch
) : cwch
);