# include <objbase.h>
void trace(const char* pMsg)
{cout<<pMsg<<endl;}
interface IX :IUnknown
{ virtual void __stdcall Fx()=0;
// virtual void __stdcall Fx2()=0;
};
interface IY :IUnknown
{ virtual void __stdcall Fy()=0;
// virtual void __stdcall Fy2()=0;
};
interface IZ :IUnknown
{ virtual void __stdcall FZ()=0;
// virtual void __stdcall Fy2()=0;
};
extern const IID IId_IX;
extern const IID IId_IY;
extern const IID IId_IZ;
class CA: public IX,
public IY
{
//virtual HRESULT __stdcall QueryInterface(const IID& iid,void** ppv);
virtual ULONG __stdcall AddRef(){ return 0;}
virtual ULONG __stdcall release() {return 0;}
virtual void __stdcall Fx(){cout << "Fx"<<endl;}
virtual void __stdcall Fy(){cout << "Fy"<<endl;}
HRESULT __stdcall CA::QueryInterface(const IID& iid,void** ppv)
{
if(iid ==IID_IUnknown)
{
trace("queryinterface :return pointer to IUnknown.");
*ppv=static_cast<IY*>(this);
}
else if(iid ==IID_IX)
{
trace("queryinterface :return pointer to IX.");
*ppv=static_cast<IX*>(this);
}
else if(iid ==IID_IY)
{
trace("queryinterface :return pointer to IY.");
*ppv=static_cast<IY*>(this);
}
else
{trace("QueryInterface:Interface not supported.")
*ppv=NULL;
return E_NOINTERFACE;
}
reintepret_cast<IUnknown*>(*ppv)->AddRef();
return S_OK;
}
IUnknown* CreateInstance()
{
IUnknown* pI =static_cast<IX*>(new CA);
pI->AddRef();
return pI;
}
static const IID IId_IX =
{0x32bb8320,0xb41b,0x11cf,
{0xa6,0xbb,0x0,0x80,0xc7,0xb2,0xd6,0x82}};
static const IID IId_IY =
{0x32bb8321,0xb41b,0x11cf,
{0xa6,0xbb,0x0,0x80,0xc7,0xb2,0xd6,0x82}};
static const IID IId_IZ =
{0x32bb8322,0xb41b,0x11cf,
{0xa6,0xbb,0x0,0x80,0xc7,0xb2,0xd6,0x82}};
int main()
{
HRESULT hr;
trace("Client:Get an IUNknown pointer.");
IUnknown* pIUnknown=CreateInstance();
trace ("Client:Get interface IX.");
IX*pIX =NULL;
hr=pIUnknown->queryInterface(IID_IX,(void**)&pIX);
if(SUCCEEDED(hr))
{
trace("Client: Succeeded getting IX.");
pIX->Fx();
}
trace ("Client: Get interface IY.");
IY* pIY =NULL;
hr=pIUnknown->queryInterface(IID_IY,(void**)&pIY);
if(SUCCEEDED(hr))
{
trace("Client: Succeeded getting IY.");
pIX->Fy();
}
trace ("Client: Get interface IZ.");
IY* pIZ =NULL;
hr=pIUnknown->queryInterface(IID_IZ,(void**)&pIZ);
if(SUCCEEDED(hr))
{
trace("Client: Succeeded getting IZ.");
pIX->FZ();
}
else
{
trace("Client: can't get interface IZ."};
}
trace ("Client: Get interface IY from interface IX.");
IY* pIYFromIX =NULL;
hr=pIX->queryInterface(IID_IY,(void**) &pIYFromIX);
if(SUCCEEDED(hr))
{
trace("Client: Succeeded getting IY.");
pIYfromIX->Fy();
}
trace ("Client: Get interface IUnknown from IY.");
IUnknown* pIUnknownFromIY =NULL;
hr=pIY->queryInterface(IID_IUnknown,(void**)&pIUnknownFromIY);
if(SUCCEEDED(hr))
{
cout <<"are the IUnknown pointers equal?";
if(pIUnknownFromIY == IUnown)
{
cout<<"yes."<<endl;
}
else
{
cout<<"no."<<endl;
}
}
delete pIUnknown;
return 0;
}
7 个解决方案
#1
我怎么没有遇到错误?以下是程序运行后的结果. WindowsNT4.0+VC6.0
H:\book\Inside COM\CODE\CHAP03>iunknown.exe
Client: Get an IUnknown pointer.
Client: Get interface IX.
QueryInterface: Return pointer to IX.
Client: Succeeded getting IX.
Fx
Client: Get interface IY.
QueryInterface: Return pointer to IY.
Client: Succeeded getting IY.
Fy
Client: Ask for an unsupported interface.
QueryInterface: Interface not supported.
Client: Could not get interface IZ.
Client: Get interface IY from interface IX.
QueryInterface: Return pointer to IY.
Client: Succeeded getting IY.
Fy
Client: Get interface IUnknown from IY.
QueryInterface: Return pointer to IUnknown.
Are the IUnknown pointers equal? Yes, pIUnknownFromIY == pIUnknown.
H:\book\Inside COM\CODE\CHAP03>iunknown.exe
Client: Get an IUnknown pointer.
Client: Get interface IX.
QueryInterface: Return pointer to IX.
Client: Succeeded getting IX.
Fx
Client: Get interface IY.
QueryInterface: Return pointer to IY.
Client: Succeeded getting IY.
Fy
Client: Ask for an unsupported interface.
QueryInterface: Interface not supported.
Client: Could not get interface IZ.
Client: Get interface IY from interface IX.
QueryInterface: Return pointer to IY.
Client: Succeeded getting IY.
Fy
Client: Get interface IUnknown from IY.
QueryInterface: Return pointer to IUnknown.
Are the IUnknown pointers equal? Yes, pIUnknownFromIY == pIUnknown.
#2
阿毛朋友,我看的是电子书,没有源程序,能把源程序传给我吗?
whlxy@263.net
谢谢!!
whlxy@263.net
谢谢!!
#3
累死我啦,全是拼写错误,我被骗了,呜。。。。呜。。。。。。
#4
hbs朋友,实在对不起,为了补偿你,我给你20分,望查收。
#5
To whlxy:
源程序给你发了(2-12章),压缩后1.69M
whlxy@263.net
源程序给你发了(2-12章),压缩后1.69M
whlxy@263.net
#6
谢谢 阿毛,我给你加了20分。
另外,你有 《inside atl》的程序吗?
另外,你有 《inside atl》的程序吗?
#7
对不起,我没有.
#1
我怎么没有遇到错误?以下是程序运行后的结果. WindowsNT4.0+VC6.0
H:\book\Inside COM\CODE\CHAP03>iunknown.exe
Client: Get an IUnknown pointer.
Client: Get interface IX.
QueryInterface: Return pointer to IX.
Client: Succeeded getting IX.
Fx
Client: Get interface IY.
QueryInterface: Return pointer to IY.
Client: Succeeded getting IY.
Fy
Client: Ask for an unsupported interface.
QueryInterface: Interface not supported.
Client: Could not get interface IZ.
Client: Get interface IY from interface IX.
QueryInterface: Return pointer to IY.
Client: Succeeded getting IY.
Fy
Client: Get interface IUnknown from IY.
QueryInterface: Return pointer to IUnknown.
Are the IUnknown pointers equal? Yes, pIUnknownFromIY == pIUnknown.
H:\book\Inside COM\CODE\CHAP03>iunknown.exe
Client: Get an IUnknown pointer.
Client: Get interface IX.
QueryInterface: Return pointer to IX.
Client: Succeeded getting IX.
Fx
Client: Get interface IY.
QueryInterface: Return pointer to IY.
Client: Succeeded getting IY.
Fy
Client: Ask for an unsupported interface.
QueryInterface: Interface not supported.
Client: Could not get interface IZ.
Client: Get interface IY from interface IX.
QueryInterface: Return pointer to IY.
Client: Succeeded getting IY.
Fy
Client: Get interface IUnknown from IY.
QueryInterface: Return pointer to IUnknown.
Are the IUnknown pointers equal? Yes, pIUnknownFromIY == pIUnknown.
#2
阿毛朋友,我看的是电子书,没有源程序,能把源程序传给我吗?
whlxy@263.net
谢谢!!
whlxy@263.net
谢谢!!
#3
累死我啦,全是拼写错误,我被骗了,呜。。。。呜。。。。。。
#4
hbs朋友,实在对不起,为了补偿你,我给你20分,望查收。
#5
To whlxy:
源程序给你发了(2-12章),压缩后1.69M
whlxy@263.net
源程序给你发了(2-12章),压缩后1.69M
whlxy@263.net
#6
谢谢 阿毛,我给你加了20分。
另外,你有 《inside atl》的程序吗?
另外,你有 《inside atl》的程序吗?
#7
对不起,我没有.