简体   繁体   中英

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!!!

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)

You can do this using Registration Free COM. See my answer to a question on Registration free com here . With a .NET assembly, you need to make your interfaces and methods COMVisible, as if you were going to use the object using registered COM. If you then follow my answer on the question I just mentioned, you should be able to put the dll side by side. The only difference is you need to put information in the assembly manifest (the managed one) about the COM classes you are exporting. If you look at the documentation on the microsoft site regarding application and assembly manifests, you should find out how to do this. The manifest attribute you are looking for is CLRClass . If you have everything setup properly, you just put the managed dll side by side with the calling executable and everything works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM