|
|
|
| 1 |
|
/*** |
| 2 |
|
* comutil.h - Native C++ compiler COM support - BSTR, VARIANT wrappers header |
| 3 |
|
* |
| 4 |
|
* Copyright (c) Microsoft Corporation. All rights reserved. |
| 5 |
|
* |
| 6 |
|
****/ |
| 7 |
|
|
| 8 |
|
#if _MSC_VER > 1000 |
| 9 |
|
#pragma once |
| 10 |
|
#endif |
| 11 |
|
|
| 12 |
|
#ifdef _M_CEE_PURE |
| 13 |
|
#error comutil.h header cannot be included under /clr:safe or /clr:pure |
| 14 |
|
#endif |
| 15 |
|
|
| 16 |
|
#if !defined(_INC_COMUTIL) |
| 17 |
|
#define _INC_COMUTIL |
| 18 |
|
|
| 19 |
|
#include <ole2.h> |
| 20 |
|
|
| 21 |
|
#if !defined(_COM_ASSERT) |
| 22 |
|
# if defined(_DEBUG) |
| 23 |
|
# include <assert.h> |
| 24 |
|
# define _COM_ASSERT(x) assert(x) |
| 25 |
|
# else |
| 26 |
|
# define _COM_ASSERT(x) ((void)0) |
| 27 |
|
# endif |
| 28 |
|
#endif |
| 29 |
|
|
| 30 |
|
#if !defined(_SECURE_COMPILER_COM) |
| 31 |
|
/* use secure versions by default if not specified otherwise */ |
| 32 |
|
#define _SECURE_COMPILER_COM 1 |
| 33 |
|
#endif |
| 34 |
|
|
| 35 |
|
#if _SECURE_COMPILER_COM && defined(__GOT_SECURE_LIB__) && __GOT_SECURE_LIB__ >= 200402L |
| 36 |
|
|
| 37 |
|
#include <stdio.h> |
| 38 |
|
|
| 39 |
|
# define _COM_MEMCPY_S(dest, destsize, src, count) memcpy_s(dest, destsize, src, count) |
| 40 |
|
# if defined(UNICODE) |
| 41 |
|
# define _COM_PRINTF_S_1(dest, destsize, format, arg1) swprintf_s(dest, destsize, format, arg1) |
| 42 |
|
# else |
| 43 |
|
# define _COM_PRINTF_S_1(dest, destsize, format, arg1) sprintf_s(dest, destsize, format, arg1) |
| 44 |
|
# endif |
| 45 |
|
|
| 46 |
|
#else |
| 47 |
|
|
| 48 |
|
# define _COM_MEMCPY_S(dest, destsize, src, count) memcpy(dest, src, count) |
| 49 |
|
# define _COM_PRINTF_S_1(dest, destsize, format, arg1) wsprintf(dest, format, arg1) |
| 50 |
|
|
| 51 |
|
#endif |
| 52 |
|
|
| 53 |
|
#pragma warning(push) |
| 54 |
|
#pragma warning(disable: 4290) |
| 55 |
|
#pragma warning(disable: 4310) |
| 56 |
|
|
| 57 |
|
#pragma push_macro("new") |
| 58 |
|
#undef new |
| 59 |
|
|
| 60 |
|
/* Add macros if the macros were not defined. */ |
| 61 |
|
#ifndef S_OK |
| 62 |
|
#define S_OK ((HRESULT)0L) |
| 63 |
|
#endif |
| 64 |
|
#ifndef INTSAFE_E_ARITHMETIC_OVERFLOW |
| 65 |
|
#define INTSAFE_E_ARITHMETIC_OVERFLOW ((HRESULT)-1) |
| 66 |
|
#endif |
| 67 |
|
#ifndef INTSAFE_UINT_MAX |
| 68 |
|
#define INTSAFE_UINT_MAX 0xffffffff |
| 69 |
|
#endif |
| 70 |
|
#ifndef FAILED |
| 71 |
|
#define FAILED(hr) (((HRESULT)(hr)) < 0) |
| 72 |
|
#endif |
| 73 |
|
|
| 74 |
|
class _com_error; |
| 75 |
|
|
| 76 |
|
void __declspec(noreturn) __stdcall _com_issue_error(HRESULT); |
| 77 |
|
|
| 78 |
|
////////////////////////////////////////////////////////////////////////////// |
| 79 |
|
// |
| 80 |
|
// Forward class declarations |
| 81 |
|
// |
| 82 |
|
////////////////////////////////////////////////////////////////////////////// |
| 83 |
|
|
| 84 |
|
class _bstr_t; |
| 85 |
|
class _variant_t; |
| 86 |
|
|
| 87 |
|
////////////////////////////////////////////////////////////////////////////// |
| 88 |
|
// |
| 89 |
|
// Error checking routines |
| 90 |
|
// |
| 91 |
|
////////////////////////////////////////////////////////////////////////////// |
| 92 |
|
|
| 93 |
|
namespace _com_util { |
| 94 |
|
inline void CheckError(HRESULT hr) throw(...) |
| 95 |
|
{ |
| 96 |
|
if (FAILED(hr)) { |
| 97 |
|
_com_issue_error(hr); |
| 98 |
|
} |
| 99 |
|
} |
| 100 |
|
static HRESULT UIntAdd(UINT uAugend, UINT uAddend, UINT *puResult) |
| 101 |
|
{ |
| 102 |
|
if((uAugend + uAddend) < uAddend) |
| 103 |
|
{ |
| 104 |
|
return INTSAFE_E_ARITHMETIC_OVERFLOW; |
| 105 |
|
} |
| 106 |
|
*puResult = uAugend + uAddend; |
| 107 |
|
return S_OK; |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
static HRESULT UIntMult(UINT uMultiplicand, UINT uMultiplier, UINT *puResult) |
| 111 |
|
{ |
| 112 |
|
ULONGLONG ull64Result = UInt32x32To64(uMultiplicand, uMultiplier); |
| 113 |
|
if(ull64Result <= INTSAFE_UINT_MAX) |
| 114 |
|
{ |
| 115 |
|
*puResult = (UINT)ull64Result; |
| 116 |
|
return S_OK; |
| 117 |
|
} |
| 118 |
|
return INTSAFE_E_ARITHMETIC_OVERFLOW; |
| 119 |
|
} |
| 120 |
|
} |
| 121 |
|
|
| 2338 |
|
// |
| 2339 |
|
////////////////////////////////////////////////////////////////////////////////////////// |
| 2340 |
|
|
| 2341 |
|
// Construct a _bstr_t from a const _variant_t& |
| 2342 |
|
// |
| 2343 |
|
inline _bstr_t::_bstr_t(const _variant_t &var) |
| 2344 |
|
: m_Data(NULL) |
| 2345 |
|
{ |
| 2346 |
|
if (V_VT(&var) == VT_BSTR) { |
| 2347 |
|
*this = V_BSTR(&var); |
| 2348 |
|
return; |
| 2349 |
|
} |
| 2350 |
|
|
| 2351 |
|
_variant_t varDest; |
| 2352 |
|
varDest.ChangeType(VT_BSTR, &var); |
| 2353 |
|
|
| 2354 |
|
*this = V_BSTR(&varDest); |
| 2355 |
|
} |
| 2356 |
|
|
| 2357 |
|
// Assign a const _variant_t& to a _bstr_t |
| 2358 |
|
// |
| 2359 |
|
inline _bstr_t& _bstr_t::operator=(const _variant_t &var) |
| 2360 |
|
{ |
| 2361 |
|
if (V_VT(&var) == VT_BSTR) { |
| 2362 |
|
*this = V_BSTR(&var); |
| 2363 |
|
return *this; |
| 2364 |
|
} |
| 2365 |
|
|
| 2366 |
|
_variant_t varDest; |
| 2367 |
|
varDest.ChangeType(VT_BSTR, &var); |
| 2368 |
|
|
| 2369 |
|
*this = V_BSTR(&varDest); |
| 2370 |
|
|
| 2371 |
|
return *this; |
| 2372 |
|
} |
| 2373 |
|
|
| 2374 |
|
extern _variant_t vtMissing; |
| 2375 |
|
|
| 2376 |
|
#ifndef _USE_RAW |
| 2377 |
|
#define bstr_t _bstr_t |
| 2378 |
|
#define variant_t _variant_t |
| 2379 |
|
#endif |
| 2380 |
|
|
| 2381 |
|
#pragma pop_macro("new") |
| 2382 |
|
|
| 2383 |
|
#pragma warning(pop) |
| 2384 |
|
|
| 2385 |
|
#endif // _INC_COMUTIL |
| 2386 |
|
|
|
|
|