本文介绍了QueryInterface问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我试图通过使用以下代码段来使用QueryInterface. com组件是由我创建的,它具有两个接口.但是"queryinterface"返回E_NOINTERFACE
.
请Kindlly帮我解决这个问题.
谢谢,
Ganesamoorthy
Hi All,
I have tried to use QueryInterface by using the following code segment. the com component had been created by me and which has two interfaces. But the "queryinterface" returns E_NOINTERFACE
.
Kindlly help me, to come out of this issue.
Thanks,
Ganesamoorthy
HRESULT hr;
IGreet *greetObject;
IGreet2 *greetObject2;
CString strTemp;
hr = CoInitialize(NULL);
if (FAILED(hr) )
return ;
hr = CoCreateInstance(CLSID_Greet2,NULL,CLSCTX_INPROC_SERVER, IID_IGreet2,(void **) & greetObject2);
if (FAILED(hr) )
{
CoUninitialize();
return ;
}
greetObject2->AddRef();
cout << greetObject->Sayhello("Ganesh") << endl;
CComBSTR bstrVal, bstrVal1;
bstrVal1.Append ("Ganesh");
greetObject2->SayHello2(bstrVal1, & bstrVal);
strTemp.Format (_T("%s"), bstrVal);
MessageBox (strTemp);
hr = greetObject2->QueryInterface(IID_IGreet,(void **)& greetObject);
DWORD dw = GetLastError();
if (FAILED(hr) )
{
CoUninitialize();
return ;
}
CoUninitialize();
推荐答案
HRESULT __stdcall Greet2::QueryInterface(REFIID riid, void **ppObj)
{
if(riid == IID_IUnknown || riid == IID_IGreet2)
{
*ppObj = static_cast<IGreet2*>(this) ;
reinterpret_cast<IUnknown*> (*ppObj)->AddRef();
return S_OK;
}
else if(riid == IID_Greet)
{
*ppObj = static_cast<IGreet*>(this) ;
reinterpret_cast<IUnknown*> (*ppObj)->AddRef();
return S_OK;
}
else
{
*ppObj = NULL;
return E_NOINTERFACE;
}
}
这篇关于QueryInterface问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!