如何从 Visual Studio 2008 安装项目中使用 regasm 注册 .NET CCW
我有一个 .NET 服务应用程序的安装项目,它使用公开 COM 接口(COM 可调用包装器/CCW)的 .NET 组件。 为了让组件在目标机器上工作,它必须注册到
regasm.exe /tlb /codebase组件.dll
在这种情况下,用于生成类型库的 /tlb 开关是强制性的,否则我无法从该程序集中创建对象。
问题是,如何配置我的 Visual Studio 2008 安装项目以通过调用 regasm /tlb 来注册此程序集?
I have a setup project for a .NET Service Application which uses a .NET component which exposes a COM interface (COM callable wrapper / CCW).
To get the component working on a target machine, it has to be registered with
regasm.exe /tlb /codebase component.dll
The /tlb switch to generate the typelib is mandatory in this case, otherwise I can't create objects from that assembly.
The question is, how can I configure my Visual Studio 2008 Setup-Project to register this assembly with a call to regasm /tlb ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过使用 System.Runtime.InteropServices.RegistrationServices 来丢失对 regasm.exe 的手动调用:
这也会在卸载时取消注册库。
You can lose the manual call to regasm.exe by using System.Runtime.InteropServices.RegistrationServices instead:
This also unregisters the library upon uninstall.
重写安装、提交和卸载方法,添加:
// 获取高潮位置
字符串 regasmPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() + @"regasm.exe";
// 获取我们的DLL的位置
string componentPath = typeof(RegisterAssembly).Assembly.Location;
// 执行 regasm
System.Diagnostics.Process.Start(regasmPath, "/codebase /tlb \"" + componentPath + "\"");
在卸载操作中将 /codebase /tlb 替换为 /u。
可以在以下位置找到供您尝试的实际类的演练: http://leon.mvps.org/DotNet/RegasmInstaller.html
Override the Install, Commit and Uninstall methods adding:
// Get the location of regasm
string regasmPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() + @"regasm.exe";
// Get the location of our DLL
string componentPath = typeof(RegisterAssembly).Assembly.Location;
// Execute regasm
System.Diagnostics.Process.Start(regasmPath, "/codebase /tlb \"" + componentPath + "\"");
Swap /codebase /tlb for /u in the uninstall action.
A walkthrough with an actual class for you to try can be found at: http://leon.mvps.org/DotNet/RegasmInstaller.html
您的服务应该有一个 Installer 类。
注册到 OnAfterInstall 事件并调用 RegAsm:路径应从 Windows 目录计算并绑定到特定的 .Net 版本。
Your service should have an Installer class.
Register to the OnAfterInstall event and call RegAsm: the path should be computed from the Windows directory and tied to a specific .Net version.
我最初尝试从安装程序进程运行 regasm(在我看到这篇文章之前)。 尝试运行 regasm 并处理所有错误是有问题的 - 即使没有尝试处理 Windows 7 的提升权限。
使用 Runtime.InteropServices.RegistrationServices.RegisterAssembly 更加干净,并提供了更好的错误捕获。
I initially tried running regasm from the installer process (before I saw this posting). Trying to run regasm , and handling all the errors was problematic - even without trying to handle elevated privileges for Windows 7.
Using
Runtime.InteropServices.RegistrationServices.RegisterAssembly
was much cleaner and provided a much better error trapping.默认情况下,Visual Studio 安装程序仅进行 COM 类注册,但不进行类型库生成和注册(这是
regasm.exe 处的
)。 至少在 Visual Studio 2017 中,在要使用/tlb
开关所做的Tlbexp.exe
实用程序注册的 DLL 的后期构建步骤中生成类型库就足够了。如果安装程序项目在同一目录中发现一个扩展名为
.tlb
的文件,并且与您正在安装的库同名,它会自动将其包含到安装项目中,并在安装过程中执行注册步骤。 当然,也可以手动生成.tlb
文件并将其包含在安装项目中(并将其 Register 属性设置为vsdrfCOM
)。这是 关于 C# 和 COM 接口的精彩文章,上面的信息来自其名为“部署”的部分。
Visual Studio installer makes only COM class registration, but does not make the type library generation and registration (this is what
/tlb
switch atregasm.exe does
) by default. At least in Visual Studio 2017 it is enough to generate the type library in the post-build steps of DLL to be registered usingTlbexp.exe
utility.If the installer project discovers a file with extension
.tlb
in the same directory and with the same name as the library you are installing, it automatically includes it to the setup project and makes the registration steps during the installation. Of course it is also possible to generate the.tlb
file by hand and include it in the setup project (and set its Register property tovsdrfCOM
).Here is a great article about C# and COM interface and the information above come from its section called Deployment.