【问题标题】:Import TLB into C#将 TLB 导入 C#
【发布时间】:2011-08-15 14:25:18
【问题描述】:

我想将类型库 (tlb) 导入 C#。

如何将.tlb 导入.cs 代码文件?


Borland Delphi 可以使用命令行工具tlibimp.exe.tlb 导入.pas

C:\Develop>tlibimp.exe SopQuotingEngineActiveX.tlb
Borland TLIBIMP Version 5.1  Copyright (c) 1997, 2000 Inprise Corporation
Type library loaded...
Created C:\Develop\SopQuotingEngineActiveX_TLB.dcr
Created C:\Develop\SopQuotingEngineActiveX_TLB.pas

现在有一个 .pas 源代码文件,其中包含已编译的类型库 (tlb) 文件中的常量、枚举、接口:

SopQuotingEngineActiveX_TLB.pas

unit SopQuotingEngineActiveX_TLB;

interface
...
const
   CLASS_XSopQuotingEngine: TGUID = '{3A46FFB8-8092-4920-AEE4-0A1AAACF81A0}';
...

// *********************************************************************//
// Interface: IXSopQuotingEngine
// Flags:     (4416) Dual OleAutomation Dispatchable
// GUID:      {AA3B73CC-8ED6-4261-AB68-E6AE154D7D52}
// *********************************************************************//
  IXSopQuotingEngine = interface(IDispatch)
    ['{AA3B73CC-8ED6-4261-AB68-E6AE154D7D52}']
    procedure OnStartPage(const AScriptingContext: IUnknown); safecall;
    procedure OnEndPage; safecall;
    procedure Connect(const ConnectionString: WideString); safecall;
    procedure Disconnect; safecall;
    function  xmlRateQuote(const xmlQuote: WideString): WideString; safecall;
  end;
  ...

  CoXSopQuotingEngine = class
    class function Create: IXSopQuotingEngine;
  end;

将类型库导入本机 C# 代码的 .NET C# 等效项是什么?


注意:我曾尝试使用 Windows SDK 附带的 tlbimp.exe,但会将类型库导入托管程序集 dll:

C:\Develop>"c:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\NETFX 4.0 Tools\x64\tlbimp" SopQuotingEngineActiveX.tlb
Microsoft (R) .NET Framework Type Library to Assembly Converter 4.0.30319.1
Copyright (C) Microsoft Corporation.  All rights reserved.

TlbImp : warning TI3002 : Importing a type library into a platform agnostic assembly.  This can cause errors if the type library is not truly platform agnostic.
TlbImp : Type library imported to SopQuotingEngineActiveX.dll

将类型库导入本机 C# 代码的 .NET C# 等效项是什么?


注意:我想看到的是一个.cs 代码文件,其中包含所有必需的接口、常量、枚举——调用COM 对象所需的一切。例如:

SopQuotingEngineActiveX.cs

[ComImport, Guid("AA3B73CC-8ED6-4261-AB68-E6AE154D7D52")
       ]
public interface IXSopQuotingEngine
{
    void OnStartPage(object AScriptingContext);
    void OnEndPage();
    void Connect(string ConnectionString);
    void Disconnect();
    string xmlRateQuote(string xmlQuote);
}

[ComImport, Guid("3A46FFB8-8092-4920-AEE4-0A1AAACF81A0")]
public class XSopQuotingEngineClass
{
}

(除了没有错误)

另见

【问题讨论】:

  • 为什么装配不够?你可以在你的项目中引用它,如果你想看看它包含什么,你可以反思它。为什么需要 C# 源代码?
  • @Marius Bancila:单个二进制文件。
  • @IanBoyd 你最后是怎么解决这个问题的?我已经看到了下面的答案,但这让我很不高兴......:'[
  • @Killercam 最终我使用 Reflector 将程序集的全部内容复制并粘贴到一个新的 .cs 文件中。你必须小心使用 Reflector,而不是 ILSpy。 ILSpy 中存在一个错误,即它不会使接口方法保持相同的顺序。这会导致您的虚拟表偏移全部错误,并且代码以可怕和不可能的方式失败。

标签: c# com typelib


【解决方案1】:

您已经找到了 .Net 等效项 Tlbimp.exe - 此输出是一个程序集,不幸的是无法更改它。

如果您想查看接口等的 C# 声明......那么您应该在生成的程序集中使用反编译器(例如 ReflectorILSpy)。此外,微软关于如何修改这些声明的官方建议是修改生成的 MSIL - 请参阅Customizing Primary Interop Assemblies

(目前)唯一的替代方法是自己手工制作所有声明。

【讨论】:

  • +1 表示无法完成。 +1 ILSpy,我不知道。
【解决方案2】:

要注册一个类型库,你应该使用regtlib.exe,如下:

导航到以下文件夹并将文件路径复制到剪贴板: C:\Windows\Microsoft.NET\Framework\v4.0.30319\regtlibv12.exe (实际文件夹路径可能因计算机上安装的 .NET Framework 版本而异。) (这也可能位于 C:\WINDOWS\system32\URTTemp\regtlib.exe)

复制路径 打开命令窗口并执行以下命令,

C:\Windows\Microsoft.NET\Framework\v4.0.30319\regtlibv12.exe ".TLB 文件的完整路径"

这应该说注册.......tlb成功

打开 Visual Studio 并创建一个 C# 控制台应用程序。右键单击 References,选择 Add Reference...,然后浏览到 tlb 文件。

这应该提供对 dll/tlb 的引用。右键单击名称并选择,在对象浏览器中查看...展开树以查看所有可能使用的类型、调用和事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-02
    • 2010-11-14
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多