//
{7B55D765-68A6-404e-99E2-CA9ABC389A27}
static
const
GUID IID_IUnknown9
=
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
0x7b55d765
,
0x68a6
,
0x404e
,
{
0x99
,
0xe2
,
0xca
,
0x9a
,
0xbc
,
0x38
,
0x9a
,
0x27
}
}
;
//
{060CAA56-33E9-4c79-BE5B-0F70A5DDFE15}
static
const
GUID CLSID_myadd
=
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
0x60caa56
,
0x33e9
,
0x4c79
,
{
0xbe
,
0x5b
,
0xf
,
0x70
,
0xa5
,
0xdd
,
0xfe
,
0x15
}
}
;
//
{9A03B1AC-8253-4e69-971F-1E88E817F6FA}
static
const
GUID IID_IAdd
=
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
0x9a03b1ac
,
0x8253
,
0x4e69
,
{
0x97
,
0x1f
,
0x1e
,
0x88
,
0xe8
,
0x17
,
0xf6
,
0xfa
}
}
;
//
{F03E6879-75E9-4cbc-A123-2AD92545869C}
static
const
GUID IID_ISubcat
=
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
0xf03e6879
,
0x75e9
,
0x4cbc
,
{
0xa1
,
0x23
,
0x2a
,
0xd9
,
0x25
,
0x45
,
0x86
,
0x9c
}
}
;
![](/Images/OutliningIndicators/None.gif)
class
IUnknown9
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public
:
virtual
HRESULT QueryInterface(REFIID riid,
void
**
ppvObject)
=
0
;
virtual
ULONG AddRef(
void
)
=
0
;
virtual
ULONG Release(
void
)
=
0
;
protected
:
private
:
}
;
class
IAdd:
public
IUnknown9
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public
:
virtual
int
add(
int
,
int
)
=
0
;
}
;
class
ISubcat:
public
IUnknown9
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public
:
virtual
int
sub(
int
,
int
)
=
0
;
protected
:
private
:
}
;
![](/Images/OutliningIndicators/None.gif)
class
Cmyadd:
public
IAdd,
public
ISubcat
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public
:
HRESULT QueryInterface(REFIID riid,
void
**
ppvObject)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if
(riid
==
IID_IUnknown9)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
*
ppvObject
=
(IAdd
*
)
this
;
AddRef();
return
S_OK;
}
else
if
(riid
==
IID_IAdd)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
*
ppvObject
=
(IAdd
*
)
this
;
AddRef();
return
S_OK;
}
else
if
(riid
==
IID_ISubcat)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
*
ppvObject
=
(ISubcat
*
)
this
;
AddRef();
return
S_OK;
}
else
return
E_UNEXPECTED;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
ULONG AddRef()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
++
m_ref;
cout
<<
"
\n object count =
"
<<
m_ref;
return
m_ref;
}
ULONG Release()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
--
m_ref;
if
(m_ref
==
0
)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
delete
this
;
cout
<<
"
\n object delete
"
;
}
return
m_ref;
}
Cmyadd()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_ref
=
0
;
}
int
add(
int
a,
int
b )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return
a
+
b;
}
int
sub(
int
a,
int
b )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return
a
-
b;
![](/Images/OutliningIndicators/InBlock.gif)
}
protected
:
private
:
ULONG m_ref;
}
;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
HRESULT DlllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID
*
ppv)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if
(rclsid
==
CLSID_myadd)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Cmyadd
*
pthis
=
new
Cmyadd;
pthis
->
QueryInterface(riid,ppv);
return
S_OK;
}
return
E_UNEXPECTED;
}
在exe中
HANDLE handle
=
::LoadLibrary(
"
.\\dllcom1.dll
"
);
![](/Images/OutliningIndicators/None.gif)
typedef HRESULT (
*
pc)(REFCLSID rclsid, REFIID riid, LPVOID
*
ppv);
![](/Images/OutliningIndicators/None.gif)
pc p
=
(pc)::GetProcAddress((HMODULE)handle,
"
DlllGetClassObject
"
);
IAdd
*
padd
=
NULL;
ISubcat
*
psub
=
NULL;
HRESULT hr
=
p(CLSID_myadd,IID_IAdd,(
void
**
)
&
padd);
if
(FAILED(hr))
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
cout
<<
"
\n can not get com
"
;
return
1
;
}
hr
=
padd
->
QueryInterface(IID_ISubcat,(
void
**
)
&
psub);
if
(FAILED(hr))
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
cout
<<
"
\n can not get interface
"
;
return
1
;
}
cout
<<
"
\n
"
<<
padd
->
add(
5
,
5
);
padd
->
Release();
cout
<<
"
\n
"
<<
psub
->
sub(
10
,
5
);
psub
->
Release();
cout
<<
"
\n
"
;
posted on 2006-06-30 17:23
黄大仙 阅读(901)
评论(1) 编辑 收藏 引用 所属分类:
c++