ReactOS  0.4.13-dev-100-gc8611ae
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 
63 template<class T>
64 class CComPtr
65 {
66 public:
67  T *p;
68 public:
70  {
71  p = NULL;
72  }
73 
74  CComPtr(T *lp)
75  {
76  p = lp;
77  if (p != NULL)
78  p->AddRef();
79  }
80 
81  CComPtr(const CComPtr<T> &lp)
82  {
83  p = lp.p;
84  if (p != NULL)
85  p->AddRef();
86  }
87 
89  {
90  if (p != NULL)
91  p->Release();
92  }
93 
94  T *operator = (T *lp)
95  {
96  T* pOld = p;
97 
98  p = lp;
99  if (p != NULL)
100  p->AddRef();
101 
102  if (pOld != NULL)
103  pOld->Release();
104 
105  return *this;
106  }
107 
109  {
110  T* pOld = p;
111 
112  p = lp.p;
113  if (p != NULL)
114  p->AddRef();
115 
116  if (pOld != NULL)
117  pOld->Release();
118 
119  return *this;
120  }
121 
122  // We cannot enable this until gcc starts supporting __uuidof
123  // See CORE-12710
124 #if 0
125  template <typename Q>
126  T* operator=(const CComPtr<Q>& lp)
127  {
128  T* pOld = p;
129 
130  if (!lp.p || FAILED(lp.p->QueryInterface(__uuidof(T), (void**)(IUnknown**)&p)))
131  p = NULL;
132 
133  if (pOld != NULL)
134  pOld->Release();
135 
136  return *this;
137  }
138 #endif
139 
140  void Release()
141  {
142  if (p != NULL)
143  {
144  p->Release();
145  p = NULL;
146  }
147  }
148 
149  void Attach(T *lp)
150  {
151  if (p != NULL)
152  p->Release();
153  p = lp;
154  }
155 
157  {
158  T *saveP;
159 
160  saveP = p;
161  p = NULL;
162  return saveP;
163  }
164 
166  {
167  ATLASSERT(p == NULL);
168  return &p;
169  }
170 
171  operator T * ()
172  {
173  return p;
174  }
175 
177  {
178  ATLASSERT(p != NULL);
179  return p;
180  }
181 };
182 
183 
184 //CComQIIDPtr<I_ID(Itype)> is the gcc compatible version of CComQIPtr<Itype>
185 #define I_ID(Itype) Itype,&IID_##Itype
186 
187 template <class T, const IID* piid>
188 class CComQIIDPtr :
189  public CComPtr<T>
190 {
191 public:
192  // Let's tell GCC how to find a symbol.
193  using CComPtr<T>::p;
194 
196  {
197  }
199  CComPtr<T>(lp)
200  {
201  }
203  CComPtr<T>(lp.p)
204  {
205  }
207  {
208  if (lp != NULL)
209  {
210  if (FAILED(lp->QueryInterface(*piid, (void**)(IUnknown**)&p)))
211  p = NULL;
212  }
213  }
215  {
216  T* pOld = p;
217 
218  p = lp;
219  if (p != NULL)
220  p->AddRef();
221 
222  if (pOld != NULL)
223  pOld->Release();
224 
225  return *this;
226  }
227 
229  {
230  T* pOld = p;
231 
232  p = lp.p;
233  if (p != NULL)
234  p->AddRef();
235 
236  if (pOld != NULL)
237  pOld->Release();
238 
239  return *this;
240  }
241 
243  {
244  T* pOld = p;
245 
246  if (!lp || FAILED(lp->QueryInterface(*piid, (void**)(IUnknown**)&p)))
247  p = NULL;
248 
249  if (pOld != NULL)
250  pOld->Release();
251 
252  return *this;
253  }
254 };
255 
256 
257 class CComBSTR
258 {
259 public:
261 public:
263  m_str(NULL)
264  {
265  }
266 
267  CComBSTR(LPCOLESTR pSrc)
268  {
269  if (pSrc == NULL)
270  m_str = NULL;
271  else
272  m_str = ::SysAllocString(pSrc);
273  }
274 
276  {
277  if (length == 0)
278  m_str = NULL;
279  else
281  }
282 
283  CComBSTR(int length, LPCOLESTR pSrc)
284  {
285  if (length == 0)
286  m_str = NULL;
287  else
289  }
290 
292  {
293  if (pSrc)
294  {
295  int len = MultiByteToWideChar(CP_THREAD_ACP, 0, pSrc, -1, NULL, 0);
297  if (m_str)
298  {
299  int res = MultiByteToWideChar(CP_THREAD_ACP, 0, pSrc, -1, m_str, len);
300  ATLASSERT(res == len);
301  if (res != len)
302  {
304  m_str = NULL;
305  }
306  }
307  }
308  else
309  {
310  m_str = NULL;
311  }
312  }
313 
315  {
316  m_str = other.Copy();
317  }
318 
320  {
321  OLECHAR szGuid[40];
324  }
325 
327  {
329  m_str = NULL;
330  }
331 
332  operator BSTR () const
333  {
334  return m_str;
335  }
336 
338  {
339  return &m_str;
340  }
341 
343  {
345  m_str = other.Copy();
346  return *this;
347  }
348 
349  void Attach(BSTR bstr)
350  {
352  m_str = bstr;
353  }
354 
356  {
357  BSTR str = m_str;
358  m_str = NULL;
359  return str;
360  }
361 
362  BSTR Copy() const
363  {
364  if (!m_str)
365  return NULL;
367  }
368 
370  {
371  if (!other)
372  return E_POINTER;
373  *other = Copy();
374  return S_OK;
375  }
376 
378  {
380  m_str = NULL;
381  const wchar_t *ptr = NULL;
382  int len = ::LoadStringW(module, uID, (PWSTR)&ptr, 0);
383  if (len)
385  return m_str != NULL;
386  }
387 
388  unsigned int Length() const
389  {
391  }
392 
393  unsigned int ByteLength() const
394  {
396  }
397 };
398 
399 
400 class CComVariant : public tagVARIANT
401 {
402 public:
404  {
405  ::VariantInit(this);
406  }
407 
409  {
410  V_VT(this) = VT_EMPTY;
411  Copy(&other);
412  }
413 
415  {
416  Clear();
417  }
418 
419  CComVariant(LPCOLESTR lpStr)
420  {
421  V_VT(this) = VT_BSTR;
422  V_BSTR(this) = ::SysAllocString(lpStr);
423  }
424 
426  {
427  V_VT(this) = VT_BSTR;
428  CComBSTR str(lpStr);
429  V_BSTR(this) = str.Detach();
430  }
431 
433  {
434  V_VT(this) = VT_BOOL;
435  V_BOOL(this) = value ? VARIANT_TRUE : VARIANT_FALSE;
436  }
437 
439  {
440  V_VT(this) = VT_I1;
441  V_I1(this) = value;
442  }
443 
445  {
446  V_VT(this) = VT_UI1;
447  V_UI1(this) = value;
448  }
449 
451  {
452  V_VT(this) = VT_I2;
453  V_I2(this) = value;
454  }
455 
456  CComVariant(unsigned short value)
457  {
458  V_VT(this) = VT_UI2;
459  V_UI2(this) = value;
460  }
461 
463  {
464  if (type == VT_I4 || type == VT_INT)
465  {
466  V_VT(this) = type;
467  V_I4(this) = value;
468  }
469  else
470  {
471  V_VT(this) = VT_ERROR;
472  V_ERROR(this) = E_INVALIDARG;
473  }
474  }
475 
477  {
478  if (type == VT_UI4 || type == VT_UINT)
479  {
480  V_VT(this) = type;
481  V_UI4(this) = value;
482  }
483  else
484  {
485  V_VT(this) = VT_ERROR;
486  V_ERROR(this) = E_INVALIDARG;
487  }
488  }
489 
491  {
492  if (type == VT_I4 || type == VT_ERROR)
493  {
494  V_VT(this) = type;
495  V_I4(this) = value;
496  }
497  else
498  {
499  V_VT(this) = VT_ERROR;
500  V_ERROR(this) = E_INVALIDARG;
501  }
502  }
503 
504  CComVariant(unsigned long value)
505  {
506  V_VT(this) = VT_UI4;
507  V_UI4(this) = value;
508  }
509 
511  {
512  V_VT(this) = VT_R4;
513  V_R4(this) = value;
514  }
515 
517  {
518  if (type == VT_R8 || type == VT_DATE)
519  {
520  V_VT(this) = type;
521  V_R8(this) = value;
522  }
523  else
524  {
525  V_VT(this) = VT_ERROR;
526  V_ERROR(this) = E_INVALIDARG;
527  }
528  }
529 
531  {
532  V_VT(this) = VT_I8;
533  V_I8(this) = value;
534  }
535 
537  {
538  V_VT(this) = VT_UI8;
539  V_UI8(this) = value;
540  }
541 
543  {
544  V_VT(this) = VT_CY;
545  V_I8(this) = value.int64;
546  }
547 
548 
550  {
551  return ::VariantClear(this);
552  }
553 
555  {
556  return ::VariantCopy(this, const_cast<VARIANT*>(src));
557  }
558 
560  {
561  const LPVARIANT lpSrc = src ? src : this;
562  return ::VariantChangeType(this, lpSrc, 0, newType);
563  }
564 };
565 
566 
567 
568 }; // namespace ATL
569 
570 #ifndef _ATL_NO_AUTOMATIC_NAMESPACE
571 using namespace ATL;
572 #endif
573 
WCHAR OLECHAR
Definition: compat.h:1933
CComVariant(const ULONGLONG &value)
Definition: atlcomcli.h:536
CComVariant(float value)
Definition: atlcomcli.h:510
CComBSTR(const CComBSTR &other)
Definition: atlcomcli.h:314
HRESULT WINAPI DECLSPEC_HOTPATCH VariantChangeType(VARIANTARG *pvargDest, VARIANTARG *pvargSrc, USHORT wFlags, VARTYPE vt)
Definition: variant.c:965
CComQIIDPtr(_Inout_ const CComQIIDPtr< T, piid > &lp)
Definition: atlcomcli.h:202
Definition: compat.h:1939
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92
Definition: compat.h:1955
Definition: scsiwmi.h:51
BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
Definition: oleaut.c:342
Definition: compat.h:1951
#define ATLASSERT(x)
Definition: CComVariant.cpp:9
CComVariant(long value, VARENUM type=VT_I4)
Definition: atlcomcli.h:490
uint16_t * PWSTR
Definition: typedefs.h:54
int other
Definition: msacm.c:1364
#define V_R8(A)
Definition: oleauto.h:262
CComBSTR(LPCOLESTR pSrc)
Definition: atlcomcli.h:267
#define V_I2(A)
Definition: oleauto.h:245
CComVariant(bool value)
Definition: atlcomcli.h:432
CComBSTR(int length)
Definition: atlcomcli.h:275
CComVariant(short value)
Definition: atlcomcli.h:450
HRESULT ChangeType(_In_ VARTYPE newType, _In_opt_ const LPVARIANT src=NULL)
Definition: atlcomcli.h:559
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
UINT WINAPI SysStringByteLen(BSTR str)
Definition: oleaut.c:218
CComVariant(char value)
Definition: atlcomcli.h:438
OLECHAR * BSTR
Definition: compat.h:1934
void WINAPI VariantInit(VARIANTARG *pVarg)
Definition: variant.c:571
CComVariant(LPCSTR lpStr)
Definition: atlcomcli.h:425
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 _In_opt_
Definition: no_sal2.h:213
#define V_I4(A)
Definition: oleauto.h:247
BSTR Detach()
Definition: atlcomcli.h:355
HRESULT Clear()
Definition: atlcomcli.h:549
const GUID * guid
Definition: compat.h:1896
unsigned int ByteLength() const
Definition: atlcomcli.h:393
CComBSTR(PCSTR pSrc)
Definition: atlcomcli.h:291
CComVariant(const CComVariant &other)
Definition: atlcomcli.h:408
CComVariant(unsigned long value)
Definition: atlcomcli.h:504
static PVOID ptr
Definition: dispmode.c:27
#define E_INVALIDARG
Definition: ddrawi.h:101
const WCHAR * str
HRESULT AtlHresultFromLastError()
Definition: atlcomcli.h:56
smooth NULL
Definition: ftsmooth.c:416
#define V_UI2(A)
Definition: oleauto.h:268
BSTR WINAPI SysAllocString(LPCOLESTR str)
Definition: oleaut.c:241
#define V_I1(A)
Definition: oleauto.h:243
CComQIIDPtr(_Inout_opt_ IUnknown *lp)
Definition: atlcomcli.h:206
const char * LPCSTR
Definition: xmlstorage.h:183
CComVariant(LPCOLESTR lpStr)
Definition: atlcomcli.h:419
CComBSTR(REFGUID guid)
Definition: atlcomcli.h:319
#define V_I8(A)
Definition: oleauto.h:249
int64_t LONGLONG
Definition: typedefs.h:66
CComVariant(BYTE value)
Definition: atlcomcli.h:444
Definition: rosdlgs.h:5
#define V_R4(A)
Definition: oleauto.h:260
CComPtr(const CComPtr< T > &lp)
Definition: atlcomcli.h:81
CComVariant(const CY &value)
Definition: atlcomcli.h:542
BSTR Copy() const
Definition: atlcomcli.h:362
LONG HRESULT
Definition: typedefs.h:77
GLenum GLuint GLenum GLsizei length
Definition: glext.h:5579
CComVariant(const LONGLONG &value)
Definition: atlcomcli.h:530
T * operator ->()
Definition: atlcomcli.h:176
uint64_t ULONGLONG
Definition: typedefs.h:65
CComVariant(unsigned int value, VARENUM type=VT_UI4)
Definition: atlcomcli.h:476
#define V_UI1(A)
Definition: oleauto.h:266
INT WINAPI StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
Definition: compobj.c:2343
#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:462
#define _Inout_
Definition: no_sal2.h:244
#define V_UI4(A)
Definition: oleauto.h:270
T ** operator &()
Definition: atlcomcli.h:165
T * operator=(T *lp)
Definition: atlcomcli.h:214
HRESULT WINAPI DECLSPEC_HOTPATCH VariantClear(VARIANTARG *pVarg)
Definition: variant.c:651
Definition: compat.h:1940
CComQIIDPtr(_Inout_opt_ T *lp)
Definition: atlcomcli.h:198
void Attach(BSTR bstr)
Definition: atlcomcli.h:349
#define V_VT(A)
Definition: oleauto.h:211
CComVariant(unsigned short value)
Definition: atlcomcli.h:456
GLenum GLsizei len
Definition: glext.h:6722
unsigned char BYTE
Definition: mem.h:68
UINT WINAPI SysStringLen(BSTR str)
Definition: oleaut.c:199
HRESULT Copy(_In_ const VARIANT *src)
Definition: atlcomcli.h:554
GLenum src
Definition: glext.h:6340
Definition: compat.h:1942
HRESULT CopyTo(BSTR *other) const
Definition: atlcomcli.h:369
GLsizei const GLfloat * value
Definition: glext.h:6069
#define V_BSTR(A)
Definition: oleauto.h:226
CComBSTR(int length, LPCOLESTR pSrc)
Definition: atlcomcli.h:283
#define _In_
Definition: no_sal2.h:204
CComVariant(double value, VARENUM type=VT_R8)
Definition: atlcomcli.h:516
CComPtr(T *lp)
Definition: atlcomcli.h:74
#define S_OK
Definition: intsafe.h:59
VARENUM
Definition: compat.h:1935
bool LoadString(HMODULE module, DWORD uID)
Definition: atlcomcli.h:377
static const WCHAR szGuid[]
Definition: rtlstr.c:1892
void Release()
Definition: atlcomcli.h:140
unsigned short VARTYPE
Definition: compat.h:1895
BSTR * operator &()
Definition: atlcomcli.h:337
void WINAPI DECLSPEC_HOTPATCH SysFreeString(BSTR str)
Definition: oleaut.c:274
#define V_ERROR(A)
Definition: oleauto.h:241
#define MultiByteToWideChar
Definition: compat.h:100
HRESULT QueryInterface([in] REFIID riid, [out, iid_is(riid)] void **ppvObject)
GLuint res
Definition: glext.h:9613
T * operator=(T *lp)
Definition: atlcomcli.h:94
CComBSTR & operator=(const CComBSTR &other)
Definition: atlcomcli.h:342
const char * PCSTR
Definition: typedefs.h:51
T * Detach()
Definition: atlcomcli.h:156
GLfloat GLfloat p
Definition: glext.h:8902
HRESULT WINAPI VariantCopy(VARIANTARG *pvargDest, VARIANTARG *pvargSrc)
Definition: variant.c:751
#define E_POINTER
Definition: winerror.h:2365
#define CP_THREAD_ACP
Definition: winnls.h:230
unsigned int Length() const
Definition: atlcomcli.h:388
Definition: compat.h:1941
void Attach(T *lp)
Definition: atlcomcli.h:149
Definition: compat.h:1938
T * operator=(IUnknown *lp)
Definition: atlcomcli.h:242
#define _Inout_opt_
Definition: no_sal2.h:258