QueryInterface and Release Etc

Top  Previous  Next

If possible use CComQIPtr. If you insist you can read the following...


There is some confusion in this question's answers about what QueryInterface actually does. It simply retrieves pointers to the supported interfaces on an object and increments the reference count on that object. It doesn't create a new object for each interface that it implements.


For example if you have an object which implements 2 interfaces, then the call would simply cast that object as each of the interface and increment a variable which is used as the reference count.


QueryInterface allows the caller to retrieve references to different interfaces the component implements. It is similar to dynamic_cast<> in C++. Specifically, it is used to obtain a pointer to another interface, given a GUID that uniquely identifies that interface (commonly known as an interface ID, or IID). If the COM object does not implement that interface, an E_NOINTERFACE error is returned instead.


COM object gives you some pointers to certain functions that you can call to manipulate the object.


COM object is basically a C++ class. A C++ class is just a struct that always starts with a pointer to its VTable (an array of function pointers). And the first three pointers in the VTable will always be named QueryInterface, AddRef, and Release. What additional functions may be in its VTable, and what the name of their pointers are, depends upon what type of object it is.


This is another important rule of COM. If you get hold of a COM object created by someone else, you must call its Release function when you're done with it.


CComQIPtr is a CComPtr with a QueryInterface inside it, apparently.


Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)