-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathatlassert.h
5639 lines (5293 loc) · 148 KB
/
atlassert.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This is a part of the Active Template Library.
// Copyright (C) 1996-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Active Template Library Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Active Template Library product.
#ifndef __ATLBASE_H__
#define __ATLBASE_H__
#ifndef __cplusplus
#error ATL requires C++ compilation (use a .cpp suffix)
#endif
#ifndef _ATL_NO_PRAGMA_WARNINGS
#pragma warning(disable : 4201) // nameless unions are part of C++
#pragma warning(disable : 4127) // constant expression
#pragma warning(disable : 4505) // unreferenced local function has been removed
#pragma warning( \
disable : 4512) // can't generate assignment operator (so what?)
#pragma warning(disable : 4514) // unreferenced inlines are common
#pragma warning(disable : 4103) // pragma pack
#pragma warning(disable : 4702) // unreachable code
#pragma warning(disable : 4237) // bool
#pragma warning(disable : 4710) // function couldn't be inlined
#pragma warning( \
disable : 4355) // 'this' : used in base member initializer list
#pragma warning(disable : 4097) // typedef name used as synonym for class-name
#pragma warning( \
disable : 4786) // identifier was truncated in the debug information
#pragma warning( \
disable : 4268) // const static/global data initialized to zeros
#pragma warning(disable : 4291) // allow placement new
#endif //!_ATL_NO_PRAGMA_WARNINGS
#include <atldef.h>
#include <ole2.h>
#include <windows.h>
#include <winnls.h>
#include <comcat.h>
#include <stddef.h>
#ifdef new
#pragma push_macro("new")
#define _ATL_REDEF_NEW
#undef new
#endif
#include <malloc.h>
#include <tchar.h>
#ifndef _ATL_NO_DEBUG_CRT
// Warning: if you define the above symbol, you will have
// to provide your own definition of the ATLASSERT(x) macro
// in order to compile ATL
#include <crtdbg.h>
#endif
#include <atliface.h>
#include <olectl.h>
#include <winreg.h>
#ifdef _DEBUG
#include <stdarg.h>
#include <stdio.h>
#endif
#include <atlconv.h>
#include <shlwapi.h>
#pragma pack(push, _ATL_PACKING)
#if defined(_ATL_DLL)
#pragma comment(lib, "atl.lib")
#endif
extern "C" const __declspec(selectany) GUID LIBID_ATLLib = {
0x44EC0535,
0x400F,
0x11D0,
{0x9D, 0xCD, 0x00, 0xA0, 0xC9, 0x03, 0x91, 0xD3}};
extern "C" const __declspec(selectany) CLSID CLSID_Registrar = {
0x44EC053A,
0x400F,
0x11D0,
{0x9D, 0xCD, 0x00, 0xA0, 0xC9, 0x03, 0x91, 0xD3}};
extern "C" const __declspec(selectany) IID IID_IRegistrar = {
0x44EC053B,
0x400F,
0x11D0,
{0x9D, 0xCD, 0x00, 0xA0, 0xC9, 0x03, 0x91, 0xD3}};
extern "C" const __declspec(selectany) IID IID_IAxWinHostWindow = {
0xb6ea2050, 0x48a, 0x11d1, {0x82, 0xb9, 0x0, 0xc0, 0x4f, 0xb9, 0x94, 0x2e}};
extern "C" const __declspec(selectany) IID IID_IAxWinAmbientDispatch = {
0xb6ea2051, 0x48a, 0x11d1, {0x82, 0xb9, 0x0, 0xc0, 0x4f, 0xb9, 0x94, 0x2e}};
extern "C" const __declspec(selectany) IID IID_IInternalConnection = {
0x72AD0770,
0x6A9F,
0x11d1,
{0xBC, 0xEC, 0x00, 0x60, 0x08, 0x8F, 0x44, 0x4E}};
extern "C" const __declspec(selectany) IID IID_IDocHostUIHandlerDispatch = {
0x425B5AF0,
0x65F1,
0x11d1,
{0x96, 0x11, 0x00, 0x00, 0xF8, 0x1E, 0x0D, 0x0D}};
#ifndef _ATL_DLL_IMPL
namespace ATL {
#endif
struct _ATL_CATMAP_ENTRY {
int iType;
const CATID* pcatid;
};
#define _ATL_CATMAP_ENTRY_END 0
#define _ATL_CATMAP_ENTRY_IMPLEMENTED 1
#define _ATL_CATMAP_ENTRY_REQUIRED 2
typedef HRESULT(WINAPI _ATL_CREATORFUNC)(void* pv, REFIID riid, LPVOID* ppv);
typedef HRESULT(WINAPI _ATL_CREATORARGFUNC)(void* pv, REFIID riid, LPVOID* ppv,
DWORD dw);
typedef HRESULT(WINAPI _ATL_MODULEFUNC)(DWORD dw);
typedef LPCTSTR(WINAPI _ATL_DESCRIPTIONFUNC)();
typedef const struct _ATL_CATMAP_ENTRY*(_ATL_CATMAPFUNC)();
typedef void(__stdcall _ATL_TERMFUNC)(DWORD dw);
struct _ATL_TERMFUNC_ELEM {
_ATL_TERMFUNC* pFunc;
DWORD dw;
_ATL_TERMFUNC_ELEM* pNext;
};
struct _ATL_OBJMAP_ENTRY {
const CLSID* pclsid;
HRESULT(WINAPI* pfnUpdateRegistry)(BOOL bRegister);
_ATL_CREATORFUNC* pfnGetClassObject;
_ATL_CREATORFUNC* pfnCreateInstance;
IUnknown* pCF;
DWORD dwRegister;
_ATL_DESCRIPTIONFUNC* pfnGetObjectDescription;
_ATL_CATMAPFUNC* pfnGetCategoryMap;
HRESULT WINAPI RevokeClassObject() { return CoRevokeClassObject(dwRegister); }
HRESULT WINAPI RegisterClassObject(DWORD dwClsContext, DWORD dwFlags) {
IUnknown* p = NULL;
if (pfnGetClassObject == NULL) return S_OK;
HRESULT hRes =
pfnGetClassObject(pfnCreateInstance, IID_IUnknown, (LPVOID*)&p);
if (SUCCEEDED(hRes))
hRes =
CoRegisterClassObject(*pclsid, p, dwClsContext, dwFlags, &dwRegister);
if (p != NULL) p->Release();
return hRes;
}
// Added in ATL 3.0
void(WINAPI* pfnObjectMain)(bool bStarting);
};
struct _ATL_REGMAP_ENTRY {
LPCOLESTR szKey;
LPCOLESTR szData;
};
struct _AtlCreateWndData {
void* m_pThis;
DWORD m_dwThreadID;
_AtlCreateWndData* m_pNext;
};
struct _ATL_MODULE {
// Attributes
public:
UINT cbSize;
HINSTANCE m_hInst;
HINSTANCE m_hInstResource;
HINSTANCE m_hInstTypeLib;
_ATL_OBJMAP_ENTRY* m_pObjMap;
LONG m_nLockCnt;
HANDLE m_hHeap;
union {
CRITICAL_SECTION m_csTypeInfoHolder;
CRITICAL_SECTION m_csStaticDataInit;
};
CRITICAL_SECTION m_csWindowCreate;
CRITICAL_SECTION m_csObjMap;
// Original Size = 100
// Stuff added in ATL 3.0
DWORD dwAtlBuildVer;
_AtlCreateWndData* m_pCreateWndList;
bool m_bDestroyHeap;
GUID* pguidVer;
DWORD m_dwHeaps; // Number of heaps we have (-1)
HANDLE* m_phHeaps;
int m_nHeap; // Which heap to choose from
_ATL_TERMFUNC_ELEM* m_pTermFuncs;
};
const int _nAtlModuleVer1Size = 100;
// This define makes debugging asserts easier.
#define _ATL_SIMPLEMAPENTRY ((_ATL_CREATORARGFUNC*)1)
struct _ATL_INTMAP_ENTRY {
const IID* piid; // the interface id (IID)
DWORD dw;
_ATL_CREATORARGFUNC* pFunc; // NULL:end, 1:offset, n:ptr
};
/////////////////////////////////////////////////////////////////////////////
// QI Support
ATLAPI AtlInternalQueryInterface(void* pThis, const _ATL_INTMAP_ENTRY* pEntries,
REFIID iid, void** ppvObject);
/////////////////////////////////////////////////////////////////////////////
// Smart Pointer helpers
ATLAPI_(IUnknown*) AtlComPtrAssign(IUnknown** pp, IUnknown* lp);
ATLAPI_(IUnknown*) AtlComQIPtrAssign(IUnknown** pp, IUnknown* lp, REFIID riid);
/////////////////////////////////////////////////////////////////////////////
// Inproc Marshaling helpers
ATLAPI AtlFreeMarshalStream(IStream* pStream);
ATLAPI AtlMarshalPtrInProc(IUnknown* pUnk, const IID& iid, IStream** ppStream);
ATLAPI AtlUnmarshalPtr(IStream* pStream, const IID& iid, IUnknown** ppUnk);
ATLAPI_(BOOL) AtlWaitWithMessageLoop(HANDLE hEvent);
/////////////////////////////////////////////////////////////////////////////
// Connection Point Helpers
ATLAPI AtlAdvise(IUnknown* pUnkCP, IUnknown* pUnk, const IID& iid, LPDWORD pdw);
ATLAPI AtlUnadvise(IUnknown* pUnkCP, const IID& iid, DWORD dw);
/////////////////////////////////////////////////////////////////////////////
// IDispatch Error handling
ATLAPI AtlSetErrorInfo(const CLSID& clsid, LPCOLESTR lpszDesc, DWORD dwHelpID,
LPCOLESTR lpszHelpFile, const IID& iid, HRESULT hRes,
HINSTANCE hInst);
/////////////////////////////////////////////////////////////////////////////
// Module
ATLAPI AtlModuleRegisterClassObjects(_ATL_MODULE* pM, DWORD dwClsContext,
DWORD dwFlags);
ATLAPI AtlModuleRevokeClassObjects(_ATL_MODULE* pM);
ATLAPI AtlModuleGetClassObject(_ATL_MODULE* pM, REFCLSID rclsid, REFIID riid,
LPVOID* ppv);
ATLAPI AtlModuleRegisterServer(_ATL_MODULE* pM, BOOL bRegTypeLib,
const CLSID* pCLSID = NULL);
ATLAPI AtlModuleUnregisterServer(_ATL_MODULE* pM, const CLSID* pCLSID = NULL);
ATLAPI AtlModuleUnregisterServerEx(_ATL_MODULE* pM, BOOL bUnRegTypeLib,
const CLSID* pCLSID = NULL);
ATLAPI AtlModuleUpdateRegistryFromResourceD(
_ATL_MODULE* pM, LPCOLESTR lpszRes, BOOL bRegister,
struct _ATL_REGMAP_ENTRY* pMapEntries, IRegistrar* pReg = NULL);
ATLAPI AtlModuleRegisterTypeLib(_ATL_MODULE* pM, LPCOLESTR lpszIndex);
ATLAPI AtlModuleUnRegisterTypeLib(_ATL_MODULE* pM, LPCOLESTR lpszIndex);
ATLAPI AtlModuleLoadTypeLib(_ATL_MODULE* pM, LPCOLESTR lpszIndex,
BSTR* pbstrPath, ITypeLib** ppTypeLib);
ATLAPI AtlModuleInit(_ATL_MODULE* pM, _ATL_OBJMAP_ENTRY* p, HINSTANCE h);
ATLAPI AtlModuleTerm(_ATL_MODULE* pM);
ATLAPI_(DWORD) AtlGetVersion(void* pReserved);
ATLAPI_(void)
AtlModuleAddCreateWndData(_ATL_MODULE* pM, _AtlCreateWndData* pData,
void* pObject);
ATLAPI_(void*) AtlModuleExtractCreateWndData(_ATL_MODULE* pM);
ATLAPI AtlModuleAddTermFunc(_ATL_MODULE* pM, _ATL_TERMFUNC* pFunc, DWORD dw);
#ifndef _ATL_DLL_IMPL
}; // namespace ATL
#endif
namespace ATL {
enum atlTraceFlags {
// Application defined categories
atlTraceUser = 0x00000001,
atlTraceUser2 = 0x00000002,
atlTraceUser3 = 0x00000004,
atlTraceUser4 = 0x00000008,
// ATL defined categories
atlTraceGeneral = 0x00000020,
atlTraceCOM = 0x00000040,
atlTraceQI = 0x00000080,
atlTraceRegistrar = 0x00000100,
atlTraceRefcount = 0x00000200,
atlTraceWindowing = 0x00000400,
atlTraceControls = 0x00000800,
atlTraceHosting = 0x00001000,
atlTraceDBClient = 0x00002000,
atlTraceDBProvider = 0x00004000,
atlTraceSnapin = 0x00008000,
atlTraceNotImpl = 0x00010000,
};
#ifndef ATL_TRACE_CATEGORY
#define ATL_TRACE_CATEGORY 0xFFFFFFFF
#endif
#ifdef _DEBUG
#ifndef ATL_TRACE_LEVEL
#define ATL_TRACE_LEVEL 0
#endif
inline void _cdecl AtlTrace(LPCSTR lpszFormat, ...) {
va_list args;
va_start(args, lpszFormat);
int nBuf;
char szBuffer[512];
nBuf = _vsnprintf(szBuffer, sizeof(szBuffer), lpszFormat, args);
ATLASSERT(nBuf <
sizeof(szBuffer)); // Output truncated as it was > sizeof(szBuffer)
OutputDebugStringA(szBuffer);
va_end(args);
}
inline void _cdecl AtlTrace2(DWORD category, UINT level, LPCSTR lpszFormat,
...) {
if (category & ATL_TRACE_CATEGORY && level <= ATL_TRACE_LEVEL) {
va_list args;
va_start(args, lpszFormat);
int nBuf;
char szBuffer[512];
nBuf = _vsnprintf(szBuffer, sizeof(szBuffer), lpszFormat, args);
ATLASSERT(
nBuf <
sizeof(szBuffer)); // Output truncated as it was > sizeof(szBuffer)
OutputDebugStringA("ATL: ");
OutputDebugStringA(szBuffer);
va_end(args);
}
}
#ifndef OLE2ANSI
inline void _cdecl AtlTrace(LPCWSTR lpszFormat, ...) {
va_list args;
va_start(args, lpszFormat);
int nBuf;
WCHAR szBuffer[512];
nBuf =
_vsnwprintf(szBuffer, sizeof(szBuffer) / sizeof(WCHAR), lpszFormat, args);
ATLASSERT(nBuf <
sizeof(szBuffer)); // Output truncated as it was > sizeof(szBuffer)
OutputDebugStringW(szBuffer);
va_end(args);
}
inline void _cdecl AtlTrace2(DWORD category, UINT level, LPCWSTR lpszFormat,
...) {
if (category & ATL_TRACE_CATEGORY && level <= ATL_TRACE_LEVEL) {
va_list args;
va_start(args, lpszFormat);
int nBuf;
WCHAR szBuffer[512];
nBuf = _vsnwprintf(szBuffer, sizeof(szBuffer) / sizeof(WCHAR), lpszFormat,
args);
ATLASSERT(
nBuf <
sizeof(szBuffer)); // Output truncated as it was > sizeof(szBuffer)
OutputDebugStringW(L"ATL: ");
OutputDebugStringW(szBuffer);
va_end(args);
}
}
#endif //! OLE2ANSI
#ifndef ATLTRACE
#define ATLTRACE AtlTrace
#define ATLTRACE2 AtlTrace2
#endif
#define ATLTRACENOTIMPL(funcname) \
ATLTRACE2(atlTraceNotImpl, 2, _T("ATL: %s not implemented.\n"), funcname); \
return E_NOTIMPL
#else // !DEBUG
inline void _cdecl AtlTrace(LPCSTR, ...) {}
inline void _cdecl AtlTrace2(DWORD, UINT, LPCSTR, ...) {}
#ifndef OLE2ANSI
inline void _cdecl AtlTrace(LPCWSTR, ...) {}
inline void _cdecl AtlTrace2(DWORD, UINT, LPCWSTR, ...) {}
#endif // OLE2ANSI
#ifndef ATLTRACE
#define ATLTRACE 1 ? (void)0 : AtlTrace
#define ATLTRACE2 1 ? (void)0 : AtlTrace2
#endif // ATLTRACE
#define ATLTRACENOTIMPL(funcname) return E_NOTIMPL
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// Win32 libraries
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "olepro32.lib")
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
#pragma comment(lib, "uuid.lib")
static HRESULT AtlSetChildSite(IUnknown* punkChild, IUnknown* punkParent) {
if (punkChild == NULL) return E_POINTER;
HRESULT hr;
IObjectWithSite* pChildSite = NULL;
hr = punkChild->QueryInterface(IID_IObjectWithSite, (void**)&pChildSite);
if (SUCCEEDED(hr) && pChildSite != NULL) {
hr = pChildSite->SetSite(punkParent);
pChildSite->Release();
}
return hr;
}
template <class T>
class _NoAddRefReleaseOnCComPtr : public T {
private:
STDMETHOD_(ULONG, AddRef)() = 0;
STDMETHOD_(ULONG, Release)() = 0;
};
template <class T>
class CComPtr {
public:
typedef T _PtrClass;
CComPtr() { p = NULL; }
CComPtr(T* lp) {
if ((p = lp) != NULL) p->AddRef();
}
CComPtr(const CComPtr<T>& lp) {
if ((p = lp.p) != NULL) p->AddRef();
}
~CComPtr() {
if (p) p->Release();
}
void Release() {
IUnknown* pTemp = p;
if (pTemp) {
p = NULL;
pTemp->Release();
}
}
operator T*() const { return (T*)p; }
T& operator*() const {
ATLASSERT(p != NULL);
return *p;
}
// The assert on operator& usually indicates a bug. If this is really
// what is needed, however, take the address of the p member explicitly.
T** operator&() {
ATLASSERT(p == NULL);
return &p;
}
_NoAddRefReleaseOnCComPtr<T>* operator->() const {
ATLASSERT(p != NULL);
return (_NoAddRefReleaseOnCComPtr<T>*)p;
}
T* operator=(T* lp) { return (T*)AtlComPtrAssign((IUnknown**)&p, lp); }
T* operator=(const CComPtr<T>& lp) {
return (T*)AtlComPtrAssign((IUnknown**)&p, lp.p);
}
bool operator!() const { return (p == NULL); }
bool operator<(T* pT) const { return p < pT; }
bool operator==(T* pT) const { return p == pT; }
// Compare two objects for equivalence
bool IsEqualObject(IUnknown* pOther) {
if (p == NULL && pOther == NULL) return true; // They are both NULL objects
if (p == NULL || pOther == NULL)
return false; // One is NULL the other is not
CComPtr<IUnknown> punk1;
CComPtr<IUnknown> punk2;
p->QueryInterface(IID_IUnknown, (void**)&punk1);
pOther->QueryInterface(IID_IUnknown, (void**)&punk2);
return punk1 == punk2;
}
void Attach(T* p2) {
if (p) p->Release();
p = p2;
}
T* Detach() {
T* pt = p;
p = NULL;
return pt;
}
HRESULT CopyTo(T** ppT) {
ATLASSERT(ppT != NULL);
if (ppT == NULL) return E_POINTER;
*ppT = p;
if (p) p->AddRef();
return S_OK;
}
HRESULT SetSite(IUnknown* punkParent) {
return AtlSetChildSite(p, punkParent);
}
HRESULT Advise(IUnknown* pUnk, const IID& iid, LPDWORD pdw) {
return AtlAdvise(p, pUnk, iid, pdw);
}
HRESULT CoCreateInstance(REFCLSID rclsid, LPUNKNOWN pUnkOuter = NULL,
DWORD dwClsContext = CLSCTX_ALL) {
ATLASSERT(p == NULL);
return ::CoCreateInstance(rclsid, pUnkOuter, dwClsContext, __uuidof(T),
(void**)&p);
}
HRESULT CoCreateInstance(LPCOLESTR szProgID, LPUNKNOWN pUnkOuter = NULL,
DWORD dwClsContext = CLSCTX_ALL) {
CLSID clsid;
HRESULT hr = CLSIDFromProgID(szProgID, &clsid);
ATLASSERT(p == NULL);
if (SUCCEEDED(hr))
hr = ::CoCreateInstance(clsid, pUnkOuter, dwClsContext, __uuidof(T),
(void**)&p);
return hr;
}
template <class Q>
HRESULT QueryInterface(Q** pp) const {
ATLASSERT(pp != NULL && *pp == NULL);
return p->QueryInterface(__uuidof(Q), (void**)pp);
}
T* p;
};
// AnthonyC changed this from const IID* to IID* 16/9/2002
template <class T, const IID* piid = &__uuidof(T)>
class CComQIPtr {
public:
typedef T _PtrClass;
CComQIPtr() { p = NULL; }
CComQIPtr(T* lp) {
if ((p = lp) != NULL) p->AddRef();
}
CComQIPtr(const CComQIPtr<T, piid>& lp) {
if ((p = lp.p) != NULL) p->AddRef();
}
CComQIPtr(IUnknown* lp) {
p = NULL;
if (lp != NULL) lp->QueryInterface(*piid, (void**)&p);
}
~CComQIPtr() {
if (p) p->Release();
}
void Release() {
IUnknown* pTemp = p;
if (pTemp) {
p = NULL;
pTemp->Release();
}
}
operator T*() const { return p; }
T& operator*() const {
ATLASSERT(p != NULL);
return *p;
}
// The assert on operator& usually indicates a bug. If this is really
// what is needed, however, take the address of the p member explicitly.
T** operator&() {
ATLASSERT(p == NULL);
return &p;
}
_NoAddRefReleaseOnCComPtr<T>* operator->() const {
ATLASSERT(p != NULL);
return (_NoAddRefReleaseOnCComPtr<T>*)p;
}
T* operator=(T* lp) { return (T*)AtlComPtrAssign((IUnknown**)&p, lp); }
T* operator=(const CComQIPtr<T, piid>& lp) {
return (T*)AtlComPtrAssign((IUnknown**)&p, lp.p);
}
T* operator=(IUnknown* lp) {
return (T*)AtlComQIPtrAssign((IUnknown**)&p, lp, *piid);
}
bool operator!() const { return (p == NULL); }
bool operator<(T* pT) const { return p < pT; }
bool operator==(T* pT) const { return p == pT; }
// Compare two objects for equivalence
bool IsEqualObject(IUnknown* pOther) {
if (p == NULL && pOther == NULL) return true; // They are both NULL objects
if (p == NULL || pOther == NULL)
return false; // One is NULL the other is not
CComPtr<IUnknown> punk1;
CComPtr<IUnknown> punk2;
p->QueryInterface(IID_IUnknown, (void**)&punk1);
pOther->QueryInterface(IID_IUnknown, (void**)&punk2);
return punk1 == punk2;
}
void Attach(T* p2) {
if (p) p->Release();
p = p2;
}
T* Detach() {
T* pt = p;
p = NULL;
return pt;
}
HRESULT CopyTo(T** ppT) {
ATLASSERT(ppT != NULL);
if (ppT == NULL) return E_POINTER;
*ppT = p;
if (p) p->AddRef();
return S_OK;
}
HRESULT SetSite(IUnknown* punkParent) {
return AtlSetChildSite(p, punkParent);
}
HRESULT Advise(IUnknown* pUnk, const IID& iid, LPDWORD pdw) {
return AtlAdvise(p, pUnk, iid, pdw);
}
HRESULT CoCreateInstance(REFCLSID rclsid, LPUNKNOWN pUnkOuter = NULL,
DWORD dwClsContext = CLSCTX_ALL) {
ATLASSERT(p == NULL);
return ::CoCreateInstance(rclsid, pUnkOuter, dwClsContext, __uuidof(T),
(void**)&p);
}
HRESULT CoCreateInstance(LPCOLESTR szProgID, LPUNKNOWN pUnkOuter = NULL,
DWORD dwClsContext = CLSCTX_ALL) {
CLSID clsid;
HRESULT hr = CLSIDFromProgID(szProgID, &clsid);
ATLASSERT(p == NULL);
if (SUCCEEDED(hr))
hr = ::CoCreateInstance(clsid, pUnkOuter, dwClsContext, __uuidof(T),
(void**)&p);
return hr;
}
template <class Q>
HRESULT QueryInterface(Q** pp) {
ATLASSERT(pp != NULL && *pp == NULL);
return p->QueryInterface(__uuidof(Q), (void**)pp);
}
T* p;
};
// Specialization to make it work
template <>
class CComQIPtr<IUnknown, &IID_IUnknown> {
public:
typedef IUnknown _PtrClass;
CComQIPtr() { p = NULL; }
CComQIPtr(IUnknown* lp) {
// Actually do a QI to get identity
p = NULL;
if (lp != NULL) lp->QueryInterface(IID_IUnknown, (void**)&p);
}
CComQIPtr(const CComQIPtr<IUnknown, &IID_IUnknown>& lp) {
if ((p = lp.p) != NULL) p->AddRef();
}
~CComQIPtr() {
if (p) p->Release();
}
void Release() {
IUnknown* pTemp = p;
if (pTemp) {
p = NULL;
pTemp->Release();
}
}
operator IUnknown*() const { return p; }
IUnknown& operator*() const {
ATLASSERT(p != NULL);
return *p;
}
// The assert on operator& usually indicates a bug. If this is really
// what is needed, however, take the address of the p member explicitly.
IUnknown** operator&() {
ATLASSERT(p == NULL);
return &p;
}
_NoAddRefReleaseOnCComPtr<T>* operator->() const {
ATLASSERT(p != NULL);
return (_NoAddRefReleaseOnCComPtr<T>*)p;
}
IUnknown* operator=(IUnknown* lp) {
// Actually do a QI to get identity
return (IUnknown*)AtlComQIPtrAssign((IUnknown**)&p, lp, IID_IUnknown);
}
IUnknown* operator=(const CComQIPtr<IUnknown, &IID_IUnknown>& lp) {
return (IUnknown*)AtlComPtrAssign((IUnknown**)&p, lp.p);
}
bool operator!() const { return (p == NULL); }
bool operator<(IUnknown* pT) const { return p < pT; }
bool operator==(IUnknown* pT) const { return p == pT; }
// Compare two objects for equivalence
bool IsEqualObject(IUnknown* pOther) {
if (p == NULL && pOther == NULL) return true; // They are both NULL objects
if (p == NULL || pOther == NULL)
return false; // One is NULL the other is not
CComPtr<IUnknown> punk1;
CComPtr<IUnknown> punk2;
p->QueryInterface(IID_IUnknown, (void**)&punk1);
pOther->QueryInterface(IID_IUnknown, (void**)&punk2);
return punk1 == punk2;
}
IUnknown* Detach() {
IUnknown* pt = p;
p = NULL;
return pt;
}
HRESULT CopyTo(T** ppT) {
ATLASSERT(ppT != NULL);
if (ppT == NULL) return E_POINTER;
*ppT = p;
if (p) p->AddRef();
return S_OK;
}
HRESULT SetSite(IUnknown* punkParent) {
return AtlSetChildSite(p, punkParent);
}
HRESULT Advise(IUnknown* pUnk, const IID& iid, LPDWORD pdw) {
return AtlAdvise(p, pUnk, iid, pdw);
}
HRESULT CoCreateInstance(REFCLSID rclsid, LPUNKNOWN pUnkOuter = NULL,
DWORD dwClsContext = CLSCTX_ALL) {
ATLASSERT(p == NULL);
return ::CoCreateInstance(rclsid, pUnkOuter, dwClsContext, __uuidof(T),
(void**)&p);
}
HRESULT CoCreateInstance(LPCOLESTR szProgID, LPUNKNOWN pUnkOuter = NULL,
DWORD dwClsContext = CLSCTX_ALL) {
CLSID clsid;
HRESULT hr = CLSIDFromProgID(szProgID, &clsid);
ATLASSERT(p == NULL);
if (SUCCEEDED(hr))
hr = ::CoCreateInstance(clsid, pUnkOuter, dwClsContext, __uuidof(T),
(void**)&p);
return hr;
}
template <class Q>
HRESULT QueryInterface(Q** pp) {
ATLASSERT(pp != NULL && *pp == NULL);
return p->QueryInterface(__uuidof(Q), (void**)pp);
}
IUnknown* p;
};
#define com_cast CComQIPtr
/////////////////////////////////////////////////////////////
// Class to Adapt CComBSTR and CComPtr for use with STL containers
// the syntax to use it is
// std::vector< CAdapt <CComBSTR> > vect;
template <class T>
class CAdapt {
public:
CAdapt() {}
CAdapt(const T& rSrc) { m_T = rSrc; }
CAdapt(const CAdapt& rSrCA) { m_T = rSrCA.m_T; }
CAdapt& operator=(const T& rSrc) {
m_T = rSrc;
return *this;
}
bool operator<(const T& rSrc) const { return m_T < rSrc; }
bool operator==(const T& rSrc) const { return m_T == rSrc; }
operator T&() { return m_T; }
operator const T&() const { return m_T; }
T m_T;
};
/////////////////////////////////////////////////////////////////////////////
// GUID comparison
inline BOOL InlineIsEqualGUID(REFGUID rguid1, REFGUID rguid2) {
return (((PLONG)&rguid1)[0] == ((PLONG)&rguid2)[0] &&
((PLONG)&rguid1)[1] == ((PLONG)&rguid2)[1] &&
((PLONG)&rguid1)[2] == ((PLONG)&rguid2)[2] &&
((PLONG)&rguid1)[3] == ((PLONG)&rguid2)[3]);
}
inline BOOL InlineIsEqualUnknown(REFGUID rguid1) {
return (((PLONG)&rguid1)[0] == 0 && ((PLONG)&rguid1)[1] == 0 &&
#ifdef _ATL_BYTESWAP
((PLONG)&rguid1)[2] == 0xC0000000 &&
((PLONG)&rguid1)[3] == 0x00000046);
#else
((PLONG)&rguid1)[2] == 0x000000C0 &&
((PLONG)&rguid1)[3] == 0x46000000);
#endif
}
/////////////////////////////////////////////////////////////////////////////
// Threading Model Support
class CComCriticalSection {
public:
void Lock() { EnterCriticalSection(&m_sec); }
void Unlock() { LeaveCriticalSection(&m_sec); }
void Init() { InitializeCriticalSection(&m_sec); }
void Term() { DeleteCriticalSection(&m_sec); }
CRITICAL_SECTION m_sec;
};
class CComAutoCriticalSection {
public:
void Lock() { EnterCriticalSection(&m_sec); }
void Unlock() { LeaveCriticalSection(&m_sec); }
CComAutoCriticalSection() { InitializeCriticalSection(&m_sec); }
~CComAutoCriticalSection() { DeleteCriticalSection(&m_sec); }
CRITICAL_SECTION m_sec;
};
class CComFakeCriticalSection {
public:
void Lock() {}
void Unlock() {}
void Init() {}
void Term() {}
};
class CComMultiThreadModelNoCS {
public:
static ULONG WINAPI Increment(LPLONG p) { return InterlockedIncrement(p); }
static ULONG WINAPI Decrement(LPLONG p) { return InterlockedDecrement(p); }
typedef CComFakeCriticalSection AutoCriticalSection;
typedef CComFakeCriticalSection CriticalSection;
typedef CComMultiThreadModelNoCS ThreadModelNoCS;
};
class CComMultiThreadModel {
public:
static ULONG WINAPI Increment(LPLONG p) { return InterlockedIncrement(p); }
static ULONG WINAPI Decrement(LPLONG p) { return InterlockedDecrement(p); }
typedef CComAutoCriticalSection AutoCriticalSection;
typedef CComCriticalSection CriticalSection;
typedef CComMultiThreadModelNoCS ThreadModelNoCS;
};
class CComSingleThreadModel {
public:
static ULONG WINAPI Increment(LPLONG p) { return ++(*p); }
static ULONG WINAPI Decrement(LPLONG p) { return --(*p); }
typedef CComFakeCriticalSection AutoCriticalSection;
typedef CComFakeCriticalSection CriticalSection;
typedef CComSingleThreadModel ThreadModelNoCS;
};
#if defined(_ATL_SINGLE_THREADED)
typedef CComSingleThreadModel CComObjectThreadModel;
typedef CComSingleThreadModel CComGlobalsThreadModel;
#elif defined(_ATL_APARTMENT_THREADED)
typedef CComSingleThreadModel CComObjectThreadModel;
typedef CComMultiThreadModel CComGlobalsThreadModel;
#else
typedef CComMultiThreadModel CComObjectThreadModel;
typedef CComMultiThreadModel CComGlobalsThreadModel;
#endif
/////////////////////////////////////////////////////////////////////////////
// CComModule
#define THREADFLAGS_APARTMENT 0x1
#define THREADFLAGS_BOTH 0x2
#define AUTPRXFLAG 0x4
HRESULT WINAPI AtlDumpIID(REFIID iid, LPCTSTR pszClassName, HRESULT hr);
#ifdef _ATL_DEBUG_INTERFACES
struct _QIThunk {
STDMETHOD(QueryInterface)(REFIID iid, void** pp) {
ATLASSERT(m_dwRef >= 0);
return pUnk->QueryInterface(iid, pp);
}
STDMETHOD_(ULONG, AddRef)() {
if (bBreak) DebugBreak();
pUnk->AddRef();
return InternalAddRef();
}
ULONG InternalAddRef() {
if (bBreak) DebugBreak();
ATLASSERT(m_dwRef >= 0);
long l = InterlockedIncrement(&m_dwRef);
ATLTRACE(_T("%d> "), m_dwRef);
AtlDumpIID(iid, lpszClassName, S_OK);
if (l > m_dwMaxRef) m_dwMaxRef = l;
return l;
}
STDMETHOD_(ULONG, Release)();
STDMETHOD(f3)();
STDMETHOD(f4)();
STDMETHOD(f5)();
STDMETHOD(f6)();
STDMETHOD(f7)();
STDMETHOD(f8)();
STDMETHOD(f9)();
STDMETHOD(f10)();
STDMETHOD(f11)();
STDMETHOD(f12)();
STDMETHOD(f13)();
STDMETHOD(f14)();
STDMETHOD(f15)();
STDMETHOD(f16)();
STDMETHOD(f17)();
STDMETHOD(f18)();
STDMETHOD(f19)();
STDMETHOD(f20)();
STDMETHOD(f21)();
STDMETHOD(f22)();
STDMETHOD(f23)();
STDMETHOD(f24)();
STDMETHOD(f25)();
STDMETHOD(f26)();
STDMETHOD(f27)();
STDMETHOD(f28)();
STDMETHOD(f29)();
STDMETHOD(f30)();
STDMETHOD(f31)();
STDMETHOD(f32)();
STDMETHOD(f33)();
STDMETHOD(f34)();
STDMETHOD(f35)();
STDMETHOD(f36)();
STDMETHOD(f37)();
STDMETHOD(f38)();
STDMETHOD(f39)();
STDMETHOD(f40)();
STDMETHOD(f41)();
STDMETHOD(f42)();
STDMETHOD(f43)();
STDMETHOD(f44)();
STDMETHOD(f45)();
STDMETHOD(f46)();
STDMETHOD(f47)();
STDMETHOD(f48)();
STDMETHOD(f49)();
STDMETHOD(f50)();
STDMETHOD(f51)();
STDMETHOD(f52)();
STDMETHOD(f53)();
STDMETHOD(f54)();
STDMETHOD(f55)();
STDMETHOD(f56)();
STDMETHOD(f57)();
STDMETHOD(f58)();
STDMETHOD(f59)();
STDMETHOD(f60)();
STDMETHOD(f61)();
STDMETHOD(f62)();
STDMETHOD(f63)();
STDMETHOD(f64)();
STDMETHOD(f65)();
STDMETHOD(f66)();
STDMETHOD(f67)();
STDMETHOD(f68)();
STDMETHOD(f69)();
STDMETHOD(f70)();
STDMETHOD(f71)();
STDMETHOD(f72)();
STDMETHOD(f73)();
STDMETHOD(f74)();
STDMETHOD(f75)();
STDMETHOD(f76)();
STDMETHOD(f77)();
STDMETHOD(f78)();
STDMETHOD(f79)();
STDMETHOD(f80)();
STDMETHOD(f81)();
STDMETHOD(f82)();
STDMETHOD(f83)();
STDMETHOD(f84)();
STDMETHOD(f85)();
STDMETHOD(f86)();
STDMETHOD(f87)();
STDMETHOD(f88)();
STDMETHOD(f89)();
STDMETHOD(f90)();
STDMETHOD(f91)();
STDMETHOD(f92)();
STDMETHOD(f93)();
STDMETHOD(f94)();
STDMETHOD(f95)();
STDMETHOD(f96)();
STDMETHOD(f97)();
STDMETHOD(f98)();
STDMETHOD(f99)();
STDMETHOD(f100)();
STDMETHOD(f101)();