ReactOS 0.4.15-dev-5895-g2687c1b
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
53namespace ATL
54{
55
57{
58 DWORD dwError = ::GetLastError();
59 return HRESULT_FROM_WIN32(dwError);
60}
61
62template <class T>
64{
65 private:
68};
69
70template<class T>
72{
73public:
74 T *p;
75public:
77 {
78 p = NULL;
79 }
80
81 CComPtr(T *lp)
82 {
83 p = lp;
84 if (p != NULL)
85 p->AddRef();
86 }
87
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
194template <class T, const IID* piid>
196 public CComPtr<T>
197{
198public:
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
265{
266public:
268public:
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;
373 return ::SysAllocStringLen(m_str, ::SysStringLen(m_str));
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 {
397 return ::SysStringLen(m_str);
398 }
399
400 unsigned int ByteLength() const
401 {
402 return ::SysStringByteLen(m_str);
403 }
404};
405
406
408{
409public:
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
578using namespace ATL;
579#endif
580
#define ATLASSERT(x)
Definition: CComVariant.cpp:10
#define STDMETHODCALLTYPE
Definition: bdasup.h:9
CComBSTR(int length)
Definition: atlcomcli.h:282
CComBSTR & operator=(const CComBSTR &other)
Definition: atlcomcli.h:349
CComBSTR(PCSTR pSrc)
Definition: atlcomcli.h:298
BSTR Detach()
Definition: atlcomcli.h:362
void Attach(BSTR bstr)
Definition: atlcomcli.h:356
BSTR * operator&()
Definition: atlcomcli.h:344
unsigned int Length() const
Definition: atlcomcli.h:395
HRESULT CopyTo(BSTR *other) const
Definition: atlcomcli.h:376
CComBSTR(int length, LPCOLESTR pSrc)
Definition: atlcomcli.h:290
BSTR Copy() const
Definition: atlcomcli.h:369
CComBSTR(LPCOLESTR pSrc)
Definition: atlcomcli.h:274
unsigned int ByteLength() const
Definition: atlcomcli.h:400
CComBSTR(const CComBSTR &other)
Definition: atlcomcli.h:321
CComBSTR(REFGUID guid)
Definition: atlcomcli.h:326
bool LoadString(HMODULE module, DWORD uID)
Definition: atlcomcli.h:384
CComPtr(const CComPtr< T > &lp)
Definition: atlcomcli.h:88
_NoAddRefReleaseOnCComPtr< T > * operator->() const
Definition: atlcomcli.h:183
T * operator=(T *lp)
Definition: atlcomcli.h:101
T ** operator&()
Definition: atlcomcli.h:172
void Release()
Definition: atlcomcli.h:147
void Attach(T *lp)
Definition: atlcomcli.h:156
CComPtr(T *lp)
Definition: atlcomcli.h:81
T * Detach()
Definition: atlcomcli.h:163
T * operator=(IUnknown *lp)
Definition: atlcomcli.h:249
CComQIIDPtr(_Inout_opt_ T *lp)
Definition: atlcomcli.h:205
CComQIIDPtr(_Inout_ const CComQIIDPtr< T, piid > &lp)
Definition: atlcomcli.h:209
CComQIIDPtr(_Inout_opt_ IUnknown *lp)
Definition: atlcomcli.h:213
T * operator=(T *lp)
Definition: atlcomcli.h:221
CComVariant(const LONGLONG &value)
Definition: atlcomcli.h:537
CComVariant(const ULONGLONG &value)
Definition: atlcomcli.h:543
CComVariant(const CY &value)
Definition: atlcomcli.h:549
CComVariant(short value)
Definition: atlcomcli.h:457
CComVariant(int value, VARENUM type=VT_I4)
Definition: atlcomcli.h:469
CComVariant(bool value)
Definition: atlcomcli.h:439
CComVariant(long value, VARENUM type=VT_I4)
Definition: atlcomcli.h:497
CComVariant(BYTE value)
Definition: atlcomcli.h:451
CComVariant(unsigned long value)
Definition: atlcomcli.h:511
CComVariant(LPCSTR lpStr)
Definition: atlcomcli.h:432
CComVariant(LPCOLESTR lpStr)
Definition: atlcomcli.h:426
HRESULT ChangeType(_In_ VARTYPE newType, _In_opt_ const LPVARIANT src=NULL)
Definition: atlcomcli.h:566
CComVariant(char value)
Definition: atlcomcli.h:445
HRESULT Clear()
Definition: atlcomcli.h:556
CComVariant(float value)
Definition: atlcomcli.h:517
CComVariant(double value, VARENUM type=VT_R8)
Definition: atlcomcli.h:523
CComVariant(unsigned short value)
Definition: atlcomcli.h:463
CComVariant(const CComVariant &other)
Definition: atlcomcli.h:415
HRESULT Copy(_In_ const VARIANT *src)
Definition: atlcomcli.h:561
CComVariant(unsigned int value, VARENUM type=VT_UI4)
Definition: atlcomcli.h:483
virtual ULONG STDMETHODCALLTYPE AddRef()=0
virtual ULONG STDMETHODCALLTYPE Release()=0
#define E_INVALIDARG
Definition: ddrawi.h:101
#define NULL
Definition: types.h:112
WCHAR OLECHAR
Definition: compat.h:2292
OLECHAR * BSTR
Definition: compat.h:2293
unsigned short VARTYPE
Definition: compat.h:2254
#define MultiByteToWideChar
Definition: compat.h:110
VARENUM
Definition: compat.h:2294
@ VT_UI8
Definition: compat.h:2315
@ VT_BSTR
Definition: compat.h:2303
@ VT_INT
Definition: compat.h:2316
@ VT_R4
Definition: compat.h:2299
@ VT_UI2
Definition: compat.h:2312
@ VT_ERROR
Definition: compat.h:2305
@ VT_R8
Definition: compat.h:2300
@ VT_CY
Definition: compat.h:2301
@ VT_I8
Definition: compat.h:2314
@ VT_I1
Definition: compat.h:2310
@ VT_I4
Definition: compat.h:2298
@ VT_DATE
Definition: compat.h:2302
@ VT_BOOL
Definition: compat.h:2306
@ VT_I2
Definition: compat.h:2297
@ VT_UI4
Definition: compat.h:2313
@ VT_UINT
Definition: compat.h:2317
@ VT_EMPTY
Definition: compat.h:2295
@ VT_UI1
Definition: compat.h:2311
INT WINAPI StringFromGUID2(REFGUID id, LPOLESTR str, INT cmax)
Definition: compobj.c:2434
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLuint res
Definition: glext.h:9613
GLenum src
Definition: glext.h:6340
GLuint GLsizei GLsizei * length
Definition: glext.h:6040
GLfloat GLfloat p
Definition: glext.h:8902
GLenum GLsizei len
Definition: glext.h:6722
HRESULT QueryInterface([in] REFIID riid, [out, iid_is(riid)] void **ppvObject)
nsrefcnt Release()
#define S_OK
Definition: intsafe.h:52
#define FAILED(hr)
Definition: intsafe.h:51
#define T
Definition: mbstring.h:31
const GUID * guid
static PVOID ptr
Definition: dispmode.c:27
static const WCHAR szGuid[]
Definition: rtlstr.c:1892
#define _Inout_
Definition: ms_sal.h:378
#define _Inout_opt_
Definition: ms_sal.h:379
#define _In_
Definition: ms_sal.h:308
#define _In_opt_
Definition: ms_sal.h:309
int other
Definition: msacm.c:1376
Definition: rosdlgs.h:6
HRESULT AtlHresultFromLastError()
Definition: atlcomcli.h:56
BSTR WINAPI SysAllocString(LPCOLESTR str)
Definition: oleaut.c:238
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 V_ERROR(A)
Definition: oleauto.h:241
#define V_UI1(A)
Definition: oleauto.h:266
#define V_I8(A)
Definition: oleauto.h:249
#define V_BOOL(A)
Definition: oleauto.h:224
#define V_UI2(A)
Definition: oleauto.h:268
#define V_I1(A)
Definition: oleauto.h:243
#define V_VT(A)
Definition: oleauto.h:211
#define V_BSTR(A)
Definition: oleauto.h:226
#define V_I4(A)
Definition: oleauto.h:247
#define V_R4(A)
Definition: oleauto.h:260
#define V_UI4(A)
Definition: oleauto.h:270
#define V_R8(A)
Definition: oleauto.h:262
#define V_UI8(A)
Definition: oleauto.h:272
#define V_I2(A)
Definition: oleauto.h:245
const WCHAR * str
Definition: scsiwmi.h:51
uint16_t * PWSTR
Definition: typedefs.h:56
int64_t LONGLONG
Definition: typedefs.h:68
const char * PCSTR
Definition: typedefs.h:52
uint32_t ULONG
Definition: typedefs.h:59
uint64_t ULONGLONG
Definition: typedefs.h:67
Definition: compat.h:2255
Definition: pdh_main.c:94
void WINAPI VariantInit(VARIANTARG *pVarg)
Definition: variant.c:568
DWORD WINAPI GetLastError(void)
Definition: except.c:1040
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92
#define E_POINTER
Definition: winerror.h:2365
#define CP_THREAD_ACP
Definition: winnls.h:233
int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_writes_to_(cchBufferMax, return+1) LPWSTR lpBuffer, _In_ int cchBufferMax)
const char * LPCSTR
Definition: xmlstorage.h:183
unsigned char BYTE
Definition: xxhash.c:193