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

时间:2021-08-03 15:50:05

I have a setup project for a .NET Service Application which uses a .NET component wich exposes a COM interface (COM callable wrapper / CCW). To get the component working on a target machine, it has to be registered with

我有一个.NET服务应用程序的安装项目,它使用一个.NET组件,它公开了一个COM接口(COM可调用包装器/ CCW)。要使组件在目标计算机上运行,​​必须在其中注册

regasm.exe /tlb /codebase component.dll

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.

在这种情况下,生成类型库的/ tlb开关是必需的,否则我无法从该组件创建对象。

The question is, how can i configure my Visual Studio 2008 Setup-Project to register this assembly with a call to regasm /tlb ?

问题是,如何配置我的Visual Studio 2008安装项目以通过调用regasm / tlb注册此程序集?

4 个解决方案

#1


13  

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

您可以使用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.");
}
}

This also unregisters the library upon uninstall.

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

#2


4  

  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. 在主项目(包含要注册的类的项目)中,右键单击项目文件并选择Add / New Item并选择Installer Class。称之为clsRegisterDll.cs

  3. 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
  4. 在显示的设计器中,单击“单击此处切换到代码视图”或右键单击解决方案资源管理器中的clsRegisterDll.cs文件,然后选择“查看代码”

  5. 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 + "\"");

    //获取regasm字符串的位置regasmPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()+ @“regasm.exe”; //获取DLL字符串componentPath = typeof(RegisterAssembly)的位置.Assembly.Location; //执行regasm System.Diagnostics.Process.Start(regasmPath,“/ codebase / tlb \”“+ componentPath +”\“”);

    Swap /codebase /tlb for /u in the uninstall action.

    在卸载操作中为/ u交换/ codebase / tlb。

  6. Compile your project
  7. 编译您的项目

  8. 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
  9. 在安装程序中,确保已将dll添加到“应用程序文件夹”,然后右键单击安装程序项目并选择“查看/自定义操作”

  10. Right-click Install, and then click Add Custom Action
  11. 右键单击“安装”,然后单击“添加自定义操作”

  12. Double click on Application Folder, and then on your dll
  13. 双击应用程序文件夹,然后在您的DLL上

  14. Do the same for the Commit action
  15. 对Commit操作执行相同操作

  16. Build and test your installer
  17. 构建并测试安装程序

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

您可以在以下网址找到有关实际课程的演练:http://leon.mvps.org/DotNet/RegasmInstaller.html

#3


1  

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.

您的服务应该有一个安装程序类。注册到OnAfterInstall事件并调用RegAsm:路径应该从Windows目录计算并绑定到特定的.Net版本。

#4


1  

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.

我最初尝试从安装程序进程中运行regasm(在我看到此帖子之前)。试图运行regasm,并处理所有错误是有问题的 - 即使没有尝试处理Windows 7的提升权限。

Using Runtime.InteropServices.RegistrationServices.RegisterAssembly was much cleaner and provided a much better error trapping.

使用Runtime.InteropServices.RegistrationServices.RegisterAssembly更清晰,并提供了更好的错误捕获。

#1


13  

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

您可以使用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.");
}
}

This also unregisters the library upon uninstall.

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

#2


4  

  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. 在主项目(包含要注册的类的项目)中,右键单击项目文件并选择Add / New Item并选择Installer Class。称之为clsRegisterDll.cs

  3. 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
  4. 在显示的设计器中,单击“单击此处切换到代码视图”或右键单击解决方案资源管理器中的clsRegisterDll.cs文件,然后选择“查看代码”

  5. 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 + "\"");

    //获取regasm字符串的位置regasmPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()+ @“regasm.exe”; //获取DLL字符串componentPath = typeof(RegisterAssembly)的位置.Assembly.Location; //执行regasm System.Diagnostics.Process.Start(regasmPath,“/ codebase / tlb \”“+ componentPath +”\“”);

    Swap /codebase /tlb for /u in the uninstall action.

    在卸载操作中为/ u交换/ codebase / tlb。

  6. Compile your project
  7. 编译您的项目

  8. 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
  9. 在安装程序中,确保已将dll添加到“应用程序文件夹”,然后右键单击安装程序项目并选择“查看/自定义操作”

  10. Right-click Install, and then click Add Custom Action
  11. 右键单击“安装”,然后单击“添加自定义操作”

  12. Double click on Application Folder, and then on your dll
  13. 双击应用程序文件夹,然后在您的DLL上

  14. Do the same for the Commit action
  15. 对Commit操作执行相同操作

  16. Build and test your installer
  17. 构建并测试安装程序

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

您可以在以下网址找到有关实际课程的演练:http://leon.mvps.org/DotNet/RegasmInstaller.html

#3


1  

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.

您的服务应该有一个安装程序类。注册到OnAfterInstall事件并调用RegAsm:路径应该从Windows目录计算并绑定到特定的.Net版本。

#4


1  

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.

我最初尝试从安装程序进程中运行regasm(在我看到此帖子之前)。试图运行regasm,并处理所有错误是有问题的 - 即使没有尝试处理Windows 7的提升权限。

Using Runtime.InteropServices.RegistrationServices.RegisterAssembly was much cleaner and provided a much better error trapping.

使用Runtime.InteropServices.RegistrationServices.RegisterAssembly更清晰,并提供了更好的错误捕获。