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 為指定的類型程式庫產生主要 Interop 組件。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. 藉由指定主要 Interop 組件,就可以區別發行者的組件與使用 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. 請注意,您必須以強式名稱簽署主要 Interop 組件。Note that you must sign a primary interop assembly with a strong name. 如需詳細資訊,請參閱主要 Interop 組件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 選項指定的組件中,或是已登錄的主要 Interop 組件 (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 作為 Managed 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.

transformname 參數指定 dispret,以便將分配介面 (Dispinterface) 上方法的 [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] 參數,轉換為 Managed 程式庫中的傳回值。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 Managed 程式庫中的 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 產生 Managed 程式庫,而未指定 /transform:dispret,則工具會在 myTestLib.dll Managed 程式庫中為 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