Jump to content
ChrisChuah

How to Create dll with callback or event functions?

Recommended Posts

Hi

I have an existing component that communicates with a server via TCP.

However, this component can only be used in delphi.

When i call a function in this component, it will send out a TCP message.

After some time, it will return back data and it will trigger an event back in the component.

So, anytime i want to use this component, i can drop the component into the form.

I can double click on the event and write codes to handle the result from the event.

 

Now, if i want to make this component into a DLL for C sharp programmer to use

How can i create events into the dll for C Sharp programmer to use?

 

Is there any example that i can use or follow

 

please help

 

regards

chris

 

Share this post


Link to post
2 hours ago, ChrisChuah said:

Hi

I have an existing component that communicates with a server via TCP.

However, this component can only be used in delphi.

When i call a function in this component, it will send out a TCP message.

After some time, it will return back data and it will trigger an event back in the component.

So, anytime i want to use this component, i can drop the component into the form.

I can double click on the event and write codes to handle the result from the event.

 

Now, if i want to make this component into a DLL for C sharp programmer to use

How can i create events into the dll for C Sharp programmer to use?

 

Create a COM dll, Delphi has a wizard for that. Google for "C# com interop" to find examples for how to use such a thing from the .NET side.

  • Like 1

Share this post


Link to post
type TRequestCallback = procedure (...);

TCallbackStore = class
  procedure DoOnRequest(Sender; ...); // <- will call Callback
  Callback: TRequestCallback;
end;

var
  cbStore: TCallbackStore;

function DoRequest(Data: Pointer; Callback: TRequestCallback)
begin
  cbStore := TCallbackStore.Create;
  cbStore.Callback := Callback;
  requester.OnRequest := cbStore.DoOnRequest;
  requester.Request(Data)
end;

 

Share this post


Link to post
20 hours ago, PeterBelow said:

Create a COM dll, Delphi has a wizard for that. Google for "C# com interop" to find examples for how to use such a thing from the .NET side.

Hi

Actually I want to use delphi to create a DLL with functions or procedures containing a callback.

How can i create such functions in delphi so that the project can be compiled to a dll for C Sharp programmer to use

Previously all DLL functions does not have function pointer or callback functions when i was using Delphi 6 era.

Also my C sharp programmers said they cannot use COM because their program will be running in Docker for linux

 

 

please advise

 

regards

chris

 

Edited by ChrisChuah
update

Share this post


Link to post
18 minutes ago, Fr0sT.Brutal said:

look above

Hi

So sorry it seems like its calling DLL rather than using Delphi to create DLL isnt it?

Its been a long time since i create a DLL but isnt there an export function or something like that?

 

regards

chris

 

Share this post


Link to post
3 hours ago, ChrisChuah said:

Hi

Actually I want to use delphi to create a DLL with functions or procedures containing a callback.

How can i create such functions in delphi so that the project can be compiled to a dll for C Sharp programmer to use

Previously all DLL functions does not have function pointer or callback functions when i was using Delphi 6 era.

Also my C sharp programmers said they cannot use COM because their program will be running in Docker for linux

 

A COM dll usually uses interfaces as callbacks as well, not function pointers.

 

I know nothing about Docker so cannot give you any advice on this topic. .NET has an interface to unmanaged code, which is used to interact with the OS API, though. Your dll would be unmanaged code, you just have to make sure you do not use any Delphi-specific types (especially compiler-managed ones, like String or dynamic arrays) in the functions the DLL exports.

Share this post


Link to post
3 hours ago, ChrisChuah said:

So sorry it seems like its calling DLL rather than using Delphi to create DLL isnt it?

Its been a long time since i create a DLL but isnt there an export function or something like that?

Nope, it's a rough sketch of Delphi DLL just as you want. DoRequest is the function to export.

The idea is to accept a callback function in params, save it in some way and then call it from On* event handler. However, I'm not sure if C# is able to give pointers to functions to DLL.

Edited by Fr0sT.Brutal

Share this post


Link to post

thank you very much for your advices.

Guess i think i will try to use COM dll and see if C sharp can use it or not.

 

 

Share this post


Link to post

Probably you can go with interfaces as well. Like

function DoRequest(Data: Pointer; CallbackIntf: ICallback)

this should be more modern and suitable for C#'s OOP-everywhere-style

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×