1

I have a DLL written in C#, which I want to use in my Delphi application.

I was able to import the DLL correctly (using its TLB file) into Delphi. I can create an instance of that class from DLL, I can call its methods, all that works fine.

I don't know how I can register to receive events occurring in that DLL (it receives data from a physical device).

I have a C# DLL and a C# test app using that DLL where it is way simpler (it simply adds a handler for that event), but I can't achieve that in Delphi.

I have written my own small C# DLL where an event exists (in the same way as it does in the original DLL). Here is the code for it:

My C# DLL:

namespace ClassLibrary3
{
    public class FlowDataEventArgsMine : EventArgs
    {
        public double Flow { get; set; }
    }

    public class Class3
    {
        public string Hello()
        {
            return "Hello World";
        }

        public event EventHandler<FlowDataEventArgsMine> FlowDataReceivedMine;
    }
}

How this is used in a C# App:

c = new Class3();

c.FlowDataReceivedMine += new EventHandler<FlowDataEventArgsMine>(OnNewFlowDataReceivedMine);

private void OnNewFlowDataReceivedMine(object sender, FlowDataEventArgsMine e)
{
  // this method is called when the event provides new flow data
}

In Delphi:

CL := TClass3.Create(NiL);
res0 := CL.Hello;  //this works OK, I can call methods from that class

CL.FlowDataReceivedMine := MyMethodToBeCalled;  //this is NOT OK - how can I register for that event? It's not even showing that event as a part of this Class

CL.Free;

Obviously, I tried to read every article, question and post I could find. I have a feeling that I read half of the Internet already, but I can't find anything which would point me to a solution.

Can anybody point me to a working example, where I can achieve the same in Delphi as what's simple in C# (I mean, simply adding a method to be called when an event happens)?

EDIT:

Isn't that really possible to receive any sort of info that an event was fired in the C# Dll when using it in my Delphi app? During research I encountered some info that exported class from C# DLL needs to implement some interface, and in my Delphi app I should implement the same interface and this would allow me to handle events from that DLL (but didn't found any full solution as example and what I found was not enough to make working solution for me).

This post looks promising, author stated that he was able to achieve that but unfortunately the steps given there don't work for me and step by step blog is not reachable anymore:

Exposing C# COM server events to Delphi client applications

Feels like running in circles and still not sure what is possible and what's not :/

Lukasz P.
  • 11
  • 3
  • C# and Delphi events are not compatible. Make your DLL exporting C-style functions and callback parameters. http://rvelthuis.de/articles/articles-dlls.html – Delphi Coder Aug 03 '22 at 09:20
  • @DelphiCoder the .NET Runtime Library for Delphi has a [page about handling and raising events](https://crystalnet-tech.com/RuntimeLibrary/Help/html/index.htm?page=Events.html), isn't that exactly what OP is asking for? – MindSwipe Aug 03 '22 at 09:25
  • @MindSwipe This looks very interesting! – Delphi Coder Aug 03 '22 at 09:33
  • You can add a data member to C# class as a delegate (not event) and assign your C++ function pointer to that delegate. The delegate can be called in C# with the same syntax of the event – Marco Beninca Aug 03 '22 at 09:35
  • @MarcoBeninca: So if I understand well, I would need to create a method in my Delphi app with the same signature as added delegate existing in C# DLL, add also a method where I will pass to it my Function pointer from Delphi. And in the method which normally raises an event in C# DLL I would call that delegate which will result in calling my method in my Delphi app? Do you have any link with example achieving that? Thanks in advance for your response. – Lukasz P. Aug 03 '22 at 10:25
  • @LukaszP. yes this is the idea... events are nothing more than a collection of delegates in .NET – Marco Beninca Aug 03 '22 at 10:26
  • Did it somehow work? If you do, can you post it here? – Marco Giovanni Jul 12 '23 at 19:18

1 Answers1

0

I'm by no means a Delphi expert (in that I haven't written a single line of Delphi before) but I was able to find this which explains how to handle a event in Delphi which was raised by C#, basically you need to create a function pointer to a function you want to invoke, and that function must have the exact same parameters as the C# event, something like

procedure MyMethodToBeCalled(ASender: _ClrObject; AEventArgs: _ClrEventArgs); stdcall;
begin
  // Your code
end;

And then hook it up by calling a Add[YourEventNameHere] method

begin
  MyMethodPointer := MyMethodToBeCalled;

  // add
  CL.AddFlowDataReceivedMine(ni, @MyMethodPointer);

  // remove
  CL.RemoveFlowDataReceivedMine(ni, @MyMethodPointer);
end;

This uses the .NET Runtime Library for Delphi

MindSwipe
  • 7,193
  • 24
  • 47
  • Yes, I saw that too, and this looks exactly what I would need, but this implies buying some 3rd party components for every developer in the company, which I was hoping that can be avoided. – Lukasz P. Aug 03 '22 at 10:27