ReactOS 0.4.15-dev-8119-g4fb2fdb
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
101 HRESULT CoCreateInstance(REFCLSID rclsid, REFIID riid, LPUNKNOWN pOuter = NULL, DWORD ClsCtx = CLSCTX_ALL)
102 {
103 ATLASSERT(!p);
104 return ::CoCreateInstance(rclsid, pOuter, ClsCtx, riid, (void**)&p);
105 }
106
107 HRESULT CoCreateInstance(LPCOLESTR ProgID, REFIID riid, LPUNKNOWN pOuter = NULL, DWORD ClsCtx = CLSCTX_ALL)
108 {
109 CLSID clsid;
110 HRESULT hr = CLSIDFromProgID(ProgID, &clsid);
111 return FAILED(hr) ? hr : CoCreateInstance(clsid, riid, pOuter, ClsCtx);
112 }
113
115 {
116 T* pOld = p;
117
118 p = lp;
119 if (p != NULL)
120 p->AddRef();
121
122 if (pOld != NULL)
123 pOld->Release();
124
125 return *this;
126 }
127
129 {
130 T* pOld = p;
131
132 p = lp.p;
133 if (p != NULL)
134 p->AddRef();
135
136 if (pOld != NULL)
137 pOld->Release();
138
139 return *this;
140 }
141
142 // We cannot enable this until gcc starts supporting __uuidof
143 // See CORE-12710
144#if 0
145 template <typename Q>
146 T* operator=(const CComPtr<Q>& lp)
147 {
148 T* pOld = p;
149
150 if (!lp.p || FAILED(lp.p->QueryInterface(__uuidof(T), (void**)(IUnknown**)&p)))
151 p = NULL;
152
153 if (pOld != NULL)
154 pOld->Release();
155
156 return *this;
157 }
158
159 HRESULT CoCreateInstance(REFCLSID rclsid, LPUNKNOWN pOuter = NULL, DWORD ClsCtx = CLSCTX_ALL)
160 {
161 return CoCreateInstance(rclsid, __uuidof(T), pOuter, ClsCtx);
162 }
163
164 HRESULT CoCreateInstance(LPCOLESTR ProgID, LPUNKNOWN pOuter = NULL, DWORD ClsCtx = CLSCTX_ALL)
165 {
166 return CoCreateInstance(ProgID, __uuidof(T), pOuter, ClsCtx);
167 }
168#endif
169
170 void Release()
171 {
172 if (p != NULL)
173 {
174 p->Release();
175 p = NULL;
176 }
177 }
178
179 void Attach(T *lp)
180 {
181 if (p != NULL)
182 p->Release();
183 p = lp;
184 }
185
187 {
188 T *saveP;
189
190 saveP = p;
191 p = NULL;
192 return saveP;
193 }
194
196 {
197 ATLASSERT(p == NULL);
198 return &p;
199 }
200
201 operator T * ()
202 {
203 return p;
204 }
205
207 {
208 ATLASSERT(p != NULL);
210 }
211};
212
213
214//CComQIIDPtr<I_ID(Itype)> is the gcc compatible version of CComQIPtr<Itype>
215#define I_ID(Itype) Itype,&IID_##Itype
216
217template <class T, const IID* piid>
219 public CComPtr<T>
220{
221public:
222 // Let's tell GCC how to find a symbol.
223 using CComPtr<T>::p;
224
226 {
227 }
229 CComPtr<T>(lp)
230 {
231 }
233 CComPtr<T>(lp.p)
234 {
235 }
237 {
238 if (lp != NULL)
239 {
240 if (FAILED(lp->QueryInterface(*piid, (void**)(IUnknown**)&p)))
241 p = NULL;
242 }
243 }
245 {
246 T* pOld = p;
247
248 p = lp;
249 if (p != NULL)
250 p->AddRef();
251
252 if (pOld != NULL)
253 pOld->Release();
254
255 return *this;
256 }
257
259 {
260 T* pOld = p;
261
262 p = lp.p;
263 if (p != NULL)
264 p->AddRef();
265
266 if (pOld != NULL)
267 pOld->Release();
268
269 return *this;
270 }
271
273 {
274 T* pOld = p;
275
276 if (!lp || FAILED(lp->QueryInterface(*piid, (void**)(IUnknown**)&p)))
277 p = NULL;
278
279 if (pOld != NULL)
280 pOld->Release();
281
282 return *this;
283 }
284};
285
286
288{
289public:
291public:
293 m_str(NULL)
294 {
295 }
296
297 CComBSTR(LPCOLESTR pSrc)
298 {
299 if (pSrc == NULL)
300 m_str = NULL;
301 else
302 m_str = ::SysAllocString(pSrc);
303 }
304
306 {
307 if (length == 0)
308 m_str = NULL;
309 else
311 }
312
313 CComBSTR(int length, LPCOLESTR pSrc)
314 {
315 if (length == 0)
316 m_str = NULL;
317 else
319 }
320
322 {
323 if (pSrc)
324 {
325 int len = MultiByteToWideChar(CP_THREAD_ACP, 0, pSrc, -1, NULL, 0);
327 if (m_str)
328 {
329 int res = MultiByteToWideChar(CP_THREAD_ACP, 0, pSrc, -1, m_str, len);
330 ATLASSERT(res == len);
331 if (res != len)
332 {
334 m_str = NULL;
335 }
336 }
337 }
338 else
339 {
340 m_str = NULL;
341 }
342 }
343
345 {
346 m_str = other.Copy();
347 }
348
350 {
351 OLECHAR szGuid[40];
354 }
355
357 {
359 m_str = NULL;
360 }
361
362 operator BSTR () const
363 {
364 return m_str;
365 }
366
368 {
369 return &m_str;
370 }
371
373 {
375 m_str = other.Copy();
376 return *this;
377 }
378
379 void Attach(BSTR bstr)
380 {
382 m_str = bstr;
383 }
384
386 {
387 BSTR str = m_str;
388 m_str = NULL;
389 return str;
390 }
391
392 BSTR Copy() const
393 {
394 if (!m_str)
395 return NULL;
396 return ::SysAllocStringLen(m_str, ::SysStringLen(m_str));
397 }
398
400 {
401 if (!other)
402 return E_POINTER;
403 *other = Copy();
404 return S_OK;
405 }
406
408 {
410 m_str = NULL;
411 const wchar_t *ptr = NULL;
412 int len = ::LoadStringW(module, uID, (PWSTR)&ptr, 0);
413 if (len)
415 return m_str != NULL;
416 }
417
418 unsigned int Length() const
419 {
420 return ::SysStringLen(m_str);
421 }
422
423 unsigned int ByteLength() const
424 {
425 return ::SysStringByteLen(m_str);
426 }
427};
428
429
431{
432public:
434 {
435 ::VariantInit(this);
436 }
437
439 {
440 V_VT(this) = VT_EMPTY;
441 Copy(&other);
442 }
443
445 {
446 Clear();
447 }
448
449 CComVariant(LPCOLESTR lpStr)
450 {
451 V_VT(this) = VT_BSTR;
452 V_BSTR(this) = ::SysAllocString(lpStr);
453 }
454
456 {
457 V_VT(this) = VT_BSTR;
458 CComBSTR str(lpStr);
459 V_BSTR(this) = str.Detach();
460 }
461
463 {
464 V_VT(this) = VT_BOOL;
465 V_BOOL(this) = value ? VARIANT_TRUE : VARIANT_FALSE;
466 }
467
469 {
470 V_VT(this) = VT_I1;
471 V_I1(this) = value;
472 }
473
475 {
476 V_VT(this) = VT_UI1;
477 V_UI1(this) = value;
478 }
479
481 {
482 V_VT(this) = VT_I2;
483 V_I2(this) = value;
484 }
485
486 CComVariant(unsigned short value)
487 {
488 V_VT(this) = VT_UI2;
489 V_UI2(this) = value;
490 }
491
493 {
494 if (type == VT_I4 || type == VT_INT)
495 {
496 V_VT(this) = type;
497 V_I4(this) = value;
498 }
499 else
500 {
501 V_VT(this) = VT_ERROR;
502 V_ERROR(this) = E_INVALIDARG;
503 }
504 }
505
507 {
508 if (type == VT_UI4 || type == VT_UINT)
509 {
510 V_VT(this) = type;
511 V_UI4(this) = value;
512 }
513 else
514 {
515 V_VT(this) = VT_ERROR;
516 V_ERROR(this) = E_INVALIDARG;
517 }
518 }
519
521 {
522 if (type == VT_I4 || type == VT_ERROR)
523 {
524 V_VT(this) = type;
525 V_I4(this) = value;
526 }
527 else
528 {
529 V_VT(this) = VT_ERROR;
530 V_ERROR(this) = E_INVALIDARG;
531 }
532 }
533
534 CComVariant(unsigned long value)
535 {
536 V_VT(this) = VT_UI4;
537 V_UI4(this) = value;
538 }
539
541 {
542 V_VT(this) = VT_R4;
543 V_R4(this) = value;
544 }
545
547 {
548 if (type == VT_R8 || type == VT_DATE)
549 {
550 V_VT(this) = type;
551 V_R8(this) = value;
552 }
553 else
554 {
555 V_VT(this) = VT_ERROR;
556 V_ERROR(this) = E_INVALIDARG;
557 }
558 }
559
561 {
562 V_VT(this) = VT_I8;
563 V_I8(this) = value;
564 }
565
567 {
568 V_VT(this) = VT_UI8;
569 V_UI8(this) = value;
570 }
571
573 {
574 V_VT(this) = VT_CY;
575 V_I8(this) = value.int64;
576 }
577
578
580 {
581 return ::VariantClear(this);
582 }
583
585 {
586 return ::VariantCopy(this, const_cast<VARIANT*>(src));
587 }
588
590 {
591 const LPVARIANT lpSrc = src ? src : this;
592 return ::VariantChangeType(this, lpSrc, 0, newType);
593 }
594};
595
596
597
598}; // namespace ATL
599
600#ifndef _ATL_NO_AUTOMATIC_NAMESPACE
601using namespace ATL;
602#endif
603
#define ATLASSERT(x)
Definition: CComVariant.cpp:10
#define STDMETHODCALLTYPE
Definition: bdasup.h:9
CComBSTR(int length)
Definition: atlcomcli.h:305
CComBSTR & operator=(const CComBSTR &other)
Definition: atlcomcli.h:372
CComBSTR(PCSTR pSrc)
Definition: atlcomcli.h:321
BSTR Detach()
Definition: atlcomcli.h:385
void Attach(BSTR bstr)
Definition: atlcomcli.h:379
BSTR * operator&()
Definition: atlcomcli.h:367
unsigned int Length() const
Definition: atlcomcli.h:418
HRESULT CopyTo(BSTR *other) const
Definition: atlcomcli.h:399
CComBSTR(int length, LPCOLESTR pSrc)
Definition: atlcomcli.h:313
BSTR Copy() const
Definition: atlcomcli.h:392
CComBSTR(LPCOLESTR pSrc)
Definition: atlcomcli.h:297
unsigned int ByteLength() const
Definition: atlcomcli.h:423
CComBSTR(const CComBSTR &other)
Definition: atlcomcli.h:344
CComBSTR(REFGUID guid)
Definition: atlcomcli.h:349
bool LoadString(HMODULE module, DWORD uID)
Definition: atlcomcli.h:407
HRESULT CoCreateInstance(REFCLSID rclsid, REFIID riid, LPUNKNOWN pOuter=NULL, DWORD ClsCtx=CLSCTX_ALL)
Definition: atlcomcli.h:101
CComPtr(const CComPtr< T > &lp)
Definition: atlcomcli.h:88
_NoAddRefReleaseOnCComPtr< T > * operator->() const
Definition: atlcomcli.h:206
T * operator=(T *lp)
Definition: atlcomcli.h:114
HRESULT CoCreateInstance(LPCOLESTR ProgID, REFIID riid, LPUNKNOWN pOuter=NULL, DWORD ClsCtx=CLSCTX_ALL)
Definition: atlcomcli.h:107
T ** operator&()
Definition: atlcomcli.h:195
void Release()
Definition: atlcomcli.h:170
void Attach(T *lp)
Definition: atlcomcli.h:179
CComPtr(T *lp)
Definition: atlcomcli.h:81
T * Detach()
Definition: atlcomcli.h:186
T * operator=(IUnknown *lp)
Definition: atlcomcli.h:272
CComQIIDPtr(_Inout_opt_ T *lp)
Definition: atlcomcli.h:228
CComQIIDPtr(_Inout_ const CComQIIDPtr< T, piid > &lp)
Definition: atlcomcli.h:232
CComQIIDPtr(_Inout_opt_ IUnknown *lp)
Definition: atlcomcli.h:236
T * operator=(T *lp)
Definition: atlcomcli.h:244
CComVariant(const LONGLONG &value)
Definition: atlcomcli.h:560
CComVariant(const ULONGLONG &value)
Definition: atlcomcli.h:566
CComVariant(const CY &value)
Definition: atlcomcli.h:572
CComVariant(short value)
Definition: atlcomcli.h:480
CComVariant(int value, VARENUM type=VT_I4)
Definition: atlcomcli.h:492
CComVariant(bool value)
Definition: atlcomcli.h:462
CComVariant(long value, VARENUM type=VT_I4)
Definition: atlcomcli.h:520
CComVariant(BYTE value)
Definition: atlcomcli.h:474
CComVariant(unsigned long value)
Definition: atlcomcli.h:534
CComVariant(LPCSTR lpStr)
Definition: atlcomcli.h:455
CComVariant(LPCOLESTR lpStr)
Definition: atlcomcli.h:449
HRESULT ChangeType(_In_ VARTYPE newType, _In_opt_ const LPVARIANT src=NULL)
Definition: atlcomcli.h:589
CComVariant(char value)
Definition: atlcomcli.h:468
HRESULT Clear()
Definition: atlcomcli.h:579
CComVariant(float value)
Definition: atlcomcli.h:540
CComVariant(double value, VARENUM type=VT_R8)
Definition: atlcomcli.h:546
CComVariant(unsigned short value)
Definition: atlcomcli.h:486
CComVariant(const CComVariant &other)
Definition: atlcomcli.h:438
HRESULT Copy(_In_ const VARIANT *src)
Definition: atlcomcli.h:584
CComVariant(unsigned int value, VARENUM type=VT_UI4)
Definition: atlcomcli.h:506
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
HRESULT WINAPI DECLSPEC_HOTPATCH CLSIDFromProgID(LPCOLESTR progid, LPCLSID clsid)
Definition: compobj.c:2602
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
REFCLSID clsid
Definition: msctf.c:82
Definition: rosdlgs.h:6
HRESULT AtlHresultFromLastError() noexcept
Definition: atlcomcli.h:56
REFIID riid
Definition: atlbase.h:83
static LPUNKNOWN
Definition: ndr_ole.c:49
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
#define REFIID
Definition: guiddef.h:118
#define REFCLSID
Definition: guiddef.h:117
const WCHAR * str
HRESULT hr
Definition: shlfolder.c:183
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:1042
#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