Import TLB into C#
Asked Answered
L

2

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

Lucent answered 29/4, 2011 at 15:34 Comment(4)
why isn't having the assembly enough? you can reference it in your projects, you can reflect on it if you want to see what it contains. why do you need C# source code?Aflutter
@Marius Bancila: Single binary.Lucent
@IanBoyd how did you get around this in the end? I have seen the answer below, but this makes me very unhappy... :'[Goree
@Killercam Eventually i used Reflector to copy and paste the entire contents of the assembly in to a new .cs file. You have to be careful to use Reflector, and not ILSpy. There's a bug in ILSpy that it doesn't keep interface methods in the same order. This causes your virtual table offsets to all be wrong, and the code to fail in horrible and impossible ways.Lucent
P
14

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.

Pleonasm answered 29/4, 2011 at 15:52 Comment(1)
+1 for cannot be done. And +1 for ILSpy, i didn't know about that.Lucent
L
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.

Leonialeonid answered 15/4, 2014 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.