将 TLB 导入 C#
我想将类型库 (tlb) 导入到 C# 中。
如何将 .tlb
导入到 .cs
代码文件中?
Borland Delphi 可以使用命令行工具tlibimp.exe
将.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# 等效项是什么将类型库转换为本机 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
What is the .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
{
}
(除非没有错误)
另请参阅
- 将接口 IDL 文件转换为 C#
- 如何从 C# 读取非托管代码的 TLB(类型库)?
- <一个href="https://stackoverflow.com/questions/3994848/net-c-is-it-possible-to-import-tlb-semi-automatically-and-add-preservesig-to">.NET C#:是吗可以(半)自动导入 TLB 并将 PreserveSig 添加到一种类型吗?
- 类型库导入程序 (Tlbimp.exe)
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经找到了 .Net 等效项 Tlbimp .exe - 输出是一个程序集,不幸的是没有办法改变它。
如果您想查看接口等的 C# 声明...那么您应该使用反编译器(例如 Reflector 或 ILSpy) 在生成的程序集中。此外,Microsoft 关于如何修改这些声明的官方建议是修改生成的 MSIL - 请参阅 自定义主要互操作程序集
。
(目前)唯一的替代方案是自己手工制作所有声明。
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.
要注册类型库,您应该使用 regtlib.exe,如下所示:
导航到以下文件夹并将文件路径复制到剪贴板:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regtlibv12.exe
(实际文件夹路径可能会有所不同,具体取决于计算机上安装的 .NET Framework 版本。)
(这也可能位于 C:\WINDOWS\system32\URTTemp\regtlib.exe)
复制路径
打开命令窗口并执行以下命令,
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regtlibv12.exe "Full path of .TLB file"
这应该说注册.......tlb 成功
打开Visual Studio 并创建一个 C# 控制台应用程序。右键单击“引用”,选择“添加引用...”,然后浏览到 tlb 文件。
这应该给出 dll/tlb 的引用。右键单击名称并选择“在对象浏览器中查看...”展开树以查看可能使用的所有类型、调用和事件。
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.