In Delphi you can pass class references around to compare the types of objects, and to instantiate them. Can you do the same with interface references being passed to a COM automation server?
For example, you can define a method taking a GUID parameter using the type library editor:
function ChildNodesOfType(NodeType: TGUID): IMBNode; safecall;
In this function I would like to return automation types that support the interface specified by NodeType, e.g.
if Supports(SomeNode, NodeType) then
result := SomeNode;
But the Supports call always fails, I tried passing in the Guids defined in the type library but none of the different types (Ixxx, Class_xxxx, IId_Ixxxx) seem to work.
The SysUtils unit comes with at least five overloads of
Supports, and they all accept aTGUIDvalue for their second parameters.You can indeed pass interface types as parameters, but they’re really just GUIDs. That is, when a function expects a
TGUIDargument, you can pass it the interface type identifier, such asIMBNodeorIUnknown. For that to work, though, the interface type needs to include a GUID in its declaration, like this:When the first parameter to
Supportsis an interface reference, the function calls itsQueryInterfacemethod. If it returnsS_OK, thenSupportsreturn true; otherwise, it returns false. When the first parameter is an object reference, then it first calls the object’sGetInterfacemethod to get itsIUnknowninterface, and callsSupportson that like before. If it doesn’t work that way, then it falls back to asking for the requested interface directly fromGetInterface. If you’ve implementedQueryInterfacecorrectly on your object, or if you’ve used the default implementation fromTInterfacedObject, then everything should work fine.If
Supportsnever returns true for you, then you should revisit some assumptions. Are you sure your node really supports the interface you’re requesting? Go make sure the class declaration includes that interface. Make sureQueryInterfaceis implemented properly. And make sureSomeNodeactually refers to the node you’re expecting it to.