C# plugin to fire an Event in Delphi Host

Hello,
I want to fire/propagate an Event in Delphi Host from C# plugin.
I declared interface IMyHost and Host’s application main form is inherited from IMyHost.
Then by some reason in C# plugin this.Host appears not to be IMyHost

Do we have some basic example of Callbacks/Events from C# to Delphi?

Thanks in advance,

Hello

This is the sample you need: Need simple sample - #2 by antonk

The code there is old and might not even build in the latest Hydra versions, still you should see the approach where host sets the callback site and plugin raises its events:

unit uCustomInterfaces;

interface

uses
  uHYCrossPlatformInterfaces;

type

  ImyInterfaceEvents = interface;

  IInterfaceThatHasEvents = interface(IHYCrossPlatformInterface)
  ['{BE177C76-B8B0-47CE-B268-1BE1A3006866}']
    procedure SetEventSink(Sink: ImyInterfaceEvents); safecall;
  end;

  ImyInterfaceEvents = interface(IHYCrossPlatformInterface)
  ['{2227E41C-DC30-4B1E-8482-80430C0199BF}']
    procedure ButtonClick(Sender: IInterfaceThatHasEvents); safecall;
  end;


implementation

end.

Regards

Thank you Anton,
in general it works!
Main Idea is to register(e.g. SetEventSink ) Host with Plugin by passing there
Host’s inteface (e.g. ImyInterfaceEvents).
Then from the Plugin the Host’s Interface can trigger a method in Host.

In my code it all works if I’m firing the event from main thread of the Plugin,
but my Plugin uses Threads and If I call the event from threaded function I’m getting the following error:

ex.Message "Unable to cast COM object of type ‘System.__ComObject’ to interface type ‘MyModule.IMyHost’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{EA13C76E-47C9-49C6-A0A8-65D701B99C65}’ failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

Are there some tweaks to handle such issue?
Thanks in advance,

Host and Plugin communicate using COM interop

Unfortunately COM doesn’t like multithreaded access (ie plugins cannot be asccessed from other threads). You need to somehow perform all plugin calls from the same thread.

gotcha