??1
#pragma?once
??2
static
?
const
?GUID?CLSID_mydiv?
=
?
??3
{?
0x41443b3
,?
0xe38f
,?
0x403d
,?
{?
0xad
,?
0xe7
,?
0x64
,?
0x6e
,?
0x16
,?
0x67
,?
0x10
,?
0x77
?}
?}
;
??4
//
?{977CF9DA-1373-4534-B70B-A1897BD33C7C}
??5
static
?
const
?GUID?IID_Idiv?
=
?
??6
{?
0x977cf9da
,?
0x1373
,?
0x4534
,?
{?
0xb7
,?
0xb
,?
0xa1
,?
0x89
,?
0x7b
,?
0xd3
,?
0x3c
,?
0x7c
?}
?}
;
??7
??8
//
?{8E8A627A-D2F8-4692-8C96-CEB9EB4734C1}
??9
static
?
const
?GUID?CLSID_mydivfactory?
=
?
?10
{?
0x8e8a627a
,?
0xd2f8
,?
0x4692
,?
{?
0x8c
,?
0x96
,?
0xce
,?
0xb9
,?
0xeb
,?
0x47
,?
0x34
,?
0xc1
?}
?}
;
?11
//
??[7/4/2006?hyk]
?12
/**/
/////////////////////////////////
Cmydiv?包容了?IAdd,ISubcat?接口
?13
//
??[7/4/2006?hyk]
?14
?15
extern
??
int
?m_comobjnum;
?16
extern
??
int
?m_lock;
?17
class
?Idiv:
public
?IUnknown
?18
{
?19
public
:
?20
????
virtual
?
int
?__stdcall?div(
int
,
int
)
=
0
;
?21
????
/**/
/*
virtual?int?__stdcall?add(int,int)=0;
?22
????virtual?int?__stdcall?sub(int,int)=0;
*/
?23
protected
:
?24
private
:
?25
}
;
?26
class
?Cmydiv:
public
?Idiv,
public
?IAdd,
public
?ISubcat
?27
{
?28
public
:
?29
????Cmydiv()
?30
????
{
?31
????????m_ref
=
0
;
?32
????????m_comobjnum
++
;
?33
????????padd
=
NULL;
?34
????????psubcat
=
NULL;
?35
????}
?36
????
~
Cmydiv()
?37
????
{
?38
?39
????????padd
->
Release();
?40
???
?41
????????
?42
????????psubcat
->
Release();
?43
????????m_comobjnum
--
;
?44
?45
?46
????}
?47
????HRESULT?init()
?48
????
{??
?49
????????HRESULT?hr
=
::CoCreateInstance(CLSID_myadd,NULL,CLSCTX_INPROC_SERVER,IID_IAdd,(
void
**
)
&
padd);
?50
????????
if
?(FAILED(hr))
?51
????????
{
?52
?????????????
return
?E_FAIL;
?53
????????}
?54
????????
//
hr=::CoCreateInstance(CLSID_myadd,NULL,CLSCTX_INPROC_SERVER,IID_ISubcat,(void**)&psubcat);
?55
????????hr
==
padd
->
QueryInterface(IID_ISubcat,(
void
**
)
&
psubcat);
?56
????????
if
?(FAILED(hr))
?57
????????
{
?58
????????????
return
?E_FAIL;
?59
????????}
?60
????????
return
?S_OK;
?61
????}
?62
????HRESULT?__stdcall?QueryInterface(REFIID?riid,?
void
?
**
ppvObject)
?63
????
{
?64
????????
if
?(riid
==
IID_IAdd)
?65
????????
{
?66
?????????????????
*
ppvObject
=
padd;
?67
?????????????????AddRef();
?68
?????????????????
return
?S_OK;
?69
????????}
else
?
if
?(riid
==
IID_ISubcat)
?70
????????
{
?71
??????????????????
*
ppvObject
=
psubcat;
?72
??????????????????
return
?S_OK;
?73
????????}
else
?
if
?(riid
==
IID_Idiv)
?74
????????
{
?75
????????????
*
ppvObject
=
(Idiv?
*
)
this
;
?76
????????????
return
?S_OK;
?77
?78
????????}
else
?
if
?(riid
==
IID_IUnknown)
?79
????????
{
?80
????????????
*
ppvObject
=
(Idiv?
*
)
this
;
?81
????????????
return
?S_OK;
?82
????????}
?83
????????
else
?84
????????????
return
?E_FAIL;
?85
????}
?86
????ULONG??__stdcall?AddRef()
?87
????
{
?88
????????
//
cout<<"\n"<<typeid(*this).name();
?89
????????
++
m_ref;
?90
????????cout
<<
"
\n?Cmydiv?object?ref?count?++=
"
<<
m_ref;
?91
????????
return
?m_ref;
?92
????}
?93
????ULONG??__stdcall?Release()?????????
?94
????
{
?95
????????
//
cout<<"\n"<<typeid(*this).name();
?96
????????
--
m_ref;
?97
????????cout
<<
"
\n?Cmydiv?object?ref?count?--=
"
<<
m_ref;
?98
????????
if
?(m_ref
==
0
)
?99
????????
{
100
????????????delete?
this
;
101
????????????cout
<<
"
\n?object?delete
"
;
102
????????}
103
????????
return
?m_ref;
104
????}
105
????
int
?__stdcall?add(
int
?a?,
int
?b?)
106
????
{
107
?????????????
return
??padd
->
add(a,b);
108
????}
109
????
int
?__stdcall?sub(
int
?a,
int
?b?)
110
????
{
111
????????????????
return
??psubcat
->
sub(a,b);
112
????}
113
????
int
?__stdcall?div(
int
?a,
int
?b)
114
????
{
115
????????
return
?a
/
b;
116
????}
117
protected
:
118
private
:
119
????
int
?m_ref;
120
????IAdd?
*
?padd;
121
????ISubcat?
*
?psubcat;
122
}
;
123
class
??Cmydivfactory:
public
?IClassFactory
124
{
125
public
:
126
????Cmydivfactory()
127
????
{
128
????????m_ref
=
0
;
129
????????
130
????}
131
????
~
Cmydivfactory()
132
????
{
133
134
????}
135
??????HRESULT?__stdcall?QueryInterface(REFIID?riid,?
void
?
**
ppvObject)
136
??????
{
137
??????????
if
?(riid
==
IID_IClassFactory)
138
??????????
{
139
??????????????
*
ppvObject
=
(IClassFactory
*
?)
this
;
140
??????????????AddRef();
141
??????????????
return
?S_OK;
142
??????????}
else
143
??????????
return
?E_NOINTERFACE;
144
?????????????
145
??????}
146
??????ULONG?__stdcall?AddRef()
147
??????
{
148
??????????
++
m_ref;
149
??????????
return
?m_ref;
150
151
??????}
152
??????ULONG?__stdcall?Release()
153
??????
{
154
??????????
--
m_ref;
155
??????????
if
?(
!
m_ref)
156
??????????
{
157
??????????????delete?
this
;
158
??????????}
159
??????????
return
?m_ref;
160
??????}
161
??????HRESULT?__stdcall?CreateInstance(IUnknown?
*
?pUnknownOuter,?
const
?IID
&
?iid,?
void
?
**
ppv)
162
??????
{
163
????????Cmydiv??
*
?pObj;???
164
??????????HRESULT?hr;
165
166
??????????
*
ppv
=
NULL;
167
??????????hr
=
E_OUTOFMEMORY;
168
??????????
if
?(NULL?
!=
?pUnknownOuter)
169
??????????????
return
?CLASS_E_NOAGGREGATION;
170
171
??????????
//
Create?the?object?passing?function?to?notify?on?destruction.
172
??????????pObj
=
new
?Cmydiv;
173
??????????
if
?(NULL
==
pObj)
174
??????????????
return
?hr;???
175
????????????????hr
=
pObj
->
init();
176
????????????????
if
?(FAILED(hr))
177
????????????????
{
178
????????????????????
return
?hr;
179
????????????????}
180
??????????
//
Obtain?the?first?interface?pointer?(which?does?an?AddRef)
181
??????????hr
=
pObj
->
QueryInterface(iid,?ppv);
182
183
??????????
if
?(hr?
!=
?S_OK)?
{
184
??????????????
//
Kill?the?object?if?initial?creation?or?FInit?failed.
185
??????????????m_comobjnum?
--
;?
//
?Reference?count?g_cDictionary?be?added?in?constructor
186
??????????????delete?pObj;
187
??????????}
188
189
??????????
return
?hr;???
190
??????}
191
??????HRESULT?__stdcall?LockServer(BOOL?fLock)
192
??????
{
193
??????????
if
?(fLock)
194
??????????
{
195
??????????????m_lock
++
;
196
??????????}
else
197
??????????????m_lock
--
;
198
??????????
return
?S_OK;
199
??????}
200
protected
:
201
202
private
:
203
????
int
?m_ref;
204
}
;
205
?????? A要重用 實現2個接口? ,a,b,? 則A 繼承 a,b,并要改寫 a,b 函數,增加 a,b 2個指針,在初始化
中獲取 a,b 接口指針 ,在析構中釋放。
?? 關于類廠要注意的是,類廠guid,在客戶文件中并不需要,僅僅在組件中使用,也不進入注冊表(進程
中的組件)
?????????? 對象計數和鎖計數,其中對象計數一般可以不包含類廠
????????????????? 以這種方式來實現重用,更體現 接口的概念,
?????????????????????????? 客戶端只需要 接口頭文件, 接口iid,com類clsid
posted on 2006-07-04 16:20
黃大仙 閱讀(659)
評論(0) 編輯 收藏 引用 所屬分類:
c++