Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

com - Import TLB into C#

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:DevelopSopQuotingEngineActiveX_TLB.dcr
Created C:DevelopSopQuotingEngineActiveX_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 FilesMicrosoft SDKsWindowsv7.1BinNETFX 4.0 Toolsx64lbimp" 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...