我想要将一个类型库(tlb)导入到C#中。
如何将.tlb
导入到.cs
代码文件中?
使用命令行工具tlibimp.exe
,Borland Delphi可以将.tlb
导入到.pas
中:
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)文件中的常量、枚举和接口:
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#中导入类型库的等效方法是什么?
注意:我已经尝试使用随Windows SDK一起提供的tlbimp.exe
,但它将类型库导入到受控程序集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#代码的等效方法是什么?
注意:我想要看到一个包含所有必需接口、常量、枚举的.cs
代码文件,以便调用COM对象。例如:
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
{
}
(但没有错误)
.cs
文件中。你必须小心使用Reflector,而不是ILSpy。ILSpy存在一个错误,它不能保持接口方法的相同顺序。这会导致虚拟表偏移量全部错误,并且代码以可怕和不可能的方式失败。 - Ian Boyd