In vtable the first three address of functions are the IUknown interface methods then after that comes our own methods .Now the question is why not get the pointer to the vtable and iterate through the vtable to reach the method we want ?What role does QuerryInterface() plays ?Why should i use QuerryInterface().
Who is Participating?
kkarunakarConnect With a Mentor Commented:
Hi Sam,
It is not matter to discuss that we can get the pointer of vtable and iterate thru that.The Important thing is to note here is that . The architecure of COM is like that..We have to use QueryInterface() function of COM object to get the interface it supports. After getting the interface pointer we can access the method, event , properties of the Interface. For showing this it is done thry vtable. COM is said to be platform independent and language independent. And there are languages which does not have vtable concept. In that case we can't use ur logic.
And after all they have implemented like this architecture.
And It is more robust.And U will find benefit of this when u will forward with other chapters..
Regards keshav
r_sam_14Author Commented:
Please give your email id so that we can exchange technical ideas.
QueryInterface is kind of a query booth where you ask from a COM object about the inercaces it supports. Some interface designer don't use QI for this purpose especially if you have more than interfaces implemented then the scripting languages can not excess interface other than that has been marked as default. In that case you may come across implementation like

HRESULT GetInerfaceFoo1 ();
HRESULT GetInterfaceFoo2 ();

The purpose for this type of design is that you specifically ask for an interface from a COM object.

C implementation does the same thing as getting to a method by applying offset to vtable pointer.

Andif you look at the implementation of ATL, it has similar kind of approach. You will come across ATL_THUNKxxx.
All Courses

From novice to tech pro — start learning today.