如何从 Visual Studio 2008 安装项目中使用 regasm 注册 .NET CCW

发布于 2024-07-07 18:13:34 字数 283 浏览 7 评论 0原文

我有一个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

以往的大感动 2024-07-14 18:13:34

您可以通过使用 System.Runtime.InteropServices.RegistrationServices 来丢失对 regasm.exe 的手动调用:

[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);

RegistrationServices regsrv = new RegistrationServices();
if (!regsrv.RegisterAssembly(GetType().Assembly, AssemblyRegistrationFlags.SetCodeBase))
{
    throw new InstallException("Failed to register for COM Interop.");
}

}

[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Uninstall(IDictionary savedState)
{
base.Uninstall(savedState);

RegistrationServices regsrv = new RegistrationServices();
if (!regsrv.UnregisterAssembly(GetType().Assembly))
{
    throw new InstallException("Failed to unregister for COM Interop.");
}
}

这也会在卸载时取消注册库。

You can lose the manual call to regasm.exe by using System.Runtime.InteropServices.RegistrationServices instead:

[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);

RegistrationServices regsrv = new RegistrationServices();
if (!regsrv.RegisterAssembly(GetType().Assembly, AssemblyRegistrationFlags.SetCodeBase))
{
    throw new InstallException("Failed to register for COM Interop.");
}

}

[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Uninstall(IDictionary savedState)
{
base.Uninstall(savedState);

RegistrationServices regsrv = new RegistrationServices();
if (!regsrv.UnregisterAssembly(GetType().Assembly))
{
    throw new InstallException("Failed to unregister for COM Interop.");
}
}

This also unregisters the library upon uninstall.

万劫不复 2024-07-14 18:13:34
  1. 在您的主项目(包含要注册的类的项目)中,右键单击项目文件并选择“添加/新项目”,然后选择“安装程序类”。 将其命名为 clsRegisterDll.cs
  2. 在出现的设计器中,单击“单击此处切换到代码视图”或右键单击解决方案资源管理器中的 clsRegisterDll.cs 文件,然后选择查看代码
  3. 重写安装、提交和卸载方法,添加:

    // 获取高潮位置
    字符串 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。

  4. 编译您的项目
  5. 在安装程序中,确保已将 dll 添加到应用程序文件夹,然后右键单击安装程序项目并选择“查看/自定义操作”
  6. 右键单击​​“安装”,然后单击“添加自定义操作”
  7. 双击“应用程序文件夹”,然后在您的 dll 上对
  8. “提交”操作执行相同的操作
  9. 构建并测试您的安装程序

可以在以下位置找到供您尝试的实际类的演练: http://leon.mvps.org/DotNet/RegasmInstaller.html

  1. In your main project (the one containing the class you want to register), right click the project file and select Add / New Item and select Installer Class. Call it something like clsRegisterDll.cs
  2. In the designer that appears, click 'Click here to switch to code view' or right click the clsRegisterDll.cs file in solution explorer and select View Code
  3. 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.

  4. Compile your project
  5. In your installer, make sure you have added your dll to the Application Folder, and then right-click the installer project and select View / Custom Actions
  6. Right-click Install, and then click Add Custom Action
  7. Double click on Application Folder, and then on your dll
  8. Do the same for the Commit action
  9. Build and test your installer

A walkthrough with an actual class for you to try can be found at: http://leon.mvps.org/DotNet/RegasmInstaller.html

迷鸟归林 2024-07-14 18:13:34

您的服务应该有一个 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.

半﹌身腐败 2024-07-14 18:13:34

我最初尝试从安装程序进程运行 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.

小帐篷 2024-07-14 18:13:34

默认情况下,Visual Studio 安装程序仅进行 COM 类注册,但不进行类型库生成和注册(这是 regasm.exe 处的 /tlb 开关所做的)。 至少在 Visual Studio 2017 中,在要使用 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 at regasm.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 using Tlbexp.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 to vsdrfCOM).

Here is a great article about C# and COM interface and the information above come from its section called Deployment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文