QueryInterface

Started by
9 comments, last by rmsimpson 17 years, 11 months ago
pointer->QueryInterface (id,ppvoid); I think pointer is the same as ppvoid in value. only different is type. the QueryInterface see if a object derived from a interface(base class). if yes, then it convert pointer type to interface type Am I right? so IDirectXFileData is interface. and IDirectXFileObject derived from it? right?
Advertisement
Sort of. COM objects don't (usually) work by deriving, they work by containing other objects. In your example, the implementation of IDirectXFileData may contain a member variable which is a IDirectXFileObject.
Where'd you get that info? I always thought that COM was usually implemented via inheritance (interfaces and stuff), rather than aggregation.
daerid@gmail.com
Quote:Original post by derek7
pointer->QueryInterface (id,ppvoid);

I think pointer is the same as ppvoid in value. only different is type.
the QueryInterface see if a object derived from a interface(base class).
if yes, then it convert pointer type to interface type

Am I right?


so IDirectXFileData is interface. and IDirectXFileObject derived from it?

right?


Since IDirectXFileData derives from IDirectXFileObject, it is safe to cast an IDirectXFileData pointer to an IDirectXFileObject pointer. It is NOT permissible to cast the other way however -- from IDirectXFileObject to IDirectXFileData.

However, QueryInterface does not check to see if the object inherits or derives from the requested interface -- that's not how QueryInterface is designed to work.

COM objects are allowed to aggregate interfaces. They're allowed to inherit interfaces too. In the case of aggregation, the COM object is even allowed to postpone creation of aggregated objects until an interface is requested that requires that object.

You should not (generally) cast interface pointers from one type to another unless you are casting from a derived interface to a base interface.

Robert
WRONG ! rmsimpson. IDirectXFileObject derive from IDirectXFileData .

object->QueryInterface(interface,pointer) first detect object is derive from interface. if yes ,it convert object to interface pointer.


anyone know that?




another question. what is different between RTTI and DTI(Dynamic Type Infomation)

do RTTI have the stuff DTI?
does the COM queryinterface function has DTI stuff?
I mean sometimes I need to know which type a object is, sometimes I need to know which interface a ojbect derive from. all is about type info. the stuff seem to be implement in total different class.
Well, derek7, my limited understanding is that COM exposes a pointer to a vector of function pointers (an interface) and takes advantage of the vtable implicit in a c++ class with virtual functions to wrap these functions in an interface we can use without needing to cast all the pointers to the correct type of function manually.

I am not aware of any direct relationship between COM interface inheritance and C++ inheritance, hence why you can also access COM through C.

Oh, and I think you mean QueryInterface(Interface,(void**)&Pointer), or it won't work.
Quote:Original post by derek7
WRONG ! rmsimpson. IDirectXFileObject derive from IDirectXFileData .

"To derive from" means to inherit from. This is how IDirectXFileData is declared in the DirectX headers:

#define DECLARE_INTERFACE_(iface, baseiface)    interface DECLSPEC_NOVTABLE iface : public baseifaceDECLARE_INTERFACE_(IDirectXFileData, IDirectXFileObject){    IUNKNOWN_METHODS(PURE);    IDIRECTXFILEOBJECT_METHODS(PURE);    STDMETHOD(GetData)          (THIS_ LPCSTR, DWORD *, void **) PURE;    STDMETHOD(GetType)          (THIS_ const GUID **) PURE;    STDMETHOD(GetNextObject)    (THIS_ LPDIRECTXFILEOBJECT *) PURE;    STDMETHOD(AddDataObject)    (THIS_ LPDIRECTXFILEDATA) PURE;    STDMETHOD(AddDataReference) (THIS_ LPCSTR, const GUID *) PURE;    STDMETHOD(AddBinaryObject)  (THIS_ LPCSTR, const GUID *, LPCSTR, LPVOID, DWORD) PURE;};


Therefore, quite clearly, IDirectXFileData derives from IDirectXFileObject.

Quote:
object->QueryInterface(interface,pointer) first detect object is derive from interface. if yes ,it convert object to interface pointer.

another question. what is different between RTTI and DTI(Dynamic Type Infomation)
do RTTI have the stuff DTI?
does the COM queryinterface function has DTI stuff?
I mean sometimes I need to know which type a object is, sometimes I need to know which interface a ojbect derive from. all is about type info. the stuff seem to be implement in total different class.


QueryInterface() doesn't DETECT anything usually. When the COM-enabled class is declared, and QueryInterface() is IMPLEMENTED by the class, that FUNCTION can do *anything* it likes to determine whether or not to return a pointer for a given RIID. While yes, it is *possible* that someone could use RTTI to implement QueryInterface, it is atypical for one to do so.

A typical implementation of QueryInterface will use a lookup table present in the class definition which explicitly declares which interfaces to expose and explicitly how to expose them. They could be exposed through inheritance, through aggregation, or any of a number of different ways.

COM is implemented by many languages. If you insist on trying to box COM into a C++ paradigm, you're just going to give yourself a GP fault.

Robert
rmsimpson , could you explain me a little more detail, what does QueryInterface do?

HRESULT QueryInterface(
REFIID iid,
void ** ppvObject
);


I think the QueryInterface do the stuff(not care how to implement):

see if object derived from iid, if yes return ppvObject.

so I think IDirectXFileObject derive from IDirectXFileData .

but If IDirectXFileData derived from IDirectXFileObject

what does IDirectXFileObject ::QueryInterface (IDirectXFileData object id,(void**)pointer) do??
Quote:
what does IDirectXFileObject ::QueryInterface (IDirectXFileData object id,(void**)pointer) do??


If you have an IDirectXFileObject pointer and want to find out if that object implements IDirectXFileData, then you must call QueryInterface. It is not permitted for you to promote a base interface to a derived interface in COM outside of the QueryInterface mechanism.

If the object exposes that interface, then QueryInterface will give you a valid pointer to that interface. Notice I said expose and not implements. An object of type IDirectXFileObject is not required to implement IDirectXFileData, whereas an object that implements IDirectXFileData is required to implement IDirectXFileObject.

Now then ... it is likely that QueryInterface() for IDirectXFileData on an IDirectXFileObject pointer will return you exactly the same pointer you already had. In all probability, it will. However, you cannot assume in COM. You must call QueryInterface.

I can't tell you how a particular object implements QueryInterface, I can only tell you different ways that it can implement QueryInterface. The implementations vary by language and are unimportant to you, the consumer of the object.

Robert
what does "expose a interface" mean?

Can a interface expose a interface that derived from ? could you explain a little more. a sample is good.

This topic is closed to new replies.

Advertisement