Tlbimp.exe(类型库导入程序)Tlbimp.exe (Type Library Importer)

类型库导入程序将 COM 类型库中的类型定义转换为公共语言运行时程序集中的等效定义。The Type Library Importer converts the type definitions found within a COM type library into equivalent definitions in a common language runtime assembly. Tlbimp.exe 的输出是一个二进制文件(程序集),其中包含在原始类型库中定义的类型的运行时元数据。The output of Tlbimp.exe is a binary file (an assembly) that contains runtime metadata for the types defined within the original type library. 可以使用 Ildasm.exe 这样的工具检查此文件。You can examine this file with tools such as Ildasm.exe.

此工具会自动随 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

tlbimp tlbFile [options]  

参数Parameters

参数Argument 说明Description
tlbFiletlbFile 任何包含 COM 类型库的文件的名称。The name of any file that contains a COM type library.
选项Option 说明Description
/asmversion: versionnumber/asmversion: versionnumber 指定要生成的程序集的版本号。Specifies the version number of the assembly to produce. major.minor.build.revision 格式指定 versionnumberSpecify versionnumber in the format major.minor.build.revision.
/company: companyinformation/company: companyinformation 将公司信息添加到输出程序集。Adds company information to the output assembly.
/copyright: copyrightinformation/copyright: copyrightinformation 将版权信息添加到输出程序集。Adds copyright information to the output assembly. 此信息可在程序集的“文件属性” 对话框中查看。This information can be viewed in the File Properties dialog box for the assembly.
/delaysign/delaysign 指定 Tlbimp.exe 使用延迟签名对通过结果程序集进行强名称签名。Specifies to Tlbimp.exe to sign the resulting assembly with a strong name using delayed signing. 此选项必须与 /keycontainer:/keyfile:/publickey: 选项一起指定。You must specify this option with either the /keycontainer:, /keyfile:, or /publickey: option. 有关延迟签名过程的更多信息,请参见延迟为程序集签名For more information on the delayed signing process, see Delay Signing an Assembly.
/help/help 显示该工具的命令语法和选项。Displays command syntax and options for the tool.
/keycontainer: containername/keycontainer: containername 使用在 containername 指定的密钥容器中找到的公钥/私钥对,对结果程序集进行强名称签名。Signs the resulting assembly with a strong name using the public/private key pair found in the key container specified by containername.
/keyfile: filename/keyfile: filename 使用在 filename中找到的发行者的正式公钥/私钥对,对结果程序集进行强名称签名。Signs the resulting assembly with a strong name using the publisher's official public/private key pair found in filename.
/machine: machinetype/machine: machinetype 创建以指定计算机类型(微处理器)为目标的程序集。Creates an assembly that targets the specified machine type (microprocessor). 支持的计算机类型:x86、x64、Itanium 和 Agnostic。Supported machine types: x86, x64, Itanium, and Agnostic.
/namespace: namespace/namespace: namespace 指定在其中生成程序集的命名空间。Specifies the namespace in which to produce the assembly.
/noclassmembers/noclassmembers 阻止 Tlbimp.exe 向类添加成员。Prevents Tlbimp.exe from adding members to classes. 这样可避免潜在的 TypeLoadExceptionThis avoids a potential TypeLoadException.
/nologo/nologo 取消显示 Microsoft 启动版权标志。Suppresses the Microsoft startup banner display.
/out: filename/out: filename 指定输出文件、程序集以及要写入元数据定义的命名空间的名称。Specifies the name of the output file, assembly, and namespace in which to write the metadata definitions. 如果类型库指定的接口定义语言 (IDL) 自定义特性显式控制程序集的命名空间,则 /out 选项对程序集的命名空间没有任何影响。The /out option has no effect on the assembly's namespace if the type library specifies the Interface Definition Language (IDL) custom attribute that explicitly controls the assembly's namespace. 如果你没有指定此选项,则 Tlbimp.exe 会将元数据写入到与在输入文件内定义的实际类型库同名的文件中,并且为其分配 .dll 扩展名。If you do not specify this option, Tlbimp.exe writes the metadata to a file with the same name as the actual type library defined within the input file and assigns it a .dll extension. 如果输出文件的名称与输入文件的名称相同,则该工具将生成一个错误以防止覆盖类型库。If the output file is the same name as the input file, the tool generates an error to prevent overwriting the type library.
/primary/primary 生成指定类型库的主互操作程序集。Produces a primary interop assembly for the specified type library. 将在程序集中添加相关信息以指示类型库的发行者已生成程序集。Information is added to the assembly indicating that the publisher of the type library produced the assembly. 通过指定主互操作程序集,可以将发布者的程序集与使用 Tlbimp.exe 从类型库创建的任何其他程序集区分开来。By specifying a primary interop assembly, you differentiate a publisher's assembly from any other assemblies that are created from the type library using Tlbimp.exe. 如果你是用 Tlbimp.exe 导入的类型库的发布者,则应只使用 /primary 选项。You should only use the /primary option if you are the publisher of the type library that you are importing with Tlbimp.exe. 请注意,你必须使用强名称对主互操作程序集进行签名。Note that you must sign a primary interop assembly with a strong name. 有关详细信息,请参阅主互操作程序集For more information, see Primary Interop Assemblies.
/product: productinformation/product: productinformation 将产品信息添加到输出程序集。Adds product information to the output assembly. 此信息可在程序集的“文件属性” 对话框中查看。This information can be viewed in the File Properties dialog box for the assembly.
/productversion: productversioninformation/productversion: productversioninformation 将产品版本信息添加到输出程序集。Adds product version information to the output assembly. 没有格式限制。There are no format restrictions. 此信息可在程序集的“文件属性” 对话框中查看。This information can be viewed in the File Properties dialog box for the assembly.
/publickey: filename/publickey: filename 指定包含用来对结果程序集签名的公钥的文件。Specifies the file containing the public key to use to sign the resulting assembly. 如果指定 /keyfile:/keycontainer: 选项而非 /publickey: ,则 Tlbimp.exe 将根据随 /keyfile:/keycontainer: 一起提供的公钥/私钥对来生成公钥。If you specify the /keyfile: or /keycontainer: option instead of /publickey:, Tlbimp.exe generates the public key from the public/private key pair supplied with /keyfile: or /keycontainer:. /publickey: 选项支持测试键和延迟签名方案。The /publickey: option supports test key and delay signing scenarios. 文件的格式为 Sn.exe 生成的格式。The file is in the format generated by Sn.exe. 有关详细信息,请参阅强名称工具 (Sn.exe) 中的 Sn.exe 的 -p 选项。For more information, see the -p option of Sn.exe in Strong Name Tool (Sn.exe).
/reference: filename/reference: filename 指定用来解析对在当前类型库外定义的类型的引用的程序集文件。Specifies the assembly file to use to resolve references to types defined outside the current type library. 如果没有指定 /reference 选项,则 Tlbimp.exe 将自动以递归方式导入任何由导入的类型库引用的外部类型库。If you do not specify the /reference option, Tlbimp.exe automatically recursively imports any external type library that the type library being imported references. 如果指定了 /reference 选项,则在导入其他类型库之前,该工具将尝试解析被引用程序集中的外部类型。If you specify the /reference option, the tool attempts to resolve external types in the referenced assemblies before it imports other type libraries.
/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.
/strictref/strictref 如果该工具不能解析当前程序集、 /reference 选项指定的程序集或已注册的主互操作程序集 (PIA) 内的所有引用,则不要导入类型库。Does not import a type library if the tool cannot resolve all references within the current assembly, the assemblies specified with the /reference option, or registered primary interop assemblies (PIAs).
/strictref:nopia/strictref:nopia /strictref 相同,但忽略 PIA。Same as /strictref, but ignores PIAs.
/sysarray/sysarray 指定该工具导入 COM 样式 SafeArray 作为托管 Array 类型。Specifies to the tool to import a COM style SafeArray as a managed Array type.
/tlbreference: filename/tlbreference: filename 指定用来在不参考注册表的情况下解析类型库引用的类型库文件。Specifies the type library file to use to resolve type library references without consulting the registry.

