[英]How can I use Delphi code from a C# application?
I have a Delphi project, and I need to write a C# application, but I want to use some functions from this Delphi project. 我有一个Delphi项目,我需要编写一个C#应用程序,但我想使用这个Delphi项目中的一些函数。 I haven't worked in Delphi before.
我之前没有在Delphi工作过。
I found that I can create a DLL from the Delphi code and use it directly in C#. 我发现我可以从Delphi代码创建一个DLL并直接在C#中使用它。 How can I do that?
我怎样才能做到这一点? Or I also found some conversion tools to convert to C#, but it wasn't so good.
或者我也发现了一些转换为C#的转换工具,但它并不是那么好。 So what is the best way?
那么最好的方法是什么? DLL or convert?
DLL还是转换?
Here is a very quick sample explains how to make dll in Delphi and then how to call it from C#. 这是一个非常快速的示例,解释了如何在Delphi中创建dll,然后如何从C#调用它。 Here is Delphi code of simple dll with one function SayHello:
这里是带有一个函数SayHello的简单dll的Delphi代码:
library DllDemo;
uses
Dialogs;
{$R *.res}
Procedure SayHello;StdCall;
Begin
ShowMessage('Hello from Delphi');
End;
exports
SayHello;
begin
end.
Compile this code and it will produce a dll file. 编译此代码,它将生成一个DLL文件。
now, the C# code to call the previous procedure in the dll is like this: 现在,在dll中调用上一个过程的C#代码是这样的:
using System.Runtime.InteropServices;
namespace CallDelphiDll
{
class Program
{
static void Main(string[] args)
{
SayHello();
}
[DllImport("DllDemo")]
static extern void SayHello();
}
}
Put the Delphi produced dll in the C# project output directory and run the C# application. 将Delphi生成的dll放在C#项目输出目录中并运行C#应用程序。
Now, expand this simple sample to achieve your requirement. 现在,扩展这个简单的示例以满足您的要求。
As already suggested via a DLL (you need to flag the functions with an appropriate calling convention like stdcall) 正如已经通过DLL建议的那样(你需要使用适当的调用约定来标记函数,比如stdcall)
A different solution would be to wrap the relevant functionality in a COM object, and use that. 另一种解决方案是将相关功能包装在COM对象中,并使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.