HRESULT IUnknown::QueryInterface(REFIID iid, void** ppv)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. If the object supports an interface which matches the ID, it gives back a pointer which can be cast into a pointer to the correct type of interface.
It's pretty ugly sounding, but think of it as a C++ DynamicCast. It's essentially navigation by typecasting!!!
Or, consider that its really an AbstractFactory that takes a 128-bit guaranteed globally unique identifier (GUID) as the key.
Different from a conventional FactoryPattern, in that it always returns another pointer to the >same< object (not a pointer to a newly created object).
Same conceptual object anyway. There is nothing to prevent you from using a group of associated objects to implement a COM component.
Why didn't they just implement respondsTo(aMethodName) and polymorphism? Is this some clever way to make the computer's job easier at the expense of the human's? ;->
It's actually equivalent to respondsTo(aWholeInterfaceNotJustAMethodName) and it is polymorphic, although not type safe in the C/C++ language mapping. Typing and type safety are a language issue, and because COM is language neutral it cannot guarantee type safety, that must be provided by a language mapping that provides access to COM services from a specific language.
In C++ one can do this:
IUnknown *the_object = ... // this supports the Foo and Bar interfaces IBarInterface *bar; the_object->QueryInterface( IID_FOO, &bar ); // this works fine bar->Bar( "hello, world." ); // crash! bar actually points at an IFoo
I have a theory on the origins of QueryInterface(): http://www.lesher.ws/queryinterface.html --TimLesher
Ha! I've long suspected that's where Microsoft was getting its design ideas...
Or is that an example of DuckTyping? ;)
Note: QueryInterface is more like a DownCast? than a CrossCast?. A CrossCast? occurs when the type being cast to is not a subtype of the type you have. Since all COM types are subtypes of IUnknown, it's a DownCast?. See DynamicCast for more (though this page is full of CeePlusPlus specific stuff)--ScottJohnson
Why doesn't the Java bridge for UNO make the proxy objects implement the exported interfaces automatically?
Having to write
XFoo xFoo = (XFoo) UnoRuntime?.queryInterface(XFoo.class, obj);totally sucks, when with normal Java interfaces, you'd write
XFoo xFoo = (XFoo)obj;No idea. VisualBasic classic, VisualBasicDotNet, CeeSharp, and ActiveTemplateLibrary all allow you to QueryInterface through typecasts.