Import TLB into C#

2019-01-25 13:11发布

问题:

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

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


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

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;

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


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

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


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

(except without the bugs)

See also

  • 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)

回答1:

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.

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.



回答2:

To register a type library, you should use regtlib.exe as follows:

Navigate to the following folder and copy the file path to clipboard: C:\Windows\Microsoft.NET\Framework\v4.0.30319\regtlibv12.exe (the actual folder path may be different depending on the .NET Framework version installed on your computer.) (This may also be located in C:\WINDOWS\system32\URTTemp\regtlib.exe)

Copy the path Open a command window and execute the following command,

C:\Windows\Microsoft.NET\Framework\v4.0.30319\regtlibv12.exe "Full path of .TLB file"

This should say Registration .......tlb successful

Open Visual Studio and create a C# console app. Right click on References, select Add Reference... and then browse to the tlb file.

This should give a reference to the dll/tlb. Right click on the name and select, View in Object Browser... Expand the tree to see all the types, calls and events that may be used.