Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ATL COM COM+
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();
Posted 26-Jan-11 22:47pm
Edited 27-Jan-11 0:12am
CPallini309.1K
v2
Comments
Ryan Zahra at 27-Jan-11 5:04am
   
Is IGreet registered correctly? How are you registering it?
Is IGreet2 inheriting from IGreet?
Ganesamoorthy.S at 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 at 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 at 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 at 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.
Ganesamoorthy.S at 27-Jan-11 6:40am
   
Thanks a lot
Ganesamoorthy.S at 28-Jan-11 7:01am
   
Accept Answer Reject Answer
.I have completed this on my own.
 
Adding interface to a COM, is as usual but if we suppose to use QUERYINTERFACE then it has to be implemented(implement interface option must be chosen). As you have instructed me in the threads it has to be inherited.
 
I did it and got the expected result.
Ryan Zahra at 28-Jan-11 9:00am
   
Good to hear :)

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
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;
    }
}
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 404
1 Maciej Los 239
2 Sergey Alexandrovich Kryukov 182
3 Richard MacCutchan 130
4 CPallini 120
0 OriginalGriff 5,474
1 Sergey Alexandrovich Kryukov 4,713
2 Peter Leow 2,944
3 DamithSL 2,465
4 Maciej Los 2,434


Advertise | Privacy | Mobile
Web04 | 2.8.140718.1 | Last Updated 27 Jan 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid