6

I have a Delphi project, and I need to write a C# application, but I want to use some functions from this Delphi project. I haven't worked in Delphi before.

I found that I can create a DLL from the Delphi code and use it directly in C#. How can I do that? Or I also found some conversion tools to convert to C#, but it wasn't so good. So what is the best way? DLL or convert?

2
  • 2
    if your delphi code is native then you'll need a dll and some pinvokes Commented May 18, 2011 at 9:35
  • 1
    the best way is to create a dll and import it in c#
    – RBA
    Commented May 18, 2011 at 11:03

2 Answers 2

13

Here is a very quick sample explains how to make dll in Delphi and then how to call it from C#. Here is Delphi code of simple dll with one function SayHello:

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.

now, the C# code to call the previous procedure in the dll is like this:

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.

Now, expand this simple sample to achieve your requirement.

0
1

As already suggested via a DLL (you need to flag the functions with an appropriate calling convention like stdcall)

A different solution would be to wrap the relevant functionality in a COM object, and use that.

Not the answer you're looking for? Browse other questions tagged or ask your own question.