Tlbexp.exe(类型库导出程序)Tlbexp.exe (Type Library Exporter)

类型库导出程序生成一个类型库,该类型库描述公共语言运行时程序集中定义的类型。The Type Library Exporter generates a type library that describes the types defined in a common language runtime assembly.

此工具会自动随 Visual Studio 一起安装。This tool is automatically installed with Visual Studio. 若要运行此工具,请使用 Visual Studio 开发人员命令提示(或 Windows 7 中的 Visual Studio 命令提示)。To run the tool, use the Developer Command Prompt for Visual Studio (or the Visual Studio Command Prompt in Windows 7). 有关详细信息,请参阅命令提示For more information, see Command Prompts.

在命令提示符处,键入以下内容:At the command prompt, type the following:

语法Syntax

tlbexp assemblyName [options]  

参数Parameters

参数Argument 说明Description
assemblyNameassemblyName 为其导出类型库的程序集。The assembly for which to export a type library.
选项Option 说明Description
/asmpath: directory/asmpath: directory 指定要在其中搜索程序集的位置。Specifies the location to search for assemblies. 如果使用此选项,则必须显式指定要在其中搜索所引用的程序集的位置(包括当前目录)。If you use this option, you must explicitly specify the locations to search for referenced assemblies, including the current directory.

当使用 asmpath 选项时,类型库导出程序不会在全局程序集缓存 (GAC) 中查找程序集。When you use the asmpath option, the Type Library Exporter will not look for an assembly in the global assembly cache (GAC).
/help/help 显示该工具的命令语法和选项。Displays command syntax and options for the tool.
/names: filename/names: filename 指定类型库中名称的大小写。Specifies the capitalization of names in a type library. filename 参数是一个文本文件。The filename argument is a text file. 文件中的每一行均指定类型库中一个名称的大小写。Each line in the file specifies the capitalization of one name in the type library.
/nologo/nologo 取消显示 Microsoft 启动版权标志。Suppresses the Microsoft startup banner display.
/oldnames/oldnames 强制 Tlbexp.exe 导出修饰类型名(如果存在类型名冲突)。Forces Tlbexp.exe to export decorated type names if there is a type name conflict. 请注意,这是 .NET Framework 2.0 版之前的版本中的默认行为。Note that this was the default behavior in versions prior to the .NET Framework version 2.0.
/out: file/out: file 指定要生成的类型库文件的名称。Specifies the name of the type library file to generate. 如果省略该选项,则 Tlbexp.exe 将生成一个与程序集的名称(实际的程序集名称,不一定与包含程序集的文件同名)相同且具有 .tlb 扩展名的类型库。If you omit this option, Tlbexp.exe generates a type library with the same name as the assembly (the actual assembly name, which might not necessarily be the same as the file containing the assembly) and a .tlb extension.
/silence: warningnumber/silence: warningnumber 禁止显示指定的警告。Suppresses the display of the specified warning. 此选项不能与 /silent 一起使用。This option cannot be used with /silent.
/Silent/silent 取消显示成功消息。Suppresses the display of success messages. 此选项不能与 /silence 一起使用。This option cannot be used with /silence.
/tlbreference: typelibraryname/tlbreference: typelibraryname 强制 Tlbexp.exe 在不参考注册表的情况下显式解析类型库引用。Forces Tlbexp.exe to explicitly resolve type library references without consulting the registry. 例如,如果程序集 B 引用程序集 A,则可使用此选项来提供显式类型库引用而不依赖于注册表中指定的类型库。For example, if assembly B references assembly A, you can use this option to provide an explicit type library reference, rather than relying on the type library specified in the registry. Tlbexp.exe 将执行版本检查以确保类型库版本与程序集版本相匹配;否则将生成错误。Tlbexp.exe performs a version check to ensure that the type library version matches the assembly version; otherwise, it generates an error.

请注意,在将 ComImportAttribute 特性应用于一个接口,然后该接口由另一类型实现的情况下,tlbreference 选项仍咨询注册表。Note that the tlbreference option still consults the registry in cases where the ComImportAttribute attribute is applied to an interface that is then implemented by another type.
/tlbrefpath: path/tlbrefpath: path 所引用的类型库的完全限定路径。Fully qualified path to a referenced type library.
/win32/win32 在 64 位计算机上编译时,此选项指定 Tlbexp.exe 生成一个 32 位类型库。When compiling on a 64-bit computer, this option specifies that Tlbexp.exe generates a 32-bit type library.
/win64/win64 在 32 位计算机上编译时,此选项指定 Tlbexp.exe 生成一个 64 位类型库。When compiling on a 32-bit computer, this option specifies that Tlbexp.exe generates a 64-bit type library.
/verbose/verbose 指定详细模式;显示需要为其生成类型库的任何引用程序集的列表。Specifies verbose mode; displays a list of any referenced assemblies for which a type library needs to be generated.
/?/? 显示该工具的命令语法和选项。Displays command syntax and options for the tool.

备注

Tlbexp.exe 的命令行选项不区分大小写,并可以按任何顺序提供。The command-line options for Tlbexp.exe are case-insensitive and can be supplied in any order. 只需指定足够的选项来唯一标识它。You only need to specify enough of the option to uniquely identify it. 例如,/n 等效于 /nologo,/o: outfile.tlb 等效于 /out: outfile.tlb。For example, /n is equivalent to /nologo, and /o: outfile.tlb is equivalent to /out: outfile.tlb.

备注Remarks

Tlbexp.exe 生成一个类型库,该类型库包含程序集中定义的类型的定义。Tlbexp.exe generates a type library that contains definitions of the types defined in the assembly. 应用程序(如 Visual Basic 6.0)可以使用生成的类型库来绑定到程序集中定义的 .NET 类型。Applications such as Visual Basic 6.0 can use the generated type library to bind to the .NET types defined in the assembly.

重要

不能使用 Tlbexp.exe 导出 Windows 元数据 (.winmd) 文件。You cannot use Tlbexp.exe to export Windows metadata (.winmd) files. 不支持导出 Windows 运行时程序集。Exporting Windows Runtime assemblies is not supported.

立即转换整个程序集。The entire assembly is converted at once. 不能使用 Tlbexp.exe 生成程序集中定义的类型子集的类型信息。You cannot use Tlbexp.exe to generate type information for a subset of the types defined in an assembly.

不能使用 Tlbexp.exe 从使用类型库导入程序 (Tlbimp.exe) 导入的程序集生成类型库。You cannot use Tlbexp.exe to produce a type library from an assembly that was imported using the Type Library Importer (Tlbimp.exe). 相反,应参考用 Tlbimp.exe 导入的原始类型库。Instead, you should refer to the original type library that was imported with Tlbimp.exe. 你可以从引用程序集(使用 Tlbimp.exe 导入)的程序集导出类型库。You can export a type library from an assembly that references assemblies that were imported using Tlbimp.exe. 请参见下面的示例部分。See the examples section below.

Tlbexp.exe 将生成的类型库置于当前工作目录中或为输出文件指定的目录中。Tlbexp.exe places generated type libraries in the current working directory or the directory specified for the output file. 一个程序集可能会导致生成多个类型库。A single assembly might cause several type libraries to be generated.

Tlbexp.exe 生成类型库,但不注册它。Tlbexp.exe generates a type library but does not register it. 这与程序集注册工具 (Regasm.exe) 不同,后者生成并注册类型库。This is in contrast to the Assembly Registration tool (Regasm.exe), which both generates and registers a type library. 若要使用 COM 生成和注册类型库,请使用 Regasm.exe。To generate and register a type library with COM, use Regasm.exe.

如果未指定 /win32/win64 选项,则 Tlbexp.exe 将生成一个 32 位或 64 位类型库,该类型库与执行编译所用的计算机的类型(32 位或 64 位计算机)相对应。If you do not specify either the /win32 or /win64 option, Tlbexp.exe generates a 32-bit or 64-bit type library that corresponds to the type of computer on which you are performing the compilation (32-bit or 64-bit computer). 为了交叉编译,可以在 32 位计算机上使用 /win64 选项来生成 64 位类型库,也可以在 64 位计算机上使用 /win32 选项来生成 32 位类型库。For cross-compilation purposes, you can use the /win64 option on a 32-bit computer to generate a 64-bit type library and you can use the /win32 option on a 64-bit computer to generate a 32-bit type library. 在 32 位类型库中,将 SYSKIND 值设置为 SYS_WIN32In 32-bit type libraries, the SYSKIND value is set to SYS_WIN32. 在 64 位类型库中,将 SYSKIND 值设置为 SYS_WIN64In 64-bit type libraries, the SYSKIND value is set to SYS_WIN64. 相应地转换所有数据类型转换(例如,指针大小的数据类型,如 IntPtrUIntPtr)。All data type transformations (for example, pointer-sized data types such as IntPtr and UIntPtr) are converted appropriately.

如果使用 MarshalAsAttribute 特性指定 SafeArraySubTypeVT_UNKOWNVT_DISPATCH 值,则 Tlbexp.exe 将忽略随后使用的任何 SafeArrayUserDefinedSubType 字段。If you use the MarshalAsAttribute attribute to specify a SafeArraySubType value of VT_UNKOWN or VT_DISPATCH, Tlbexp.exe ignores any subsequent use of the SafeArrayUserDefinedSubType field. 例如,对于以下签名:For example, given the following signatures:

[return:MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_UNKNOWN, SafeArrayUserDefinedSubType=typeof(ConsoleKeyInfo))] public Array StructUnkSafe(){return null;}  
[return:MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_DISPATCH, SafeArrayUserDefinedSubType=typeof(ConsoleKeyInfo))] public Array StructDispSafe(){return null;}  

生成以下类型库:the following type library is generated:

[id(0x60020004)]  
HRESULT StructUnkSafe([out, retval] SAFEARRAY(IUnknown*)* pRetVal);  
[id(0x60020005)]  
HRESULT StructDispSafe([out, retval] SAFEARRAY(IDispatch*)* pRetVal);  

请注意,Tlbexp.exe 忽略 SafeArrayUserDefinedSubType 字段。Note that Tlbexp.exe ignores the SafeArrayUserDefinedSubType field.

由于类型库无法容纳在程序集中找到的所有信息,因此在导出过程中,Tlbexp.exe 可能会放弃一些数据。Because type libraries cannot accommodate all the information found in assemblies, Tlbexp.exe might discard some data during the export process. 有关对转换过程的说明和发出到类型库中的每条信息的源的标识,请参见有关从程序集转换到类型库的摘要For an explanation of the transformation process and identification of the source of each piece of information emitted to a type library, see the Assembly to Type Library Conversion Summary.

请注意,类型库导出程序导出具有 TypedReference 类型的 VARIANT 参数的方法,尽管该 TypedReference 对象在非托管代码中没有意义。Note that the Type Library Exporter exports methods that have TypedReference parameters as VARIANT, even though the TypedReference object has no meaning in unmanaged code. 在导出具有 TypedReference 参数的方法时,类型库导出程序不会生成警告或错误,但使用结果类型库的非托管代码将无法正常运行。When you export methods that have TypedReference parameters, the Type Library Exporter will not generate a warning or error and unmanaged code that uses the resulting type library will not run properly.

Microsoft Windows 2000 和更高版本支持类型库导出程序。The Type Library Exporter is supported on Microsoft Windows 2000 and later.

示例Examples

下面的命令生成一个与 myTest.dll 中找到的程序集同名的类型库。The following command generates a type library with the same name as the assembly found in myTest.dll.

tlbexp myTest.dll  

下面的命令生成一个名为 clipper.tlb 的类型库。The following command generates a type library with the name clipper.tlb.

tlbexp myTest.dll /out:clipper.tlb  

下面的示例阐释使用 Tlbexp.exe 从一个程序集导出类型库,该类型库引用使用 Tlbimp.exe 导入的程序集。The following example illustrates using Tlbexp.exe to export a type library from an assembly that references assemblies that were imported using Tlbimp.exe.

首先使用 Tlbimp.exe 导入类型库 myLib.tlb,然后将其另存为 myLib.dllFirst use Tlbimp.exe to import the type library myLib.tlb and save it as myLib.dll.

tlbimp myLib.tlb /out:myLib.dll  

下面的命令使用 C# 编译器编译 Sample.dll,,后者引用前面的示例中创建的 myLib.dllThe following command uses the C# compiler to compile the Sample.dll, which references myLib.dll created in the previous example.

CSC Sample.cs /reference:myLib.dll /out:Sample.dll  

下面的命令为引用 Sample.dllmyLib.dll 生成类型库。The following command generates a type library for Sample.dll that references myLib.dll.

tlbexp Sample.dll  

请参阅See also