-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
COM学习笔记.html
1778 lines (1707 loc) · 63 KB
/
COM学习笔记.html
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
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>COM 学习笔记</title>
<link rel="stylesheet" type="text/css" href="../Script/样式.css">
<script language ="vbscript" src ="../Script/webScript.bas" ></script>
<script language="JavaScript" fptype="dynamicoutline">
<!--
function dynOutline() {}
//-->
</script>
<script language="JavaScript1.2" fptype="dynamicoutline" for="document" event="onreadystatechange()">
<!--
initOutline()
//-->
</script>
<script language="JavaScript1.2" fptype="dynamicoutline" src="file:///C:/Program%20Files/Microsoft%20Office/OFFICE11/fpclass/outline.js">
</script>
</head>
<body onclick="dynOutline()" language="Javascript1.2">
<p align="center"><b><font size="7">COM 学习笔记</font></b></p>
<p align="center">康 林 2004年11月</p>
<p align="left">关键词:组件、接口、动态链接库、注册表、CLSID、GUID、IID、UUID</p>
<ol dynamicoutline initcollapsed type="I" style ="cursor:hand">
<li>
<p align="left"><b>概念:</b></p>
</li>
<ol>
<li>
<p align="left"><b>类型库:</b></p></li>
<li>
<p align="left"><b>组件:</b>是一个接口的集合。</p></li>
<li>
<p align="left"><b>接口:</b>是一个包含一个函数接针数组的内存结构。每一个数组元素包含的是一个由组件所实现的函数的地址。</p>
</li>
<p align="left"><font color="#FF0000">组件是接口的集合,接口是函数的集合。</font></p>
</ol>
<li>
<p align="left"><b>QueryInterface 的实现规则:</b></p>
</li>
<ol>
<li>
<p align="left">QueryInterface 返回的总是同一个 IUnknown 指针。</li>
<li>
<p align="left">若客户曾经获得过某个接口,那么它将总能获取此接口。</li>
<li>
<p align="left">客户可以再次获得已经拥有的接口。</li>
<li>
<p align="left">客户可以返回到起始接口。</li>
<li>
<p align="left">若能够从某个接口获得某按按特定接口,那么可以从任意接口都将可以获得此接口。</li>
</ol>
<li>
<p align="left"><b>
<a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/dnduwam/html/d35remot.htm">ProgID</a> 与 CLSID的转换:</b></p>
</li>
<ol>
<li>
<p align="left">ProgIDFromCLSID<br><code><b>HRESULT CLSIDFromProgID(<br> LPCOLESTR</b><i> lpszProgID</i><b>,
</b>//Pointer to the ProgID<br><b> LPCLSID</b><i> pclsid
</i>//Pointer to the CLSID<br><b>);</b></code></li>
<li>
<p align="left">CLSIDFromProgID <br><code><b>WINOLEAPI ProgIDFromCLSID(<br> REFCLSID</b><i> clsid</i><b>,
</b>//CLSID for which the ProgID is
requested<br><b> LPOLESTR *</b><i> lplpszProgID</i><br><i>
</i>//Address of output variable that receives a <br>
// pointer to the requested ProgID string<br><b>);</b><br> </code></li>
</ol>
<li>
<p align="left"><b>CLSID 与字符串的转换:</b></p>
</li>
<ol>
<li>
<p align="left">CLSIDFromString<code><b><br>HRESULT CLSIDFromString(<br> LPOLESTR</b><i> lpsz</i><b>,
</b>//Pointer to the string
representation of the CLSID<br><b> LPCLSID</b><i> pclsid </i>//Pointer to the CLSID<br>
<b>);</b></code></li>
<li>
<p align="left">StringFromCLSID<br><code><b>WINOLEAPI StringFromCLSID(<br> REFCLSID</b><i> rclsid</i><b>,
</b>//CLSID to be converted<br><b> LPOLESTR *</b><i> ppsz </i>//Address of output variable that
receives a <br>
// pointer to the resulting string<br><b>);</b></code></li>
<li>
<p align="left">StringFromGUID2<br><code><b>int StringFromGUID2(<br> REFGUID</b><i> rguid</i><b>,
</b>//Interface identifier to be
converted<br><b> LPOLESTR</b><i> lpsz</i><b>, </b>//Pointer to the resulting
string on return<br><b> int</b><i> cchMax
</i>//Character count of string at <i>lpsz</i><br><b>);</b><br> </code></li>
<li>
<p align="left">StringFromIID <br><code><b>WINOLEAPI StringFromIID(<br> REFIID</b><i> rclsid</i><b>,
</b>//Interface
identifier to be converted<br><b> LPOLESTR *</b><i> lplpsz </i>//Address of output variable
that receives a <br>
// pointer to the resulting string<br><b>);</b></code></li>
<li>
<p align="left">IIDFromString<br><code><b>WINOLEAPI IIDFromString(<br> LPOLESTR</b><i> lpsz</i><b>,
</b>//Pointer to the string
representation of the IID<br><b> LPIID</b><i> lpiid
</i>//Pointer to the
requested IID on return<br><b>);</b></code></li>
</ol>
<li>
<p align="left"><b>注册:</b></p>
</li>
<ol>
<li>
<p align="left">用程序注册:regsvr32.exe</li>
<li>
<p align="left">调用动态函数库中的注册函数:DllRegisterServer</li>
<li>
<p align="left">调用动态函数库中的反注册函数:DllUnregisterServer </li>
</ol>
<li>
<p align="left"><b>COM库函数:</b></p>
</li>
<ol>
<li>
<p align="left">CoCreateInstance<br><code><b> STDAPI CoCreateInstance(<br> REFCLSID</b><i> rclsid</i><b>,
</b>//Class identifier (CLSID) of the object<br><b> LPUNKNOWN</b><i> pUnkOuter</i><b>,
</b>//Pointer
to whether object is or isn't part <br>
// of an aggregate<br><b> DWORD</b><i> dwClsContext</i><b>,
</b>//Context for running executable code<br><b> REFIID</b><i> riid</i><b>,
</b>//Reference to the identifier of the interface<br><b> LPVOID *</b><i> ppv
</i>//Address of output variable that receives <br>
// the interface pointer requested in <i>riid</i><br><b> );<br></b>CoCreateInstance 实际上是调用
</code>CoGetClassObject 实现的。CoGetClassObject
将在注册表中查找指定的组件。找到之后,它将装载实现此组件的 DLL(用 <b>CoLoadLibrary</b>)。装载成功之后,它将调用在 DLL
服务器中的实现的 DllGetClassObject。此函数的作用是创建相应的类厂。另外 DllGetClassObject 还将查询
IClassFactory 接口,并将其返回给 CoCreateInstance。然后,CoCreatInstnce 将使用此接口来调用
IClassFactory::CreateInstance 函数。并查询指 CoCreateInstance 参数 riid
中指定的接口。在得到了此接口之后,CoCreateInstance
将释放相应的类厂并将此接口的指针返回给客户。然后客户就能使用此指针来调用组件中的某个方法了。</li>
<li>
<p align="left">CoGetClassObject<br><code><b>STDAPI CoGetClassObject(<br> REFCLSID</b><i> rclsid</i><b>,
</b>//CLSID associated with the
class object<br><b> DWORD</b><i> dwClsContext</i><b>,</b><br><b>
</b>//Context for running executable code<br><b> COSERVERINFO *</b><i> pServerInfo</i><b>,</b><br>
<b>
</b>//Pointer to machine on which the object is to <br>
// be instantiated<br><b> REFIID</b><i> riid</i><b>,
</b>//Reference to the identifier of the interface<br><b> LPVOID *</b><i> ppv
</i>//Address of
output variable that receives the <br>
// interface pointer requested in <i>riid</i><br><b>);</b></code></li>
<li>CoFreeUnusedLibraries:释放不再使用的DLL<br> </li>
</ol>
<li>
<b>包容与聚合:</b>
</li>
<li>
<p align="left"><b>接口指针类(智能接口指针):</b></p>
</li>
<ol>
<li>
<p align="left">ATL中有 CComPrt 和 CComQIprt(<span class="ShowLink" onclick="Call Show(ccomptr_h)">#include <ATLBASE.H></span>)
<table border="1" width="100%" class="TableStyle" style ="display:none" id="ccomptr_h">
<tr>
<td>
<pre>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</pre>
<pre> if (p == NULL || pOther == NULL)
return false; // One is NULL the other is not</pre>
<pre> 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;
};
</pre>
<pre>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</pre>
<pre> if (p == NULL || pOther == NULL)
return false; // One is NULL the other is not</pre>
<pre> 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;
};</pre>
<pre>//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</pre>
<pre> if (p == NULL || pOther == NULL)
return false; // One is NULL the other is not</pre>
<pre> 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;
};</pre>
<pre>#define com_cast CComQIPtr</pre>
</td>
</tr>
</table>
</li>
<li>
<p align="left">MFC中有CIP(<span class="ShowLink" onclick ="Call Show(afxcom_h)" >#include <afxcom_.h></span>)<table border="1" width="100%" id ="afxcom_h" class="TableStyle" style ="display:none">
<tr>
<td>
<pre>// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.</pre>
<pre>/////////////////////////////////////////////////////////////////////////////
// AFXCOM_.H
//
// THIS FILE IS FOR MFC IMPLEMENTATION ONLY.</pre>
<pre>#ifndef __AFXCOM_H__
#define __AFXCOM_H__</pre>
<pre>#ifndef _OBJBASE_H_
#include <objbase.h>
#endif</pre>
<pre>/////////////////////////////////////////////////////////////////////////////</pre>
<pre>#ifdef _AFX_MINREBUILD
#pragma component(minrebuild, off)
#endif
#ifndef _AFX_FULLTYPEINFO
#pragma component(mintypeinfo, on)
#endif</pre>
<pre>/////////////////////////////////////////////////////////////////////////////</pre>
<pre>#ifndef _AFX_NOFORCE_LIBS
#pragma comment(lib, "uuid.lib")
#endif</pre>
<pre>/////////////////////////////////////////////////////////////////////////////</pre>
<pre>#ifdef _AFX_PACKING
#pragma pack(push, _AFX_PACKING)
#endif</pre>
<pre>#ifndef ASSERT
#ifndef _INC_CRTDBG
#include <crtdbg.h>
#endif // _INC_CRTDBG
#define ASSERT(x) _ASSERT(x)
#endif // ASSERT</pre>
<pre>/////////////////////////////////////////////////////////////////////////////</pre>
<pre>template<class _Interface, const IID* _IID>
class _CIP
{
public:
// Declare interface type so that the type may be available outside
// the scope of this template.
typedef _Interface Interface;</pre>
<pre> // When the compiler supports references in template params,
// _CLSID will be changed to a reference. To avoid conversion
// difficulties this function should be used to obtain the
// CLSID.
static const IID& GetIID()
{ ASSERT(_IID != NULL); return *_IID; }</pre>
<pre> // Construct empty in preparation for assignment.
_CIP();</pre>
<pre> // Copy the pointer and AddRef().
_CIP(const _CIP& cp) : _pInterface(cp._pInterface)
{ _AddRef(); }</pre>
<pre> // Saves and AddRef()'s the interface
_CIP(Interface* pInterface) : _pInterface(pInterface)
{ _AddRef(); }</pre>
<pre> // Copies the pointer. If bAddRef is TRUE, the interface will
// be AddRef()ed.
_CIP(Interface* pInterface, BOOL bAddRef)
: _pInterface(pInterface)
{
if (bAddRef)
{
ASSERT(pInterface != NULL);
_AddRef();
}
}</pre>
<pre> // Calls CoCreateClass with the provided CLSID.
_CIP(const CLSID& clsid, DWORD dwClsContext = CLSCTX_INPROC_SERVER)
: _pInterface(NULL)
{
CreateObject(clsid, dwClsContext);
}</pre>
<pre> // Calls CoCreateClass with the provided CLSID retrieved from
// the string.
_CIP(LPOLESTR str, DWORD dwClsContext = CLSCTX_INPROC_SERVER)
: _pInterface(NULL)
{
CreateObject(str, dwClsContext);
}</pre>
<pre> // Saves and AddRef()s the interface.
_CIP& operator=(Interface* pInterface)
{
if (_pInterface != pInterface)
{
Interface* pOldInterface = _pInterface;
_pInterface = pInterface;
_AddRef();
if (pOldInterface != NULL)
pOldInterface->Release();
}
return *this;
}</pre>
<pre> // Copies and AddRef()'s the interface.
_CIP& operator=(const _CIP& cp)
{ return operator=(cp._pInterface); }</pre>
<pre> // Releases any current interface and loads the class with the
// provided CLSID.
_CIP& operator=(const CLSID& clsid)
{
CreateObject(clsid);
return *this;
}</pre>
<pre> // Calls CoCreateClass with the provided CLSID retrieved from
// the string.
_CIP& operator=(LPOLESTR str)
{
CreateObject(str);
return *this;
}</pre>
<pre> ~_CIP();</pre>
<pre> // Saves/sets the interface without AddRef()ing. This call
// will release any previously acquired interface.
void Attach(Interface* pInterface)
{
_Release();
_pInterface = pInterface;
}</pre>
<pre> // Saves/sets the interface only AddRef()ing if bAddRef is TRUE.
// This call will release any previously acquired interface.
void Attach(Interface* pInterface, BOOL bAddRef)
{
_Release();
_pInterface = pInterface;
if (bAddRef)
{
ASSERT(pInterface != NULL);
pInterface->AddRef();
}
}</pre>
<pre> // Simply NULL the interface pointer so that it isn't Released()'ed.
void Detach()
{
ASSERT(_pInterface);
_pInterface = NULL;
}</pre>
<pre> // Return the interface. This value may be NULL
operator Interface*() const
{ return _pInterface; }</pre>
<pre> // Queries for the unknown and return it
operator IUnknown*()
{ return _pInterface; }</pre>
<pre> // Provides minimal level assertion before use.
operator Interface&() const
{ ASSERT(_pInterface); return *_pInterface; }</pre>
<pre> // Allows an instance of this class to act as though it were the
// actual interface. Also provides minimal assertion verification.
Interface& operator*() const
{ ASSERT(_pInterface); return *_pInterface; }</pre>
<pre> // Returns the address of the interface pointer contained in this
// class. This is useful when using the COM/OLE interfaces to create
// this interface.
Interface** operator&()
{
_Release();
_pInterface = NULL;
return &_pInterface;
}</pre>
<pre> // Allows this class to be used as the interface itself.
// Also provides simple assertion verification.
Interface* operator->() const
{ ASSERT(_pInterface != NULL); return _pInterface; }</pre>
<pre> // This operator is provided so that simple boolean expressions will
// work. For example: "if (p) ...".
// Returns TRUE if the pointer is not NULL.
operator BOOL() const
{ return _pInterface != NULL; }</pre>
<pre> // Returns TRUE if the interface is NULL.
// This operator will be removed when support for type bool
// is added to the compiler.
BOOL operator!()
{ return _pInterface == NULL; }</pre>
<pre> // Provides assertion verified, Release()ing of this interface.
void Release()
{
ASSERT(_pInterface != NULL);
_pInterface->Release();
_pInterface = NULL;
}</pre>
<pre> // Provides assertion verified AddRef()ing of this interface.
void AddRef()
{ ASSERT(_pInterface != NULL); _pInterface->AddRef(); }</pre>
<pre> // Another way to get the interface pointer without casting.
Interface* GetInterfacePtr() const
{ return _pInterface; }</pre>
<pre> // Loads an interface for the provided CLSID.
// Returns an HRESULT. Any previous interface is released.
HRESULT CreateObject(
const CLSID& clsid, DWORD dwClsContext = CLSCTX_INPROC_SERVER)
{
_Release();
HRESULT hr = CoCreateInstance(clsid, NULL, dwClsContext,
GetIID(), reinterpret_cast<void**>(&_pInterface));
ASSERT(SUCCEEDED(hr));
return hr;
}</pre>
<pre> // Creates the class specified by clsidString. clsidString may
// contain a class id, or a prog id string.
HRESULT CreateObject(
LPOLESTR clsidString, DWORD dwClsContext=CLSCTX_INPROC_SERVER)
{
ASSERT(clsidString != NULL);
CLSID clsid;
HRESULT hr;
if (clsidString[0] == '{')
hr = CLSIDFromString(clsidString, &clsid);
else
hr = CLSIDFromProgID(clsidString, &clsid);
ASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return hr;
return CreateObject(clsid, dwClsContext);
}</pre>
<pre> // Performs a QI on pUnknown for the interface type returned
// for this class. The interface is stored. If pUnknown is
// NULL, or the QI fails, E_NOINTERFACE is returned and
// _pInterface is set to NULL.
HRESULT QueryInterface(IUnknown* pUnknown)
{
if (pUnknown == NULL) // Can't QI NULL
{
operator=(static_cast<Interface*>(NULL));
return E_NOINTERFACE;
}</pre>
<pre> // Query for this interface
Interface* pInterface;
HRESULT hr = pUnknown->QueryInterface(GetIID(),
reinterpret_cast<void**>(&pInterface));
if (FAILED(hr))
{
// If failed initialize interface to NULL and return HRESULT.
Attach(NULL);
return hr;
}</pre>
<pre> // Save the interface without AddRef()ing.
Attach(pInterface);
return hr;
}</pre>
<pre>private:
// Releases only if the interface is not null.
// The interface is not set to NULL.
void _Release()
{
if (_pInterface != NULL)
_pInterface->Release();
}</pre>
<pre> // AddRefs only if the interface is not NULL
void _AddRef()
{
if (_pInterface != NULL)
_pInterface->AddRef();
}</pre>
<pre> // The Interface.
Interface* _pInterface;
}; // class _CIP</pre>
<pre>template<class _Interface, const IID* _IID>
_CIP<_Interface, _IID>::_CIP<_Interface, _IID>()
: _pInterface(NULL)
{
}</pre>
<pre>template<class _Interface, const IID* _IID>
_CIP<_Interface, _IID>::~_CIP<_Interface, _IID>()
{
// If we still have an interface then Release() it. The interface
// may be NULL if Detach() has previously been called, or if it was
// never set.</pre>
<pre> _Release();
}</pre>
<pre>template<class _Interface, const IID* _IID>
class CIP : public _CIP<_Interface, _IID>
{
public:
// Simplified name for base class and provide derived classes
// access to base type
typedef _CIP<_Interface, _IID> BC;</pre>
<pre> // Provideds derived classes access to the interface type.
typedef _Interface Interface;</pre>
<pre> // Construct empty in preparation for assignment.
CIP() { }
~CIP();</pre>
<pre> // Copy the pointer and AddRef().
CIP(const CIP& cp) : _CIP<_Interface, _IID>(cp) { }</pre>
<pre> // Saves and AddRef()s the interface.
CIP(Interface* pInterface) : _CIP<_Interface, _IID>(pInterface) { }</pre>
<pre> // Saves the interface and AddRef()s only if bAddRef is TRUE.
CIP(Interface* pInterface, BOOL bAddRef)
: _CIP<_Interface, _IID>(pInterface, bAddRef) { }</pre>
<pre> // Queries for this interface.
CIP(IUnknown* pUnknown)
{
if (pUnknown == NULL)
return;
Interface* pInterface;
HRESULT hr = pUnknown->QueryInterface(GetIID(),
reinterpret_cast<void**>(&pInterface));
ASSERT(SUCCEEDED(hr));
Attach(pInterface);
}</pre>
<pre> // Creates the interface from the CLSID.
CIP(const CLSID& clsid) : _CIP<_Interface, _IID>(clsid) { }</pre>
<pre> // Creates the interface from the CLSID.
CIP(LPOLESTR str) : _CIP<_Interface, _IID>(str) { }</pre>
<pre> // Copies and AddRef()'s the interface.
CIP& operator=(const CIP& cp)
{ _CIP<_Interface, _IID>::operator=(cp); return *this; }</pre>
<pre> // Saves and AddRef()s the interface.
CIP& operator=(Interface* pInterface)
{ _CIP<_Interface, _IID>::operator=(pInterface); return *this; }</pre>
<pre> CIP& operator=(IUnknown* pUnknown)
{
HRESULT hr = QueryInterface(pUnknown);
ASSERT(SUCCEEDED(hr));
return *this;
}</pre>
<pre> // Releases any current interface and loads the class with the
// provided CLSID.
CIP& operator=(const CLSID& clsid)
{ _CIP<_Interface, _IID>::operator=(clsid); return *this; }</pre>
<pre> // Releases any current interface and loads the class with the
// provided CLSID.
CIP& operator=(LPOLESTR str)
{ _CIP<_Interface, _IID>::operator=(str); return *this; }
}; // class CIP</pre>
<pre>template<class _Interface, const IID* _IID>
CIP<_Interface, _IID>::~CIP()
{
}</pre>
<pre>#if _MSC_VER > 1020
template<>
#endif
class CIP<IUnknown, &IID_IUnknown> : public _CIP<IUnknown, &IID_IUnknown>
{
public:
// Simplified name for base class and provide derived classes
// access to base type
typedef _CIP<IUnknown, &IID_IUnknown> BC;</pre>
<pre> // Provideds derived classes access to the interface type.
typedef IUnknown Interface;</pre>
<pre> // Construct empty in preparation for assignment.
CIP() { }</pre>
<pre> // Copy the pointer and AddRef().
CIP(const CIP& cp) : _CIP<IUnknown, &IID_IUnknown>(cp) { }</pre>
<pre> // Saves and AddRef()s the interface.
CIP(Interface* pInterface)
: _CIP<IUnknown, &IID_IUnknown>(pInterface) { }</pre>
<pre> // Saves and then AddRef()s only if bAddRef is TRUE.
CIP(Interface* pInterface, BOOL bAddRef)
: _CIP<IUnknown, &IID_IUnknown>(pInterface, bAddRef) { }</pre>
<pre> // Creates the interface from the CLSID.
CIP(const CLSID& clsid) : _CIP<IUnknown, &IID_IUnknown>(clsid) { }</pre>
<pre> // Creates the interface from the CLSID.
CIP(LPOLESTR str) : _CIP<IUnknown, &IID_IUnknown>(str) { }</pre>
<pre> // Copies and AddRef()'s the interface.
CIP& operator=(const CIP& cp)
{ _CIP<IUnknown, &IID_IUnknown>::operator=(cp); return *this; }</pre>
<pre> // Saves and AddRef()s the interface. The previously saved
// interface is released.
CIP& operator=(Interface* pInterface)
{ _CIP<IUnknown, &IID_IUnknown>::operator=(pInterface); return *this; }</pre>
<pre> // Releases any current interface and loads the class with the
// provided CLSID.
CIP& operator=(const CLSID& clsid)
{ _CIP<IUnknown, &IID_IUnknown>::operator=(clsid); return *this; }</pre>
<pre> // Releases any current interface and loads the class with the
// provided CLSID.
CIP& operator=(LPOLESTR str)
{ _CIP<IUnknown, &IID_IUnknown>::operator=(str); return *this; }</pre>
<pre> // Queries for the unknown and return it
operator IUnknown*()
{ return GetInterfacePtr(); }</pre>
<pre> // Verifies that pUnknown is not null and performs assignment.
HRESULT QueryInterface(IUnknown* pUnknown)
{
_CIP<IUnknown, &IID_IUnknown>::operator=(pUnknown);
return pUnknown != NULL ? S_OK : E_NOINTERFACE;
}
}; // CIP<IUnknown, &IID_IUnknown></pre>
<pre>#define IPTR(x) CIP<x, &IID_##x>
#define DEFINE_IPTR(x) typedef IPTR(x) x##Ptr;</pre>
<pre>/////////////////////////////////////////////////////////////////////////////</pre>
<pre>#ifdef _AFX_PACKING
#pragma pack(pop)
#endif</pre>
<pre>#ifdef _AFX_MINREBUILD
#pragma component(minrebuild, on)
#endif
#ifndef _AFX_FULLTYPEINFO
#pragma component(mintypeinfo, off)
#endif</pre>
<pre>#endif // __AFXCOM_H__</pre>
<pre>/////////////////////////////////////////////////////////////////////////////</pre>
</td>
</tr>
</table>
</li>
<li>使用接口类的成员函数,用 . 操作符。例如:<br>