15
votes

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

2
Nice hack finding +1. It almost immediately made sense once remembered that .Net header is just an extension of the COFF format. I had great article with details of .Net locations but can't find it now. Here is possibly even better (not simpler) way to do it: codeproject.com/KB/cs/unmanagedtomanaged.aspxuser44298
cont. Another useful utility I found thanks to researching into your solution which maybe helpful to visitors msdn.microsoft.com/en-us/library/ms164699(VS.80).aspxuser44298
@Ivo, this is just the common way of hosting the CLR and using COM interop via mscoree to access your types. (Not sure if even a codeproject article is really useful for something that is already documented on msdn msdn.microsoft.com/en-us/library/dd380850.aspx ) This is perfectly fine for a lot of use cases, especially if you want more than just methods and use a language that has good COM handling like C++ or Delphi. It doesn't help you when you have to have a "real" DLL though. ;-)Robert Giesecke

2 Answers

5
votes

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)

0
votes

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.