简体   繁体   中英

calling a delphi5 procedure in c# dll

i have a big problem. I have a delphi5 application which calls ac# dll. i want to call a function from my c# dll which needs a pointer to a delphi procedure as input parameter. In this c# function the delphi procedure is called afterwards.

I tried to declare the function pointer at the c# side by using IntPtr:

ourFunction(IntPtr fct){
   ...
   helpFct = (OurType)Marshal.GetDelegateForFunctionPointer(fct, typeof(OurType));
   ...
}

If i call the function from c# everything works fine. But if i want to call it from Delphi (with a delphi procedure as input parameter) it crashes without giving me any information about the error.

here is my delphi code:

hBuffer : THandle;
buffer : PInteger;
...
hBuffer:=GlobalAlloc(GMEM_fixed,SizeOf(Integer));
buffer:=GlobalLock(hBuffer);
buffer := Addr(AddDelphi);
intfRef.ourFunction(buffer^);

Has someone experience with such problems, or some ideas how it may work?

thanks Stefan

If it's Delphi 5, that's pre .NET. Your best bet would be to regasm the C# assembly, and access it as a COM object from within the Delphi 5 application, in order to work.

Of course you may also need to review and ensure that the calls are COM-compatible.

I'm not sure it works in Delphi 5, but in the past I have been successful integrating .NET things written in C# into Delphi using RemObjects Hydra (Hydra also can do the other way around: integrating Delphi things in .NET).

Function pointers will be difficult, as the .NET run-time and the Delphi run-time are quite a bit different.

What you might want to try is to export your Delphi functions in a DLL, and use P/Invoke from the C#/.NET side to import them. .NET will then marshall from the Delphi DLL into the .NET way of doing things and back.

Finally, you can try to host the .NET run-time in Delphi using the JCL TJclClrHost (see this SO question for more into in that direction).

--jeroen

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM