ChrisChuah 0 Posted May 25, 2022 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
PeterBelow 238 Posted May 25, 2022 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. 1 Share this post Link to post
Fr0sT.Brutal 900 Posted May 25, 2022 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
ChrisChuah 0 Posted May 26, 2022 (edited) 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 May 26, 2022 by ChrisChuah update Share this post Link to post
Fr0sT.Brutal 900 Posted May 26, 2022 41 minutes ago, ChrisChuah said: please advise look above Share this post Link to post
ChrisChuah 0 Posted May 26, 2022 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
PeterBelow 238 Posted May 26, 2022 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
Fr0sT.Brutal 900 Posted May 26, 2022 (edited) 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 May 26, 2022 by Fr0sT.Brutal Share this post Link to post
ChrisChuah 0 Posted May 26, 2022 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
Fr0sT.Brutal 900 Posted May 27, 2022 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
Remy Lebeau 1394 Posted May 27, 2022 On 5/26/2022 at 4:15 AM, Fr0sT.Brutal said: However, I'm not sure if C# is able to give pointers to functions to DLL. Yes, it can: Marshalling a Delegate as a Callback Method Share this post Link to post
Remy Lebeau 1394 Posted May 27, 2022 Since there is a lot of talk of COM in this discussion, have a look at the COM tutorials on this website: http://www.techvanguards.com/com/, especially the section on Events and Callbacks. Share this post Link to post