2008-10-27 84 views
5

我有一个使用.NET组件的.NET服务应用程序的安装项目,该组件提供了一个COM接口(COM可调用包装器/ CCW)。 为了得到在目标计算机上的工作部件,它具有与如何使用regasm从Visual Studio 2008安装项目注册.NET CCW

regasm.exe/TLB /代码库component.dll登记

的/ TLB切换到生成类型库是强制性的在这种情况下,否则我不能从该程序集创建对象。

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

回答

13

你可以失去的人工呼叫是通过使用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."); 
} 
} 

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

+1

此代码应该添加到什么位置?我有一个VB.net项目,它需要regasm并且有一些C#依赖项。这两个方法可以添加到实现哪个超类或接口的类中? – Amala 2012-04-25 16:13:47

1

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

4
  1. 在您的主项目(包含要注册的类的项目)中,右键单击项目文件并选择添加/新项目,然后选择安装程序类。取名类似clsRegisterDll.cs
  2. 在出现,点击“点击此处切换到代码视图”或右键单击Solution Explorer中的clsRegisterDll.cs文件,并选择查看代码
  3. 覆盖安装,提交设计师和卸载方法添加:

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

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

  4. 编译您的项目
  5. 在您的安装,请确保您已将DLL到应用程序文件夹,然后用鼠标右键单击安装项目并选择查看/自定义操作
  6. 鼠标右键单击安装,然后单击添加自定义操作
  7. 双击应用程序文件夹,然后在您的DLL
  8. 做同样的动作提交
  9. 构建和测试您的安装

与实际的类,您可以尝试的演练,可以发现:http://leon.mvps.org/DotNet/RegasmInstaller.html

+1

Wolfwyrd的链接已更改为http://leon.mvps.org/DotNet/RegasmInstaller.aspx – dashrb 2013-02-14 22:54:50

1

我最初尝试从安装过程中运行regasm(在我看到这篇文章之前)。尝试运行regasm并处理所有错误是有问题的 - 即使没有尝试处理Windows 7的提升权限。

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

相关问题