Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of using the QueryInterface method? (Direct3D)

Tags:

c++

direct3d

i understand what the QueryInterface method actually does- it only returns a pointer to a specific interface. But my question is, why would i want to use this method?

I mean, what is the difference between

QueryInterface(__uuidof(IDXGIResource), (void**)&Resource)

and

IDXGIResource * Resource

aren't these pretty much the same? if so, why would i even need to use the method? For what reasons should i use it?

like image 392
Soon_to_be_code_master Avatar asked Aug 05 '15 00:08

Soon_to_be_code_master


People also ask

What does QueryInterface do?

QueryInterface is a mechanism in COM (microsoft's Component Object Model) for determining if a known component supports a specific interface. You use the current interface pointer (usually IUnknown), and "query" the interface by passing an interface ID to it.

What is QueryInterface C++?

In this article Regardless of implementation, this method queries an object using the IID of the interface to which the caller wants a pointer. If the object supports that interface, QueryInterface retrieves a pointer to the interface, while also calling AddRef . Otherwise, it returns the E_NOINTERFACE error code.

What is IUnknown interface?

IUnknown interface (unknwn. Enables clients to get pointers to other interfaces on a given object through the QueryInterface method, and manage the existence of the object through the AddRef and Release methods. All other COM interfaces are inherited, directly or indirectly, from IUnknown.

What is Refiid?

The riid parameter is the GUID that identifies the interface you are asking for. The data type REFIID is a typedef for const GUID& . Notice that the class identifier (CLSID) is not required, because the object has already been created. Only the interface identifier is necessary.


1 Answers

COM assumes that a single object will provide multiple interfaces, i.e. the interfaces will be fine-grained and you'll want to use more than one of them at a time. QueryInterface is how you get a pointer to those other interfaces on an object.

Your examples are incomplete. The first doesn't show that QueryInterface is being called from an existing interface, and the second doesn't assign any value to the pointer (it's an uninitialized pointer). A practical example would combine the two:

IDXGIResource * Resource = NULL;
pInterface->QueryInterface(__uuidof(IDXGIResource), (void **)&Resource);

To be robust you should make sure that the QueryInterface call succeeded before you try to use the pointer.

if (SUCCEEDED(pInterface->QueryInterface(__uuidof(IDXGIResource), (void **)&Resource))
    Resource->DoSomething();
like image 86
Mark Ransom Avatar answered Oct 01 '22 17:10

Mark Ransom