请注意,此选项将不加载某些较早的类型库格式。Note that this option will not load some older type library formats. 但是,你仍可以通过注册表或当前目录隐式加载较早的类型库格式。However, you can still load older type library formats implicitly through the registry or current directory.
/trademark: trademarkinformation/trademark: trademarkinformation 将商标信息添加到输出程序集。Adds trademark information to the output assembly. 此信息可在程序集的“文件属性” 对话框中查看。This information can be viewed in the File Properties dialog box for the assembly.
/transform: transformname/transform: transformname transformname 参数指定的方式转换元数据。Transforms metadata as specified by the transformname parameter.

指定 dispret 作为 transformname 参数,可以将仅支持调度的接口的方法的 [out, retval] 参数转换为返回值。Specify dispret for the transformname parameter to transform [out, retval] parameters of methods on dispatch-only interfaces (dispinterfaces) into return values.

有关此选项的更多信息,请参见本主题后面的示例。For more information about this option, see the examples later in this topic.
/unsafe/unsafe 在不进行 .NET Framework 安全性检查的情况下生成接口。Produces interfaces without .NET Framework security checks. 调用以此方式公开的方法可能会导致安全风险。Calling a method that is exposed in this way might pose a security risk. 如果你不了解公开此类代码的风险,则不应使用此选项。You should not use this option unless you are aware of the risks of exposing such code.
/verbose/verbose 指定详细模式;显示有关导入的类型库的附加信息。Specifies verbose mode; displays additional information about the imported type library.
/VariantBoolFieldToBool/VariantBoolFieldToBool 将结构中的 VARIANT_BOOL 字段转换为 BooleanConverts VARIANT_BOOL fields in structures to Boolean.
/?/? 显示该工具的命令语法和选项。Displays command syntax and options for the tool.

备注

Tlbimp.exe 的命令行选项不区分大小写,并可以按任意顺序提供。The command-line options for Tlbimp.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,而 /ou: outfile.dll 等效于 /out: outfile.dllTherefore, /n is equivalent to /nologo and /ou: outfile.dll is equivalent to /out: outfile.dll.

