C# .NET - Calling a C# DLL

Asked By Justin P on 05-May-09 02:14 PM
Sorry if this is a JV question, but I'm new to C# and I'm still finding my way in Delphi as well. I am writing something for an App that is written in Delphi. I am calling DLLs that are written in VB6. We are currently in the process of migrating from Delphi to C# with the App. I need to write a DLL that bridges with the VB6 DLLs. I am writing this DLL in C#. I need to call this DLL from Delphi. The problem that I am having is how to do this from within Delphi (we are working with Delphi 7). Anyone have any suggestions for calling the C# DLL from my Delphi App?
Santhosh N replied to Justin P on 05-May-09 03:15 PM
You could actually make the C# dll com visible and call them in others using interop...
check here for more info..

http://msdn.microsoft.com/en-us/library/aa645712(VS.71).aspx

here is the solution

H K replied to Justin P on 05-May-09 04:09 PM
To load C# dll into Delphi you need to do the following:

1.  C# class as a COM object, to achieve this put all your methods in one or more interfaces then make the class implement each of those interfaces.
2.
register the assembly as a COM object in the registry using  regasm.

Sample code as shown below:
// IAddInterface.cs
public interface IAddInterface
{
    int Add(int i1, int i2);
}

// SimpleDll.cs
using System.Runtime.InteropServices;

[ClassInterface(ClassInterfaceType.None)]
public class SimpleDLL : IAddInterface
{
    public int Add(int i1, int i2)
    {
        return i1+i2;
    }
}

csc /t:library /out:SimpleDll.dll *.cs

Next, register the assembly as a COM object in the registry using:
Code:

regasm SimpleDll.dll /tlb:Add.tlb


In Delphi, click Project->Import Type Library (like you did before). Select your .tlb (Add.tlb in this case) and click Create Unit.
main unit add "ComObj" and "SimpleDLL_TLB"

procedure TForm1.Button1Click(Sender: TObject);
var
  intfRef: IAddInterface;
  result: Integer;
begin
  intfRef := CreateComObject(CLASS_SimpleDLL_) as IAddInterface;
  result := intfRef.Add(2, 2);

Note: Delphi .exe needs to be in the same directory as the .NET DLL




link

Perry replied to Justin P on 05-May-09 09:51 PM
Look at http://forums.devshed.com/delphi-programming-90/c-dll-need-to-be-used-in-delphi-231122.html for what you are asking.
Re :: Calling a C# DLL in Delphi
Shailendrasinh Parmar replied to Justin P on 06-May-09 12:14 AM

See the following forum post, you will get the answer

http://forums.devshed.com/delphi-programming-90/c-dll-need-to-be-used-in-delphi-231122.html

http://www.eggheadcafe.com/conversation.aspx?messageid=31222240&threadid=31222240

Hope this helps.

It works
Justin P replied to H K on 08-May-09 03:26 PM
Thanks guys for all your help. The examples were exactly what I was looking for, and I was able to get the code to work.