简体   繁体   English

使用Delphi 7访问托管代码(CLR)DLL

[英]access managed code ( CLR ) DLL with Delphi 7

How delphi7 access C# .net managed dll ? delphi7如何访问C#.net管理的dll?

i'm trying to access some DLL that compiled in c# and they are not those old style DLL. 我正在尝试访问一些用c#编译的DLL,它们不是那些旧式DLL。 ( i dont have source for those DLL) (我没有这些DLL的来源)

i did tried to search online but they are confusing. 我确实试图在网上搜索,但他们很困惑。 i guess there is some limitation even if get to access it with D7 .. 我猜即使用D7访问它也有一些限制。

thanks. 谢谢。

@AngryHacker, For what you have suggested to be true, the .NET Assembly needs to be marked with the ComVisibleAttribute ( Register for COM Interop option enabled), and expose each function to COM using the [ComVisible(true)]. @AngryHacker,对于你的建议是真的,.NET程序集需要用ComVisibleAttribute(启用COM Interop选项注册)标记,并使用[ComVisible(true)]将每个函数公开给COM。

If you are trying to use a third party assembly (which you did not write) you have to check with the vendor whether that assembly can be used through COM. 如果您尝试使用第三方程序集(您没有编写),则必须与供应商核实是否可以通过COM使用该程序集。

Also the assembly does NOT need to be strong named to be accessible via COM. 此外,程序集不需要强名称可通过COM访问。

If you do not want to go the COM way, check out CrossTalk for Delphi : http://www.atozed.com/CrossTalk/Docs/index.EN.aspx 如果您不想使用COM方式,请查看CrossTalk for Delphi: http//www.atozed.com/CrossTalk/Docs/index.EN.aspx

Delphi7 accesses managed DLLs in the same manner as any other COM-based language (such as VB6). Delphi7以与任何其他基于COM的语言(例如VB6)相同的方式访问托管DLL。

The basic drill is this: 基本演练是这样的:

  1. Make sure your .NET is strong-named. 确保您的.NET具有强名称。
  2. Run it through regasm 通过regasm运行它
  3. Drop it into the GAC 将其放入GAC

Now Delphi7 should be able to reference the TLB file that was generated by regasm as it would any other COM-based DLL. 现在Delphi7应该能够引用由regasm生成的TLB文件,就像任何其他基于COM的DLL一样。

You could try to use the "Inverse P/Invoke" trick as described by Brian Long a long time ago. 很久以前你可以尝试使用Brian Long所描述的“反向P /调用”技巧

That trick does not need ComVisible (so you should be able to use it without changing the sources of the .NET assembly). 这个技巧不需要ComVisible(所以你应该能够在不改变.NET程序集的来源的情况下使用它)。

--jeroen --jeroen

That's not hard at all. 这根本不难。 You do not have to "regasm" it if you don't want to. 如果你不想,你不必“诅咒”它。 You don't even have to register anything COM-ish at all. 你根本不必注册任何COM-ish。

You'd be best served by creating a wrapper in C# or another managed language. 通过使用C#或其他托管语言创建包装器,您将获得最佳服务。 Oh and make sure your classes are ComVisible(true). 哦,确保你的课程是可见的(真实的)。 This Stack Overflow thread sums it up quite nicely, and itcomes with a set of examples for different scenarios. 这个Stack Overflow线程很好地总结了它,并且它通过一组用于不同场景的示例来实现。 Hosting CLR in Delphi with/without JCL - example 使用/不使用JCL在Delphi中托管CLR - 示例

If you don't have the ability to change the source of the original DLL, you can write a proxy in C# or Prism that exposes a com interface that you can use in your project. 如果您无法更改原始DLL的源代码,则可以使用C#或Prism编写代理,以公开可在项目中使用的com接口。 The advantage of this approach is you can tailor each call to just use specific types you can handle easily. 这种方法的优点是您可以定制每个调用,只使用您可以轻松处理的特定类型。

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

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