How to create a COM Interop DLL in C# (simple example)

Filed Under (development) by sith on 21-09-2008

Tagged Under : , ,

(* this is a legacy post from one of my earlier blogging attempts, better formatting to come)

Well, it finally happened. I had to force myself to figure out how the fsck to call a method in a C# assembly from a Delphi 5 Win32 application. The following is the culmination of all of the buttons that I clicked and swearing that I sweared…

And now our feature presentation.

1. Create a new Class Library project in Visual Studio 2005. Let’’s call it “InteropExample”.
2. Open the AssemblyInfo.cs file and set the COM visible attribute to true.

[assembly: ComVisible(true)]

3. Go to Project Properties –> Build. Check the option Register for Com Interop to “Selected”.
4. Open your class file and add the foloowing to the using clause

using System.Runtime.InteropServices;

5. In this class file, create the interface that will help expose methods to the unmanaged world.

public interface IMathFunctions
{
    int Add(int Number1, int Number2);
}

6. Create the method that will be called.

[ClassInterface(ClassInterfaceType.None)]
public class ComInteropExample : IMathFunctions
{
    public int Add(int Number1, int Number2)
    {
        try
        {
            return Number1 + Number2;
        }
        catch
        {
            return 0;
        }
    }
}

7. Now it’’s time to create the type library that we will use in Delphi (or any other unmanaged language). This type library can be used for late binding. In our example, the assembly will be private, so copy the assembly into the folder where the Delphi compiled executable will be (they need to live in the same folder). If this assembly is going to be public, you need to assign a strong key to the assembly. I”ll cover that in another tutorial (when I learn more about it myself…hahahahah).

Open a command prompt and type the following:

regasm InteropExample.dll /tlb:InteropExample.tlb

8. Switch over to Delphi, create or open a project and click Project –> Import Type Library. Select your InteropExample.tlb and click Create Unit. The pascal file that is created can now be used in your program to call the managed methods.
9. In your main unit, add “ComObj” and “InteropExample_TLB” (or whatever your generated unit is called) to your uses list.
10. Now we can slap some code in to run this bad boy

procedure TForm1.Button1Click(Sender: TObject);
var
  intfMath: IMathFunctions;
  result: Integer;
begin
  intfMath := CreateComObject(CLASS_InteropExample) as IMathFunctions;
  result := intfMath.Add(2, 2);
  ...
end;

* CLASS_InteropExample might not be the actual name, review the pascal file that was created in Step 8

11. THE MOMENT OF TRUTH…

Run your Delphi app, click the button and watch the magic…

12. When deploying on another machine, you will probably have to register the C# assembly. Make sure that the assembly is living in the same folder as the Delphi executable, then from a command line

regasm InteropExample.dll

Enjoy.

Comments:

Leave a Reply