大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说ADB Interface_QueryDefine,希望能够帮助大家进步!!!
代码语言:javascript复制STDMETHODIMP CMathFactory::QueryInterface(REFIID riid,void ** ppv)
{
*ppv=NULL;
if(riid==IID_IUnknown||riid==IID_IClassFactory)//这儿不改变也可以!
//if(riid==IID_IUnknown||riid==IID_IClassFactory || riid == IID_IDispatch)//改变
{
*ppv=static_cast<IClassFactory *>(this);
reinterpret_cast<IUnknown*>(*ppv)->AddRef();
return S_OK;
}
else
return (*ppv=0),E_NOINTERFACE;
}
STDMETHODIMP CMath::QueryInterface(REFIID riid, void **ppv)
{// 这里这是实现dynamic_cast的功能,但由于dynamic_cast与编译器相关。
if(riid == IID_ISimpleMath)
*ppv = static_cast<ISimpleMath *>(this);
else if(riid == IID_IAdvancedMath)
*ppv = static_cast<IAdvancedMath *>(this);
else if(riid == IID_IUnknown)
*ppv = static_cast<ISimpleMath *>(this);
else if(riid == IID_IDispatch)
//*ppv = static_cast<ISimpleMath *>(this);//方一
*ppv = static_cast<IAdvancedMath *>(this);//方二,都可以!
else {
*ppv = 0;
return E_NOINTERFACE;
}
reinterpret_cast<IUnknown *>(*ppv)->AddRef(); //这里要这样是因为引用计数是针对组件的
return S_OK;
}