Click here to Skip to main content
15,512,502 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
 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();
Posted
Updated 27-Jan-11 1:12am
v2
Comments
Ryan Zahra 27-Jan-11 5:04am    
Is IGreet registered correctly? How are you registering it?
Is IGreet2 inheriting from IGreet?
Ganesamoorthy.S 27-Jan-11 5:55am    
During build of the project the component is getting registered properly.

And i am able to get any one of the objects using createinstance. But i am unable to use Queryinterface.

IGreet2 is not inherited from IGreet.
Ganesamoorthy.S 27-Jan-11 5:56am    
During build of the project the component is getting registered properly.

And i am able to get any one of the objects using createinstance. But i am unable to use Queryinterface.

IGreet2 is not inherited from IGreet.
Ganesamoorthy.S 27-Jan-11 6:31am    
I am a beginner in COM, can you please clarify me?

I am using VS2005. How can i inherit the interface? Is there any wizard to do this?
Ryan Zahra 27-Jan-11 6:37am    
It's difficult to explain COM in a few lines. Here's a good tutorial to get you started:
http://www.codeproject.com/kb/com/CCOMThread.aspx#COM_APARTMENTS
http://www.codeproject.com/KB/COM/CCOMThread2.aspx

Also I recommend reading the book Inside COM by Dale Rogerson which is very simple to read and understand. I've learned COM from that book.

1 solution

Your problem is that IGreet2 is not inherited from IGreet. You must inherit IGreet in IGreet2. In the QueryInterface of IGreet2, you must create an instance of IGreet.

C
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;
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900