Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a .Net Assembly in Delphi without registering it in the GAC or COM?

i have a simple task:

is it possible to write a Delphi DLL and put a .Net Assembly (with only one interface with 4 methods and one class implementing the interface) besides it and call it from the Delphi DLL?

I mean, can i import the .Net types directly from the .Net assembly (relative filename) if i create a tlb and a delphi unit for the tlb, without registering the Assembly/tlb?

best, thalm

EDIT (what i found):

Most solutions must register at least one dll/tlb for COM. But the most promising thing i found was: Unmanaged Exports from Robert Giesecke, its a Visual Studio project template which lets you write static C# (or whatever .Net language) methods and call them from any unmanaged language, awesome:

class Test
{
    [DllExport("add", CallingConvention = CallingConvention.StdCall)]
    public static int Add(int left, int right)
    {
        return left + right;
    } 
}

EDIT 2: It really works! You can even control the type marshalling, unbeliveable!!!

like image 920
thalm Avatar asked Jan 09 '11 23:01

thalm


1 Answers

One little piece of advice: You do not have to make your exports public.

Your class is internal already, so it wouldn't show up when consumed from another assembly.

However, it is also perfectly fine to add an export to an existing static class, but declare it as private so that it doesn't show up when consumed from .Net. (unmanaged Exports tend to look a bit creepy)

like image 144
Robert Giesecke Avatar answered Sep 28 '22 16:09

Robert Giesecke