16

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

2
  • How exactly are you trying to connect the object? Are you importing it using the wizard (which generates a wrapper unit for you) or are you loading the library directly? Commented May 30, 2011 at 12:51
  • I found the solution for these issue - see the answer below Commented Jun 22, 2011 at 7:43

3 Answers 3

38

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)

5
  • Hi Robert, I can't see the template after placing the zip file in the proper place (My Documents\Visual Studio 20**\Templates\ProjectTemplates). I'm using VS2010, are you familiar with it? Commented Jun 1, 2011 at 6:28
  • You did download the UnmanagedExportLibrary.zip, right? The page with the samples has a link to DllExport.zip, which is just the build task and some libraries, not the template. If you did use the template: Go to Tools/Options/Projects and Solutions. There you'll find the property "User project templates location". This is the folder in which VS will search for project templates. Commented Jun 1, 2011 at 7:38
  • Hi Robert. I just want to ask if you have any Idea why I get a nil address (no entry point in the dll) trying to use this example in Delphi? Commented Jun 28, 2019 at 21:50
  • @RobertGiesecke How from C#/DLL return string to Delphi? Additionally Delphi parameter ansistring to C#/DLL only ok if return function int.
    – mih
    Commented Mar 6, 2023 at 17:07
  • @mih the default marshaling is the equivalent of PAnsiChar in Delphi. If you want unicode, then you can mark your function in C# with "[return: MarshalAs(UnmanagedType.BStr)]", and in Delphi, it would be PWideChar. but it's been years since I've written Delphi... Commented Mar 29, 2023 at 9:45
10

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

1
  • Special thanks for telling us what worked!
    – NoChance
    Commented Apr 21, 2022 at 4:50
3

One thing that could be a problem if you made your assembly x64 or AnyCPU. Since Delphi is 32 bit (x86) you need to make your assembly x86 or make sure regasm.exe registers it also in the 32 bits registry. You do that by using the x86 version of regasm.exe.

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