Regasm.exe(程序集注册工具)Regasm.exe (Assembly Registration Tool)

程序集注册工具读取程序集中的元数据,并将所需项添加到注册表中。注册表允许 COM 客户端以透明方式创建 .NET Framework 类。The Assembly Registration tool reads the metadata within an assembly and adds the necessary entries to the registry, which allows COM clients to create .NET Framework classes transparently. 在注册一个类之后,任何 COM 客户端都可以像使用 COM 类一样使用它。Once a class is registered, any COM client can use it as though the class were a COM class. 类仅在安装程序集时注册一次。The class is registered only once, when the assembly is installed. 只有实际注册程序集中的类实例之后才能从 COM 中创建它们。Instances of classes within the assembly cannot be created from COM until they are actually registered.

要运行该工具,请使用 Visual Studio 开发人员命令提示。To run the tool, use the Developer Command Prompt for Visual Studio. 有关详细信息,请参阅命令提示For more information, see Command Prompts.

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

语法Syntax

regasm assemblyFile [options]

参数Parameters

参数Parameter 描述Description
assemblyFile assemblyFile 要向 COM 注册的程序集。The assembly to be registered with COM.
选项Option 描述Description
/codebase /codebase 在注册表中创建一个 Codebase 项。Creates a Codebase entry in the registry. Codebase 项指定未安装到全局程序集缓存中的程序集的文件路径。The Codebase entry specifies the file path for an assembly that's not installed in the global assembly cache. 如果随后将安装要注册到全局程序集缓存中的程序集,请勿指定此选项。Don't specify this option if you will subsequently install the assembly that you're registering into the global assembly cache. 用 /codebase 选项指定的 assemblyFile 参数必须是具有强名称的程序集The assemblyFile argument that you specify with the /codebase option must be a strong-named assembly.
/registered /registered 指定此工具将仅引用已经注册的类型库。Specifies that this tool will only refer to type libraries that have already been registered.
/asmpath:directory /asmpath:directory 指定包含程序集引用的目录。Specifies a directory containing assembly references. 必须与 /regfile 选项一起使用 。Must be used with the /regfile option.
/nologo/nologo 取消显示 Microsoft 启动版权标志。Suppresses the Microsoft startup banner display.
/regfile [: regFile] /regfile [: regFile] 为程序集生成指定的 .reg 文件,其中包含所需的注册表项。Generates the specified .reg file for the assembly, which contains the needed registry entries. 指定此选项将不会更改注册表。Specifying this option does not change the registry. 此选项不能与 /u 或 /tlb 选项一起使用 。You cannot use this option with the /u or /tlb options.
/silent 或 /s /silent or /s 取消显示成功消息。Suppresses the display of success messages.
/tlb [: typeLibFile] /tlb [: typeLibFile] 从指定的程序集生成类型库,该类型库包含在程序集中定义的可访问类型的定义。Generates a type library from the specified assembly containing definitions of the accessible types defined within the assembly.
/unregister 或 /u /unregister or /u 注销在 assemblyFile 中找到的可创建类 。Unregisters the creatable classes found in assemblyFile. 省略此选项将使 Regasm.exe 注册程序集中的可创建类。Omitting this option causes Regasm.exe to register the creatable classes in the assembly.
/verbose/verbose 指定详细模式;当与 /tlb 选项一起指定时,将显示所有需要为其生成类型库的引用程序集的列表 。Specifies verbose mode; displays a list of any referenced assemblies for which a type library needs to be generated, when specified with the /tlb option.
/?/? 或 /help or /help 显示该工具的命令语法和选项。Displays command syntax and options for the tool.

备注

Regasm.exe 命令行选不区分大小写。The Regasm.exe command-line options are case insensitive. 只需提供足以唯一地进行标识的选项部分。You only need to provide enough of the option to uniquely identify it. 例如,/n 等效于 /nologo,而 /t: outfile.tlb 等效于 /tlb: outfile.tlb 。For example, /n is equivalent to /nologo and /t: outfile.tlb is equivalent to /tlb: outfile.tlb.

备注Remarks

可以使用 /regfile 选项生成包含注册表项的 .reg 文件,而不是直接对注册表进行更改 。You can use the /regfile option to generate a .reg file that contains the registry entries instead of making the changes directly to the registry. 通过注册表编辑器工具 (Regedit.exe) 导入 .reg 文件,可以在计算机上更新注册表。You can update the registry on a computer by importing the .reg file with the Registry Editor tool (Regedit.exe). .reg 文件不包含任何可由用户定义的注册函数完成的注册表更新。The .reg file does not contain any registry updates that can be made by user-defined register functions. /regfile 选项只为托管类发出注册表项 。The /regfile option only emits registry entries for managed classes. 此选项不为 TypeLibIDInterfaceID 发出注册表项。This option does not emit entries for TypeLibIDs or InterfaceIDs.

