I have created the WPF VB.net plugin that is hosted by a Delphi 11.3 application. The plugin works, but I’m trying to establish callback events from vb.net to delphi.
Here is my .net Interface
Imports System.Runtime.InteropServices
Imports RemObjects.Hydra.CrossPlatform
Public Delegate Sub NotifyEventHandler(ByVal sender As Object)
<ComVisible(True)>
<Guid("E2FDD28C-19DC-41DF-8D6B-ADCA3104B8A1")> ' ← Generate a new GUID (see below)
Public Interface ISpeechKitPlugin
Inherits IHYCrossPlatformInterface
' Methods that Delphi can call on the plugin (.NET)
Sub StartSpeech()
Sub StopSpeech()
Function GetCurrentText() As String
Sub SetText(ByVal value As String)
' Properties (optional - implemented as getter/setter methods)
Function GetIsActive() As Boolean
Sub SetIsActive(ByVal value As Boolean)
' Event setters - Delphi will assign its handlers here
Sub Set_OnSpeechStarted(ByVal handler As NotifyEventHandler)
Sub Set_OnSpeechStopped(ByVal handler As NotifyEventHandler)
Sub Set_OnTextChanged(ByVal handler As NotifyEventHandler)
' Optional: Method the plugin can call on the host (if host implements it)
Sub HostCommand(ByVal command As String)
End Interface
Here is the imported interface in delphi. ( I had to define the interface in VB.net as the import from Delphi to vb.net in visual studio 2022 doesn’t seem to exist. I also had to add the NotifyEventHandler type as the hydra tool doesn’t seem to create it.
interface
uses
Hydra.Core.Interfaces;
type
NotifyEventHandler = procedure(sender: TObject) of object;
type
ISpeechKitPlugin = interface;
// Original name: DiaSpeechKitPlugin.ISpeechKitPlugin
ISpeechKitPlugin = interface(IHYCrossPlatformInterface)
['{E2FDD28C-19DC-41DF-8D6B-ADCA3104B8A1}']
procedure StartSpeech; safecall;
procedure StopSpeech; safecall;
function GetCurrentText: WideString; safecall;
procedure SetText(const value: WideString); safecall;
function GetIsActive: WordBool; safecall;
procedure SetIsActive(const value: WordBool); safecall;
procedure Set_OnSpeechStarted(const handler: NotifyEventHandler); safecall;
procedure Set_OnSpeechStopped(const handler: NotifyEventHandler); safecall;
procedure Set_OnTextChanged(const handler: NotifyEventHandler); safecall;
procedure HostCommand(const command: WideString); safecall;
end;
implementation
end.
In the main Delphi application I define a call back procedure.
procedure OnSpeechStartedHandler(Sender: TObject);
Then Later on in the application I try to use the interface to assign the procedure.
if Supports(fPluginInstance, ISpeechKitPlugin, fSpeechKit) then
begin
fSpeechKit.StartSpeech;
fSpeechKit.SetText(‘Hello from Delphi’);
// Assign event handlers
fSpeechKit.Set_OnSpeechStarted(OnSpeechStartedHandler);
end;
When the “Set_OnspeechStarted(OnSpeechStartedHandler); is called the application crashes hard. I cannot find an example of a TnotifyEvent callback event in the samples. What is the proper way to implement this? The AI Chat bots have offered varying solutions that don’t work.
Thanks in advance for any help.