ReactOS  0.4.15-dev-5153-gfece68b
atlcomcli.h
Go to the documentation of this file.
1 /*
2  * ReactOS ATL
3  *
4  * Copyright 2009 Andrew Hill <ash77@reactos.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #pragma once
22 
23 #include "atlcore.h"
24 
25 
26 #ifdef _MSC_VER
27 // It is common to use this in ATL constructors. They only store this for later use, so the usage is safe.
28 #pragma warning(disable:4355)
29 #endif
30 
31 #ifndef _ATL_PACKING
32 #define _ATL_PACKING 8
33 #endif
34 
35 #ifndef _ATL_FREE_THREADED
36 #ifndef _ATL_APARTMENT_THREADED
37 #ifndef _ATL_SINGLE_THREADED
38 #define _ATL_FREE_THREADED
39 #endif
40 #endif
41 #endif
42 
43 #ifndef ATLTRY
44 #define ATLTRY(x) x;
45 #endif
46 
47 #ifdef _ATL_DISABLE_NO_VTABLE
48 #define ATL_NO_VTABLE
49 #else
50 #define ATL_NO_VTABLE __declspec(novtable)
51 #endif
52 
53 namespace ATL
54 {
55 
57 {
58  DWORD dwError = ::GetLastError();
59  return HRESULT_FROM_WIN32(dwError);
60 }
61 
62 template <class T>
64 {
65  private:
66  virtual ULONG STDMETHODCALLTYPE AddRef() = 0;
67  virtual ULONG STDMETHODCALLTYPE Release() = 0;
68 };
69 
70 template<class T>
71 class CComPtr
72 {
73 public:
74  T *p;
75 public:
77  {
78  p = NULL;
79  }
80 
81  CComPtr(T *lp)
82  {
83  p = lp;
84  if (p != NULL)
85  p->AddRef();
86  }
87 
88  CComPtr(const CComPtr<T> &lp)
89  {
90  p = lp.p;
91  if (p != NULL)
92  p->AddRef();
93  }
94 
96  {
97  if (p != NULL)
98  p->Release();
99  }
100 
102  {
103  T* pOld = p;
104 
105  p = lp;
106  if (p != NULL)
107  p->AddRef();
108 
109  if (pOld != NULL)
110  pOld->Release();
111 
112  return *this;
113  }
114 
116  {
117  T* pOld = p;
118 
119  p = lp.p;
120  if (p != NULL)
121  p->AddRef();
122 
123  if (pOld != NULL)
124  pOld->Release();
125 
126  return *this;
127  }
128 
129  // We cannot enable this until gcc starts supporting __uuidof
130  // See CORE-12710
131 #if 0
132  template <typename Q>
133  T* operator=(const CComPtr<Q>& lp)
134  {
135  T* pOld = p;
136 
137  if (!lp.p || FAILED(lp.p->QueryInterface(__uuidof(T), (void**)(IUnknown**)&p)))
138  p = NULL;
139 
140  if (pOld != NULL)
141  pOld->Release();
142 
143  return *this;
144  }
145 #endif
146 
147  void Release()
148  {
149  if (p != NULL)
150  {
151  p->Release();
152  p = NULL;
153  }
154  }
155 
156  void Attach(T *lp)
157  {
158  if (p != NULL)
159  p->Release();
160  p = lp;
161  }
162 
164  {
165  T *saveP;
166 
167  saveP = p;
168  p = NULL;
169  return saveP;
170  }
171 
173  {
174  ATLASSERT(p == NULL);
175  return &p;
176  }
177 
178  operator T * ()
179  {
180  return p;
181  }
182 
184  {
185  ATLASSERT(p != NULL);
187  }
188 };
189 
190 
191 //CComQIIDPtr<I_ID(Itype)> is the gcc compatible version of CComQIPtr<Itype>
192 #define I_ID(Itype) Itype,&IID_##Itype
193 
194 template <class T, const IID* piid>
195 class CComQIIDPtr :
196  public CComPtr<T>
197 {
198 public:
199  // Let's tell GCC how to find a symbol.
200  using CComPtr<T>::p;
201 
203  {
204  }
206  CComPtr<T>(lp)
207  {
208  }
210  CComPtr<T>(lp.p)
211  {
212  }
214  {
215  if (lp != NULL)
216  {
217  if (FAILED(lp->QueryInterface(*piid, (void**)(IUnknown**)&p)))
218  p = NULL;
219  }
220  }
222  {
223  T* pOld = p;
224 
225  p = lp;
226  if (p != NULL)
227  p->AddRef();
228 
229  if (pOld != NULL)
230  pOld->Release();
231 
232  return *this;
233  }
234 
236  {
237  T* pOld = p;
238 
239  p = lp.p;
240  if (p != NULL)
241  p->AddRef();
242 
243  if (pOld != NULL)
244  pOld->Release();
245 
246  return *this;
247  }
248 
250  {
251  T* pOld = p;
252 
253  if (!lp || FAILED(lp->QueryInterface(*piid, (void**)(IUnknown**)&p)))
254  p = NULL;
255 
256  if (pOld != NULL)
257  pOld->Release();
258 
259  return *this;
260  }
261 };
262 
263 
264 class CComBSTR
265 {
266 public:
268 public:
270  m_str(NULL)
271  {
272  }
273 
274  CComBSTR(LPCOLESTR pSrc)
275  {
276  if (pSrc == NULL)
277  m_str = NULL;
278  else
279  m_str = ::SysAllocString(pSrc);
280  }
281 
283  {
284  if (length == 0)
285  m_str = NULL;
286  else
288  }
289 
290  CComBSTR(int length, LPCOLESTR pSrc)
291  {
292  if (length == 0)
293  m_str = NULL;
294  else
296  }
297 
299  {
300  if (pSrc)
301  {
302  int len = MultiByteToWideChar(CP_THREAD_ACP, 0, pSrc, -1, NULL, 0);
304  if (m_str)
305  {
306  int res = MultiByteToWideChar(CP_THREAD_ACP, 0, pSrc, -1, m_str, len);
307  ATLASSERT(res == len);
308  if (res != len)
309  {
311  m_str = NULL;
312  }
313  }
314  }
315  else
316  {
317  m_str = NULL;
318  }
319  }
320 
322  {
323  m_str = other.Copy();
324  }
325 
327  {
328  OLECHAR szGuid[40];
331  }
332 
334  {
336  m_str = NULL;
337  }
338 
339  operator BSTR () const
340  {
341  return m_str;
342  }
343 
345  {
346  return &m_str;
347  }
348 
350  {
352  m_str = other.Copy();
353  return *this;
354  }
355 
356  void Attach(BSTR bstr)
357  {
359  m_str = bstr;
360  }
361 
363  {
364  BSTR str = m_str;
365  m_str = NULL;
366  return str;
367  }
368 
369  BSTR Copy() const
370  {
371  if (!m_str)
372  return NULL;
374  }
375 
377  {
378  if (!other)
379  return E_POINTER;
380  *other = Copy();
381  return S_OK;
382  }
383 
385  {
387  m_str = NULL;
388  const wchar_t *ptr = NULL;
389  int len = ::LoadStringW(module, uID, (PWSTR)&ptr, 0);
390  if (len)
392  return m_str != NULL;
393  }
394 
395  unsigned int Length() const
396  {
398  }
399 
400  unsigned int ByteLength() const
401  {
403  }
404 };
405 
406 
407 class CComVariant : public tagVARIANT
408 {
409 public:
411  {
412  ::VariantInit(this);
413  }
414 
416  {
417  V_VT(this) = VT_EMPTY;
418  Copy(&other);
419  }
420 
422  {
423  Clear();
424  }
425 
426  CComVariant(LPCOLESTR lpStr)
427  {
428  V_VT(this) = VT_BSTR;
429  V_BSTR(this) = ::SysAllocString(lpStr);
430  }
431 
433  {
434  V_VT(this) = VT_BSTR;
435  CComBSTR str(lpStr);
436  V_BSTR(this) = str.Detach();
437  }
438 
440  {
441  V_VT(this) = VT_BOOL;
442  V_BOOL(this) = value ? VARIANT_TRUE : VARIANT_FALSE;
443  }
444 
446  {
447  V_VT(this) = VT_I1;
448  V_I1(this) = value;
449  }
450 
452  {
453  V_VT(this) = VT_UI1;
454  V_UI1(this) = value;
455  }
456 
458  {
459  V_VT(this) = VT_I2;
460  V_I2(this) = value;
461  }
462 
463  CComVariant(unsigned short value)
464  {
465  V_VT(this) = VT_UI2;
466  V_UI2(this) = value;
467  }
468 
470  {
471  if (type == VT_I4 || type == VT_INT)
472  {
473  V_VT(this) = type;
474  V_I4(this) = value;
475  }
476  else
477  {
478  V_VT(this) = VT_ERROR;
479  V_ERROR(this) = E_INVALIDARG;
480  }
481  }
482 
484  {
485  if (type == VT_UI4 || type == VT_UINT)
486  {
487  V_VT(this) = type;
488  V_UI4(this) = value;
489  }
490  else
491  {
492  V_VT(this) = VT_ERROR;
493  V_ERROR(this) = E_INVALIDARG;
494  }
495  }
496 
498  {
499  if (type == VT_I4 || type == VT_ERROR)
500  {
501  V_VT(this) = type;
502  V_I4(this) = value;
503  }
504  else
505  {
506  V_VT(this) = VT_ERROR;
507  V_ERROR(this) = E_INVALIDARG;
508  }
509  }
510 
511  CComVariant(unsigned long value)
512  {
513  V_VT(this) = VT_UI4;
514  V_UI4(this) = value;
515  }
516 
518  {
519  V_VT(this) = VT_R4;
520  V_R4(this) = value;
521  }
522 
524  {
525  if (type == VT_R8 || type == VT_DATE)
526  {
527  V_VT(this) = type;
528  V_R8(this) = value;
529  }
530  else
531  {
532  V_VT(this) = VT_ERROR;
533  V_ERROR(this) = E_INVALIDARG;
534  }
535  }
536 
538  {
539  V_VT(this) = VT_I8;
540  V_I8(this) = value;
541  }
542 
544  {
545  V_VT(this) = VT_UI8;
546  V_UI8(this) = value;
547  }
548 
550  {
551  V_VT(this) = VT_CY;
552  V_I8(this) = value.int64;
553  }
554 
555 
557  {
558  return ::VariantClear(this);
559  }
560 
562  {
563  return ::VariantCopy(this, const_cast<VARIANT*>(src));
564  }
565 
567  {
568  const LPVARIANT lpSrc = src ? src : this;
569  return ::VariantChangeType(this, lpSrc, 0, newType);
570  }
571 };
572 
573 
574 
575 }; // namespace ATL
576 
577 #ifndef _ATL_NO_AUTOMATIC_NAMESPACE
578 using namespace ATL;
579 #endif
580 
WCHAR OLECHAR
Definition: compat.h:2151
CComVariant(const ULONGLONG &value)
Definition: atlcomcli.h:543
CComVariant(float value)
Definition: atlcomcli.h:517
CComBSTR(const CComBSTR &other)
Definition: atlcomcli.h:321
HRESULT WINAPI DECLSPEC_HOTPATCH VariantChangeType(VARIANTARG *pvargDest, VARIANTARG *pvargSrc, USHORT wFlags, VARTYPE vt)
Definition: variant.c:962
Definition: pdh_main.c:93
CComQIIDPtr(_Inout_ const CComQIIDPtr< T, piid > &lp)
Definition: atlcomcli.h:209
Definition: compat.h:2157
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92
#define _In_opt_
Definition: ms_sal.h:309
#define _Inout_
Definition: ms_sal.h:378
Definition: compat.h:2173
Definition: scsiwmi.h:51
BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
Definition: oleaut.c:339
Definition: compat.h:2169
#define ATLASSERT(x)
Definition: CComVariant.cpp:10
CComVariant(long value, VARENUM type=VT_I4)
Definition: atlcomcli.h:497
_NoAddRefReleaseOnCComPtr< T > * operator ->() const
Definition: atlcomcli.h:183
uint16_t * PWSTR
Definition: typedefs.h:56
int other
Definition: msacm.c:1376
#define V_R8(A)
Definition: oleauto.h:262
CComBSTR(LPCOLESTR pSrc)
Definition: atlcomcli.h:274
#define V_I2(A)
Definition: oleauto.h:245
CComVariant(bool value)
Definition: atlcomcli.h:439
CComBSTR(int length)
Definition: atlcomcli.h:282
CComVariant(short value)
Definition: atlcomcli.h:457
HRESULT ChangeType(_In_ VARTYPE newType, _In_opt_ const LPVARIANT src=NULL)
Definition: atlcomcli.h:566
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1040
UINT WINAPI SysStringByteLen(BSTR str)
Definition: oleaut.c:215
CComVariant(char value)
Definition: atlcomcli.h:445
OLECHAR * BSTR
Definition: compat.h:2152
void WINAPI VariantInit(VARIANTARG *pVarg)
Definition: variant.c:568
CComVariant(LPCSTR lpStr)
Definition: atlcomcli.h:432
int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_writes_to_(cchBufferMax, return+1) LPWSTR lpBuffer, _In_ int cchBufferMax)
#define T
Definition: mbstring.h:31
#define V_UI8(A)
Definition: oleauto.h:272
#define V_I4(A)
Definition: oleauto.h:247
BSTR Detach()
Definition: atlcomcli.h:362
HRESULT Clear()
Definition: atlcomcli.h:556
const GUID * guid
Definition: compat.h:2114
unsigned int ByteLength() const
Definition: atlcomcli.h:400
CComBSTR(PCSTR pSrc)
Definition: atlcomcli.h:298
CComVariant(const CComVariant &other)
Definition: atlcomcli.h:415
virtual ULONG STDMETHODCALLTYPE AddRef()=0
CComVariant(unsigned long value)
Definition: atlcomcli.h:511
GLenum GLuint GLenum GLsizei length
Definition: glext.h:5579
static PVOID ptr
Definition: dispmode.c:27
#define E_INVALIDARG
Definition: ddrawi.h:101
const WCHAR * str
HRESULT AtlHresultFromLastError()
Definition: atlcomcli.h:56
virtual ULONG STDMETHODCALLTYPE Release()=0
#define V_UI2(A)
Definition: oleauto.h:268
#define _In_
Definition: ms_sal.h:308
BSTR WINAPI SysAllocString(LPCOLESTR str)
Definition: oleaut.c:238
#define V_I1(A)
Definition: oleauto.h:243
CComQIIDPtr(_Inout_opt_ IUnknown *lp)
Definition: atlcomcli.h:213
const char * LPCSTR
Definition: xmlstorage.h:183
CComVariant(LPCOLESTR lpStr)
Definition: atlcomcli.h:426
CComBSTR(REFGUID guid)
Definition: atlcomcli.h:326
#define V_I8(A)
Definition: oleauto.h:249
int64_t LONGLONG
Definition: typedefs.h:68
CComVariant(BYTE value)
Definition: atlcomcli.h:451
Definition: rosdlgs.h:5
#define V_R4(A)
Definition: oleauto.h:260
CComPtr(const CComPtr< T > &lp)
Definition: atlcomcli.h:88
CComVariant(const CY &value)
Definition: atlcomcli.h:549
BSTR Copy() const
Definition: atlcomcli.h:369
LONG HRESULT
Definition: typedefs.h:79
CComVariant(const LONGLONG &value)
Definition: atlcomcli.h:537
uint64_t ULONGLONG
Definition: typedefs.h:67
CComVariant(unsigned int value, VARENUM type=VT_UI4)
Definition: atlcomcli.h:483
#define V_UI1(A)
Definition: oleauto.h:266
INT WINAPI StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
Definition: compobj.c:2434
#define STDMETHODCALLTYPE
Definition: bdasup.h:9
#define V_BOOL(A)
Definition: oleauto.h:224
nsrefcnt Release()
unsigned long DWORD
Definition: ntddk_ex.h:95
CComVariant(int value, VARENUM type=VT_I4)
Definition: atlcomcli.h:469
#define V_UI4(A)
Definition: oleauto.h:270
T ** operator &()
Definition: atlcomcli.h:172
T * operator=(T *lp)
Definition: atlcomcli.h:221
HRESULT WINAPI DECLSPEC_HOTPATCH VariantClear(VARIANTARG *pVarg)
Definition: variant.c:648
Definition: compat.h:2158
CComQIIDPtr(_Inout_opt_ T *lp)
Definition: atlcomcli.h:205
void Attach(BSTR bstr)
Definition: atlcomcli.h:356
#define V_VT(A)
Definition: oleauto.h:211
#define _Inout_opt_
Definition: ms_sal.h:379
CComVariant(unsigned short value)
Definition: atlcomcli.h:463
GLenum GLsizei len
Definition: glext.h:6722
UINT WINAPI SysStringLen(BSTR str)
Definition: oleaut.c:196
GLsizei const GLfloat * value
Definition: glext.h:6069
HRESULT Copy(_In_ const VARIANT *src)
Definition: atlcomcli.h:561
GLenum src
Definition: glext.h:6340
Definition: compat.h:2160
HRESULT CopyTo(BSTR *other) const
Definition: atlcomcli.h:376
unsigned char BYTE
Definition: xxhash.c:193
#define V_BSTR(A)
Definition: oleauto.h:226
CComBSTR(int length, LPCOLESTR pSrc)
Definition: atlcomcli.h:290
CComVariant(double value, VARENUM type=VT_R8)
Definition: atlcomcli.h:523
CComPtr(T *lp)
Definition: atlcomcli.h:81
#define S_OK
Definition: intsafe.h:52
VARENUM
Definition: compat.h:2153
bool LoadString(HMODULE module, DWORD uID)
Definition: atlcomcli.h:384
static const WCHAR szGuid[]
Definition: rtlstr.c:1892
void Release()
Definition: atlcomcli.h:147
unsigned short VARTYPE
Definition: compat.h:2113
BSTR * operator &()
Definition: atlcomcli.h:344
void WINAPI DECLSPEC_HOTPATCH SysFreeString(BSTR str)
Definition: oleaut.c:271
#define V_ERROR(A)
Definition: oleauto.h:241
#define NULL
Definition: types.h:112
#define MultiByteToWideChar
Definition: compat.h:110
HRESULT QueryInterface([in] REFIID riid, [out, iid_is(riid)] void **ppvObject)
GLuint res
Definition: glext.h:9613
T * operator=(T *lp)
Definition: atlcomcli.h:101
unsigned int ULONG
Definition: retypes.h:1
CComBSTR & operator=(const CComBSTR &other)
Definition: atlcomcli.h:349
const char * PCSTR
Definition: typedefs.h:52
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
T * Detach()
Definition: atlcomcli.h:163
GLfloat GLfloat p
Definition: glext.h:8902
HRESULT WINAPI VariantCopy(VARIANTARG *pvargDest, VARIANTARG *pvargSrc)
Definition: variant.c:748
#define E_POINTER
Definition: winerror.h:2365
#define CP_THREAD_ACP
Definition: winnls.h:233
unsigned int Length() const
Definition: atlcomcli.h:395
Definition: compat.h:2159
void Attach(T *lp)
Definition: atlcomcli.h:156
Definition: compat.h:2156
T * operator=(IUnknown *lp)
Definition: atlcomcli.h:249