指定 /tlb 选项时,Regasm.exe 将生成并注册一个类型库,对在程序集中找到的类型进行描述 。When you specify the /tlb option, Regasm.exe generates and registers a type library describing the types found in the assembly. Regasm.exe 将生成的类型库放置在当前的工作目录中或为输出文件指定的目录中。Regasm.exe places the generated type libraries in the current working directory or the directory specified for the output file. 为引用其他程序集的程序集生成类型库可能导致同时生成几个类型库。Generating a type library for an assembly that references other assemblies may cause several type libraries to be generated at once. 你可使用类型库向开发工具(如 Visual Studio)提供类型信息。You can use the type library to provide type information to development tools like Visual Studio. 如果所注册的程序集是由类型库导入程序 (Tlbimp.exe) 生成的,请勿使用 /tlb 选项 。Don't use the /tlb option if the assembly you are registering was produced by the Type Library Importer (Tlbimp.exe). 如果程序集是从类型库导入的,则不能从它导出类型库。You cannot export a type library from an assembly that was imported from a type library. 除了类型库导出程序 (Tlbexp.exe) 不会注册其生成的类型库外,使用 /tlb 选项与使用 Tlbexp.exe 和 Regasm.exe 的效果相同 。Using the /tlb option has the same effect as using the Type Library Exporter (Tlbexp.exe) and Regasm.exe, with the exception that Tlbexp.exe does not register the type library it produces. 如果使用 /tlb 选项注册某个类型库,则可将 /tlb 选项和 /unregister 选项一起使用以注销类型库。If you use the /tlb option to register a type library, you can use the /tlb option with the /unregister option to unregister the type library. 将两个选项一起使用将注销类型库和接口项,这样可较大程度地清理注册表。Using the two options together will unregister the type library and interface entries, which can clean the registry considerably.

当你注册一个程序集供 COM 使用时,Regasm.exe 会在本地计算机上的注册表中添加一些项。When you register an assembly for use by COM, Regasm.exe adds entries to the registry on the local computer. 具体而言,它将创建一些与版本相关的注册表键,从而允许在一台计算机上并行运行同一程序集的多个版本。More specifically, it creates version-dependent registry keys that allow multiple versions of the same assembly to run side by side on a computer. 在第一次注册某个程序集时,将会为该程序集创建一个顶级键,并为特定版本创建一个唯一的子键。The first time an assembly is registered, one top-level key is created for the assembly, and a unique subkey is created for the specific version. 每次注册该程序集的新版本时,Regasm.exe 都将为新版本创建一个子键。Each time you register a new version of the assembly, Regasm.exe creates a subkey for the new version.

例如,假设你注册了一个版本为 1.0.0.0 的托管组件 myComp.dll 供 COM 使用。For example, consider a scenario where you register the managed component, myComp.dll, version 1.0.0.0 for use by COM. 之后,你注册了版本为 2.0.0.0 的 myComp.dll。Later, you register myComp.dll, version 2.0.0.0. 你确定计算机上的所有 COM 客户端应用程序都要使用 2.0.0.0 版本的 myComp.dll,并且决定注销 myComponent.dll 版本 1.0.0.0。You determine that all COM client applications on the computer are using myComp.dll version 2.0.0.0 and you decide to unregister myComponent.dll version 1.0.0.0. 此注册表方案允许你注销 myComp.dll 版本 1.0.0.0,这是因为注销操作只移除 1.0.0.0 版本子键。This registry scheme allows you to unregister myComp.dll version 1.0.0.0 because only the version 1.0.0.0 subkey is removed.

使用 Regasm.exe 注册一个程序集之后,可以将该程序集安装在全局程序集缓存中,以便可以从任何 COM 客户端激活它。After registering an assembly using Regasm.exe, you can install it in the global assembly cache so that it can be activated from any COM client. 如果该程序集将仅由单个应用程序激活,则可以将其放置在该应用程序的目录中。If the assembly is only going to be activated by a single application, you can place it in that application's directory.

示例Examples

下面的命令将注册 myTest.dll 中包含的所有公共类。The following command registers all public classes contained in myTest.dll.

regasm myTest.dll

下面的命令生成包含所有必要的注册表项的 myTest.reg 文件。The following command generates the file myTest.reg, which contains all the necessary registry entries. 此命令不更新注册表。This command does not update the registry.

regasm myTest.dll /regfile:myTest.reg

下面的命令注册 myTest.dll 中包含的所有公共类,并生成和注册类型库 myTest.tlb,该类型库包含 myTest.dll 中定义的所有公共类型的定义。The following command registers all public classes contained in myTest.dll, and generates and registers the type library myTest.tlb, which contains definitions of all the public types defined in myTest.dll.

regasm myTest.dll /tlb:myTest.tlb

请参阅See also