简体   繁体   English

如何在Delphi中使用.Net程序集而无需在GAC或COM中注册?

[英]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? 有没有可能写一个Delphi DLL并放一个.Net程序集(只有一个接口有4个方法和一个实现接口的类)除了它并从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? 我的意思是,如果我为tlb创建一个tlb和一个delphi单元,而不注册Assembly / tlb,我可以直接从.Net程序集导入.Net类型(相对文件名)吗?

best, thalm 最好的,thalm

EDIT (what i found): 编辑(我找到了):

Most solutions must register at least one dll/tlb for COM. 大多数解决方案必须为COM注册至少一个dll / tlb。 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: 但我发现最有希望的事情是:来自Robert Giesecke的Unmanaged Exports ,它是一个Visual Studio项目模板,它允许你编写静态C#(或任何.Net语言)方法,并从任何非托管语言调用它们,真棒:

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

EDIT 2: It really works! 编辑2:它确实有效! 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. 但是,将导出添加到现有静态类也是完全正常的,但将其声明为私有,以便在从.Net使用时不会显示。 (unmanaged Exports tend to look a bit creepy) (非托管出口往往看起来有点令人毛骨悚然)

You can do this using Registration Free COM. 您可以使用免费注册COM执行此操作。 See my answer to a question on Registration free com here . 请参阅我在这里注册免费com的问题的答案。 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. 使用.NET程序集,您需要使您的接口和方法COMVisible,就像您将使用已注册的COM使用该对象一样。 If you then follow my answer on the question I just mentioned, you should be able to put the dll side by side. 如果你按照我刚才提到的问题回答我的答案,你应该能够将dll并排放置。 The only difference is you need to put information in the assembly manifest (the managed one) about the COM classes you are exporting. 唯一的区别是您需要将有关要导出的COM类的信息放入程序集清单(受管理的清单)中。 If you look at the documentation on the microsoft site regarding application and assembly manifests, you should find out how to do this. 如果您查看有关应用程序和程序集清单的microsoft站点上的文档,您应该了解如何执行此操作。 The manifest attribute you are looking for is CLRClass . 您正在寻找的清单属性是CLRClass If you have everything setup properly, you just put the managed dll side by side with the calling executable and everything works. 如果您已正确设置所有内容,则只需将托管dll与调用可执行文件并排放置即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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