简体   繁体   English

如何使用C#应用程序中的Delphi代码?

[英]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.

相关问题 如何在 C# 桌面应用程序中使用 PHP 代码 - How I can use a PHP code in C# desktop application 为什么我宁愿使用本机dll或com服务器从C#调用Delphi代码? - Why would I rather use a native dll or com server to call Delphi code from C#? 如何在C#项目中使用(VC ++,C ++ Builder,VB,Delphi)的API DLL? - How can I use an API DLL of (VC++, C++Builder, VB, Delphi) in C# Project? 如何使用 C# 或 Delphi 代码中的 pg_dump/pg_restore - How to use pg_dump/pg_restore from C# or Delphi code 我可以在非托管Delphi EXE中使用托管C#DLL吗? - Can I use managed C# DLL in unmanaged Delphi EXE? 我该如何嵌入<iframe>将 HTML 代码转换为 C# 应用程序? - How can I embed <iframe> HTML code into C# application? 如何在 C# 中找到已安装应用程序的升级代码? - How can I find the upgrade code for an installed application in C#? 如何在C#应用程序中使用HTML5地理定位 - How can I use HTML5 geolocation in C# application 如何在C#应用程序中使用Fortran文件? - How can I use a Fortran file in a C# application? 如何在C#控制台应用程序中使用配置替换 - How Can I Use Configuration Replacements for C# Console Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM