Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call C# dll from Delphi

I composed .Net 3.5 dll with single method, which is to be called by Delphi .exe. Unfortunately it does not work.

The steps: 1. Create C# 3.5 dll with the code:

public class MyDllClass
{
    public static int MyDllMethod(int i)
    {
        MessageBox.Show("The number is " + i.ToString());
    }
}
  1. Go to Assembly Properties --> Assembly Information and checked the "Make Assembly COM-Visible"
  2. Used RegAsm.exe to register my dll

This throws Delphi exception which indicates it cannot connect the dll. What are the steps required to enabled usage of C# managed dll from unmanaged code.

Does any one familiar with good example about the subject?

Thank you

like image 360
Roi Shabtai Avatar asked May 30 '11 09:05

Roi Shabtai


People also ask

What is call C?

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.

What is Call by need in C?

Call by need is a memoized variant of call by name, where, if the function argument is evaluated, that value is stored for subsequent use. If the argument is pure (i.e., free of side effects), this produces the same results as call by name, saving the cost of recomputing the argument.

What is call by value in C?

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. By default, C programming uses call by value to pass arguments.


2 Answers

You may have more luck skipping the COM part by using my project template for unmanaged exports

class MyDllClass
{
    [DllExport]
    static int MyDllMethod(int i)
    {
        MessageBox.Show("The number is " + i.ToString());
        return i + 2;
    }
}

In Delphi, you'd import it like so:

function MyDllMethod(i : Integer) : Integer; stdcall; extern 'YourAssembly.dll';

I had to vote your question down, though. For not even caring as much as to provide code that would compile. (your C# method doesn't return a value, yet it expects as int)

like image 95
Robert Giesecke Avatar answered Sep 20 '22 10:09

Robert Giesecke


After massive investigation I found the solution: it's all about registration parameters. The flag /codebase must be added to the regasm command.

Many posts out there suggest to use Guid and other COM attributes on the C# Com exposed object, I managed to deliver COM functionality using the ComVisible(true) attribute and regasm /tlb /codebse command.

The code:

[Guid("7DEE7A79-C1C6-41E0-9989-582D97E0D9F2")]
[ComVisible(true)]
public class ServicesTester
{
    public ServicesTester()
    {
    }

    //[ComVisible(true)]
    public void TestMethod()
    {
        MessageBox.Show("You are in TestMEthod Function");
    }
}

and as I mentioned I used regasm.exe /tlb /codebase to register it

like image 22
Roi Shabtai Avatar answered Sep 20 '22 10:09

Roi Shabtai