将 TLB 导入 C#

Import TLB into C#(将 TLB 导入 C#)

本文介绍了将 TLB 导入 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i want to import a Type Library (tlb) into C#.

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

How do i import a .tlb into a .cs code file?

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

Borland Delphi can import a .tlb into .pas by using the command line tool tlibimp.exe:

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

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

And now there is a .pas source code file containing constants, enumerations, interfaces that were inside the compiled Type Library (tlb) file:

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# 等效项是什么?

What is the .NET C# equivalent for importing a type library into native C# code?

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

Note: i have tried using tlbimp.exe that comes with the Windows SDK, but that imports a type library into a managed assembly dll:

C:Develop>"c:Program FilesMicrosoft SDKsWindowsv7.1BinNETFX 4.0 Toolsx64	lbimp" 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# 等效项是什么?

What is the .NET C# equivalent for importing a type library into native C# code?

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

Note: What i want to see is a .cs code file with all the required interfaces, constants, enumerations - everything required to call the COM object. For examples sake:

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
{
}

(除了没有错误)

  • 将接口 IDL 文件转换为 C#
  • 如何读取非托管代码的TLB(类型库)来自 C#?
  • .NET C#:是否可以(半)自动导入 TLB 并将 PreserveSig 添加到一种类型?
  • 类型库导入器 (Tlbimp.exe)

推荐答案

您已经找到了 .Net 等价物 Tlbimp.exe - 这个输出是一个程序集,不幸的是没有办法改变它.

You've already found the .Net equivalent, Tlbimp.exe - The output from this is an assembly and unfortunately there is no way to change this.

如果您想查看接口等的 C# 声明......那么您应该使用反编译器(例如 Reflector 或 ILSpy) 在生成的程序集中.此外,微软关于如何修改这些声明的官方建议是修改生成的 MSIL - 请参阅 自定义主要互操作程序集.

If you want to see C# declarations of interfaces etc... then you should use a decompiler (such as Reflector or ILSpy) on the resulting assembly. Also the official advice from Microsoft on how to modify these declarations is to modify the resulting MSIL - see Customizing Primary Interop Assemblies .

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

The only alternative to this (currently) is to hand craft all the declarations yourself.

这篇关于将 TLB 导入 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将 TLB 导入 C#