【问题标题】:Importing .NET assembly into Delphi fails将 .NET 程序集导入 Delphi 失败
【发布时间】:2017-09-24 13:57:51
【问题描述】:

我正在尝试将 .NET 程序集导入 Delphi 以创建“_TLB.pas”文件,但收到错误消息:

是什么导致了这个错误?

.NET 程序集是否没有使用正确的程序集属性构建?

XE4 可以导入的 .NET 版本(4.6.1)有限制吗?

下面显示的 .NET 程序集是在 .NET 4.6.1 上使用 VS 2012 创建的。

使用 Delphi XE4,遵循以下步骤:

  • 组件 -> 导入组件 -> 导入类型库
  • 点击“添加”
  • 浏览到 .NET 程序集。

选择程序集时,出现错误。

构建 .NET 程序集后,我成功地将其放入 GAC (gacutil),将其注册到程序集 (regasm),并将其作为组件添加到 COM+ 应用程序中。

我尝试使用regasm 创建.tlb 并导入它,但出现同样的错误。

ObjectCache.cs:

using System;
using System.EnterpriseServices;
using System.Runtime.InteropServices;

namespace TestObjectCache
{
    [Guid("7B1FEFFD-1569-404F-B7EB-6AAB903B8779")]
    [ComVisible(true)]
    public interface IObjectCache
    {
        Boolean GetTrue();
    }

    [Guid("80C986C4-6B1F-4E3C-9B5E-13943DD1E1FD")]
    [ComVisible(true)]
    [ProgId("ProgId.ObjectCache")]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class ObjectCache:ServicedComponent, IObjectCache
    {
        public ObjectCache()
        {
        }
        public Boolean GetTrue()
        {
            return true;
        }
    }
}

AssemblyInfo.cs:

[assembly: ApplicationActivation(ActivationOption.Server)]
[assembly: ApplicationName("Object Cache")]
[assembly: Description("Caches commonly used objects")]
[assembly: ApplicationAccessControl(false)]

【问题讨论】:

  • 我不能肯定地说,但对此的“直觉”感觉是 .NET 命名空间“EnterpriseServices”正在阻止 Delphi 导入其类型库。如果不使用'EnterpriseServices',仍然使用InteropServices,则可以将类型库导入Delphi。可能是由于“EnterpriseServices”无法使用 Windows api。如果我有时间验证这一点,我会更新这篇文章。
  • 我只是使用 InteropServices 而不是 EnterpriseServices。

标签: .net delphi .net-assembly delphi-xe4 tlb


【解决方案1】:

通过 Visual Studio 的 Tlbexp.exe 工具运行 Assembly 以生成 .tlb 文件,然后将该文件导入 Delphi。

否则,请尝试使用导入器的“导入 .NET 程序集”选项而不是“导入类型库”选项。

【讨论】:

  • 我在尝试导入从 Tlbexp.exe 生成的 .tlb 作为从 regasm 生成的 .tlb 时遇到相同的错误。使用“导入 .NET 程序集”也失败了,但此选项不允许创建 _TLB.pas 文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多