2011-12-24 47 views
3

我使用VS 2008创建了一个安装程序,其中包含一个桌面应用程序和一个BHO插件 ,该插件作为类库项目创建。 手动,我可以注册myPlugin.dll使用此命令[regasm/codebase“myPlugin.dll”注册表],但我不知道如何在安装项目中实现此目的。任何想法请?如何使用Visual Studio 2008部署.dll文件?

回答

2

最简单和最安全的方法是创建您自己的安装程序类,它接受要注册的DLL的名称并使用RegistrationServices来安装或卸载dll。

当您创建Installer时,它可以通过自定义操作的CustomActionData属性接受来自自定义操作的参数。每个参数都存储在Installer.Context.Parameters集合中。这意味着通过使用一些安装程序属性,您可以将完整的已安装路径传递给DLL,然后RegistrationServices可以使用该路径执行与Regasm相同的操作。

实现,这是一个多步骤的过程:

步骤1是创建安装程序类。因为这是可重用的代码,所以我建议创建一个单独的DLL项目来保存这个类。

using System; 
using System.Text; 
using System.ComponentModel; 
using System.Configuration.Install; 
using System.Collections; 
using System.IO; 
using System.Reflection; 
using System.Runtime.InteropServices; 

[RunInstaller(true)] 
public partial class AssemblyRegistrationInstaller : Installer 
{ 
    public AssemblyRegistrationInstaller() 
    { 
    } 

    public override void Install(IDictionary stateSaver) 
    { 
     base.Install(stateSaver); 

     RegisterAssembly(true); 
    } 

    public override void Rollback(IDictionary savedState) 
    { 
     base.Rollback(savedState); 

     RegisterAssembly(false); 
    } 

    public override void Uninstall(IDictionary savedState) 
    { 
     base.Rollback(savedState); 

     RegisterAssembly(false); 
    } 

    private void RegisterAssembly(bool fDoRegistration) 
    { 
     string sAssemblyFileName = base.Context.Parameters["AssemblyFileName"]; 

     if (string.IsNullOrEmpty(sAssemblyFileName)) 
     { 
      throw new InstallException("AssemblyFileName must be specified"); 
     } 

     if (!File.Exists(sAssemblyFileName)) 
     { 
      if (fDoRegistration) 
      { 
       throw new InstallException(string.Format("AssemblyFileName {0} is missing", sAssemblyFileName)); 
      } 
      else 
      { 
       // Just bail if we are uninstalling and the file doesn't exist 
       return; 
      } 
     } 

     var oServices = new RegistrationServices(); 
     var oAssembly = Assembly.LoadFrom(sAssemblyFileName); 

     if (fDoRegistration) 
     { 
      oServices.RegisterAssembly(oAssembly, AssemblyRegistrationFlags.SetCodeBase); 
     } 
     else 
     { 
      try 
      { 
       oServices.UnregisterAssembly(oAssembly); 
      } 
      catch 
      { 
       // Just eat the exception so that uninstall succeeds even if there is an error 
      } 
     } 
    } 
} 

第2步是将新DLL添加到安装项目中。由于我们需要配置DLL的设置,因此不要添加项目本身;直接从项目的发布目录添加DLL。

第3步是添加新的DLL作为自定义操作。为此,请右键单击安装项目,然后选择View,然后选择Custom Actions

右键单击Custom Actions菜单,并在弹出的对话框中选择新的安装程序DLL。这会自动将DLL添加到所有4个部分。右键单击已添加到Commit部分的部分并将其删除。

其他每个3个部分,执行以下操作:

的DLL入口右键单击并选择Properties Window

确保属性网格中的InstallerClass条目设置为true(应该是)。

将以下条目添加到CustomActionData属性通过DLL的名称被注册到​​自定义安装程序类:

/AssemblyFileName="[TARGETDIR]\myplugin.dll" 
相关问题