Sign in to follow this  
derek7

QueryInterface

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 baseiface

DECLARE_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

Share this post


Link to post
Share on other sites
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??

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
This is COM we're talking about. Throw all this talk of base classes and derived classes out the window. If you have an interface pointer and want to find out if it supports another interface, just call QueryInterface and be done with it.

Lets assume you have this object already:
IDirectXFileObject *pObject = (some pointer to an object);

If you want to find out of that object supports IDirectXFileData, just do this:


IDirectXFileData *pFileData;
HRESULT hr = pObject->QueryInterface(__uuidof(IDirectXFileData), (void **)&pFileData);
if (SUCCEEDED(hr))
{
// do something with pFileData
//
// When done, release it
pFileData->Release();
}



Forget about how QueryInterface works, and forget about whether or not an object inherits from or derives from a given interface. The only time you need to worry about that is if you are implementing a COM object.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this