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