备注Remarks

Tlbimp.exe 同时执行整个类型库的转换。Tlbimp.exe performs conversions on an entire type library at one time. 不能使用该工具为在单个类型库中定义的类型的子集生成类型信息。You cannot use the tool to generate type information for a subset of the types defined within a single type library.

能够将强名称分配给程序集通常是有用或必需的。It is often useful or necessary to be able to assign strong names to assemblies. 因此,Tlbimp.exe 包括相应的选项,用以提供必需的信息来生成具有强名称的程序集。Therefore, Tlbimp.exe includes options for supplying the information necessary to generate strongly named assemblies. /keyfile:/keycontainer: 选项均使用强名称对程序集进行签名。Both the /keyfile: and /keycontainer: options sign assemblies with strong names. 因此,一次只提供这些选项中的一个是合理的。Therefore, it is logical to supply only one of these options at a time.

可通过多次使用 /reference 选项来指定多个引用程序集。You can specify multiple reference assemblies by using the /reference option multiple times.

由于 Tlbimp.exe 生成程序集的方式,因此无法将程序集重定向到其他 mscorlib 版本。Due to the way in which Tlbimp.exe generates assemblies, it is not possible to retarget an assembly to a different mscorlib version. 例如,如果希望生成面向 .NET Framework 2.0 的程序集,则必须使用 .NET Framework 2.0/3.0/3.5 SDK 附带的 Tlbimp.exe。For example, if you desire to generate an assembly that targets .NET Framework 2.0, the Tlbimp.exe shipped with the .NET Framework 2.0/3.0/3.5 SDK must be used. 要面向 .NET Framework 4.x,应使用 .NET Framework 4.x SDK 附带的 Tlbimp.exe。In order to target .NET Framework 4.x, the Tlbimp.exe shipped with a .NET Framework 4.x SDK should be used.

在从包含多个类型库的模块中导入类型库时,可以选择将资源 ID 追加到一个类型库文件中。A resource ID can optionally be appended to a type library file when importing a type library from a module containing multiple type libraries. 仅当类型库文件位于当前目录中时或者当你指定了完整的路径时,Tlbimp.exe 才能找到该文件。Tlbimp.exe is able to locate this file only if it is in the current directory or if you specify the full path. 请参见本主题后面的示例。See the example later in this topic.

示例Examples

下面的命令生成一个与在 myTest.tlb 中找到的类型库具有相同的名称的程序集(具有 .dll 扩展名)。The following command generates an assembly with the same name as the type library found in myTest.tlb and with the .dll extension.

tlbimp myTest.tlb   

下面的命令生成一个名为 myTest.dll 的程序集。The following command generates an assembly with the name myTest.dll.

tlbimp  myTest.tlb  /out:myTest.dll  

下面的命令生成一个与 MyModule.dll\1 指定的类型库具有相同的名称的程序集(具有 .dll 扩展名)。The following command generates an assembly with the same name as the type library specified by MyModule.dll\1 and with the .dll extension. MyModule.dll\1 必须位于当前目录中。MyModule.dll\1 must be located in the current directory.

tlbimp MyModule.dll\1  

下面的命令为类型库 myTestLib.dll 生成一个名为 TestLib.dll 的程序集。The following command generates an assembly with the name myTestLib.dll for the type library TestLib.dll. /transform:dispret 选项将类型库中的调度接口方法的任何 [out, retval] 参数转换为托管库中的返回值。The /transform:dispret option transforms any [out, retval] parameters of methods on dispinterfaces in the type library into return values in the managed library.

tlbimp TestLib.dll /transform:dispret /out:myTestLib.dll  

上例中的类型库 TestLib.dll 包含一个名为 SomeMethod 的调度接口方法,它返回 void 且具有一个 [out, retval] 参数。The type library TestLib.dll, in the preceding example, includes a dispinterface method named SomeMethod that returns void and has an [out, retval] parameter. 下面的代码是 SomeMethodTestLib.dll 的输入类型库方法签名。The following code is the input type library method signature for SomeMethod in TestLib.dll.

void SomeMethod([out, retval] VARIANT_BOOL*);  

指定 /transform:dispret 选项会使 Tlbimp.exe 将 SomeMethod[out, retval] 参数转换为 bool 返回值。Specifying the /transform:dispret option causes Tlbimp.exe to transform the [out, retval] parameter of SomeMethod into a bool return value. 下面是指定 /transform:dispret 选项时,Tlbimp.exe 为托管库 myTestLib.dll 中的 SomeMethod 生成的方法签名。The following is the method signature that Tlbimp.exe produces for SomeMethod in the managed library myTestLib.dll when the /transform:dispret option is specified.

bool SomeMethod();  

如果使用 Tlbimp.exe 生成 TestLib.dll 的托管库时没有指定 /transform:dispret,则该工具将为托管库 myTestLib.dll 中的 SomeMethod 生成以下方法签名。If you use Tlbimp.exe to produce a managed library for TestLib.dll without specifying the /transform:dispret, the tool produces the following method signature for SomeMethod in the managed library myTestLib.dll.

void SomeMethod(out bool x);  

请参阅See also