Soon_to_be_code_master
Soon_to_be_code_master

Reputation: 323

What is the purpose of using the QueryInterface method? (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?

Upvotes: 9

Views: 12157

Answers (1)

Mark Ransom
Mark Ransom

Reputation: 308391

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();

Upvotes: 13

Related Questions