在c# dll中调用delphi5过程
我有一个大问题。我有一个 delphi5 应用程序,它调用 ac# dll。我想从我的 c# dll 调用一个函数,它需要一个指向 delphi 过程的指针作为输入参数。在此 C# 函数中,随后调用 delphi 过程。
我尝试使用 IntPtr 在 c# 端声明函数指针:
ourFunction(IntPtr fct){
...
helpFct = (OurType)Marshal.GetDelegateForFunctionPointer(fct, typeof(OurType));
...
}
如果我从 c# 调用该函数,一切正常。但是,如果我想从 Delphi 调用它(使用 Delphi 过程作为输入参数),它就会崩溃,并且不会给我任何有关错误的信息。
这是我的delphi代码:
hBuffer : THandle;
buffer : PInteger;
...
hBuffer:=GlobalAlloc(GMEM_fixed,SizeOf(Integer));
buffer:=GlobalLock(hBuffer);
buffer := Addr(AddDelphi);
intfRef.ourFunction(buffer^);
有人遇到过这样的问题,或者有一些想法如何工作?
谢谢 斯特凡
i have a big problem. I have a delphi5 application which calls a c# 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果是 Delphi 5,那就是 .NET 之前的版本。最好的选择是
regasm
C# 程序集,并从 Delphi 5 应用程序中将其作为 COM 对象访问,以便工作。当然,您可能还需要检查并确保调用是 COM 兼容的。
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.
我不确定它在 Delphi 5 中是否有效,但过去我已经使用 RemObjects Hydra(Hydra 也可以采取相反的方式:将 Delphi 的东西集成到 .NET 中)。
函数指针会很困难,因为 .NET 运行时和 Delphi 运行时有很大不同。
您可能想要尝试的是将 Delphi 函数导出到 DLL 中,然后使用 C#/.NET 端的 P/Invoke 来导入它们。然后,.NET 将从 Delphi DLL 编组到 .NET 处理方式并返回。
最后,您可以尝试使用 JCL TJclClrHost 在 Delphi 中托管 .NET 运行时(请参阅 这个SO问题以了解更多关于这个方向的信息)。
——杰罗恩
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