How to find out if an object supports this interface (between two DLLs)

I'm just starting to learn more about how interfaces work, so it doesn't matter if this is a trivial question.

I have two plugins (name them A and B) as a DLL (not packages). There is an interface with a declared GUID in the application that loads the DLL, calls it IMyInterface. Both plugins see the same interface definition with the same GUID. Plugin B actually implements the interface.

Plugin A wants to know if Plugin B supports the IMyInterface interface. I am using obj.GetInterface (IMyInterface, IObj) to find out about this:

var IObj : IMyInterface; obj : TObject; obj := getPluginObjReference; if obj.GetInterface(IMyInterface, IObj) then showmessage ('Interface Supported'); 

If I name this code in plugin B, the answer will be yes, as expected. If I use the same code (cut and paste) in plugin A, the same code claims that plugin B does not support this interface. When I trace the GetInterface call in system.pas, I find that InterfaceEntry: = GetInterfaceEntry (IID); return nil, therefore, no interface to search.

For reference, IMyInterface looks like this:

 IMyInterface = interface ['{277A3122-A3F2-4A14-AE56-C99230F31CE9}'] function getModel : AnsiString; function getDescription : AnsiString; end; 

and the implementation is as follows:

 // Now the real class, this is private to this plugin TModelAPI = class (TInterfacedObject, IMyInterface) function getModel : AnsiString; function getDescription : AnsiString; end; 

and etc.

My question is:

As expected, plugin B rightly claims to be supported by IMyInterface. Why can't plugin A detect that plugin B supports IMyInterface? Is there a problem with polling interfaces across DLL boundaries?

+4
source share
2 answers

You cannot reliably pass objects across DLL boundaries. Instead, you should pass interfaces across the border and use the capabilities of as or Supports for queries. Interfaces are intended for binary compatibility between DLL boundaries, but objects are not.

You can easily transfer IInterface from one DLL to another and then request this. Or, if you have a common interface that implements all the objects of the plugin, you can pass this. All that matters is that you always go through interfaces and never skip objects.

+7
source

You should really only use interfaces, i.e. getPluginObjReference should return the lowest common interface supported by all plugins, and then you use Support () to check which interfaces (plugin versions) are supported by a particular plugin.

+6
source

Source: https://habr.com/ru/post/1385780/


All Articles