TLB导入到C# [英] Import TLB into C#

查看:1803
本文介绍了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 code>:

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:\Develop\SopQuotingEngineActiveX_TLB.dcr
Created C:\Develop\SopQuotingEngineActiveX_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;



什么是.NET C#相当于导入类型库到本地的C#代码?

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

注意:我已经尝试使用 tlbimp.exe是自带的Windows SDK的,但进口类型库进入托管程序集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 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



什么是.NET C#的等效?导入类型库到本地的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
{
}

(除了没有错误)

  • Convert Interface IDL file to C#
  • How to read TLB(Type Libraries)of unmanaged code from C# ?
  • .NET C#: Is it possible to import TLB (semi-)automatically and add PreserveSig to one type?
  • Type Library Importer (Tlbimp.exe)

推荐答案

您已经找到了净相当于 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#声明..然后你应该使用反编译器(如反射或的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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