为什么我的安装项目不会执行我的自定义注册过程

PIC*_*ain 6 com installation visual-studio-2008 visual-studio

我正在尝试为使用Visual Studio 2008在C#中编写的类库驱动程序编写安装项目/安装程序.驱动程序项目有一段看起来像这样的代码...

    [ComRegisterFunction]
    public static void RegisterASCOM(Type t)
    {
        Trace.WriteLine("Registration Started.");
        DoRegistration(true);  
    }
Run Code Online (Sandbox Code Playgroud)

在驱动程序项目属性 - >"装配信息"中,我设置了选中Make COM-Visible = true的框.

我在VS中的解决方案中添加了一个Setup Project,从驱动程序项目添加了输出dll,以便它安装在目标机器上并将dll的Register属性设置为"vsdraCOM".所以,我的理解是当安装程序运行它时,应该执行用[COMRegisterFunction]标记的dll方法.

使用SysInternals Debug View我可以通过观察窗口中显示的"Registration started"文本来监视上面的代码片段何时被点击.当我构建解决方案时,我可以看到文本显示,所以我知道驱动程序正在正确注册.问题是,当我运行安装程序时,我不认为它正在执行注册位.我看到Debug View中没有显示任何内容.如果我尝试通过另一个应用程序访问我的驱动程序,我收到一个错误,说"无法创建ActiveX对象".为什么在安装过程中不会发生注册?

驱动程序注册COM,但它不会调用我的自定义注册方法.

有没有人对我可能遗失的东西有所建议?还有其他方法可以调试吗?

(如果有人想看看,我可以提供更多代码!!)

Tim*_*ong 8

如果安装程序未使用默认机制注册COM对象,则可以尝试使用自定义操作强制它.此技术的另一个好处是,您可以单步执行自定义操作以计算正在进行的操作.

向您的项目添加一个继承自Installer的类,并使用此属性进行修饰

[RunInstaller(true)]  
Run Code Online (Sandbox Code Playgroud)

并覆盖以下一种或多种方法:

Install()
Uninstall()
Commit()
Rollback()
Run Code Online (Sandbox Code Playgroud)

这是一个使用COM Interop执行注册的完整示例.它散布着跟踪输出(与一些观点相反)将出现在Sysinternals DebugVw中.在Debug配置中内置时,它会弹出一个对话框,允许您附加调试器并单步执行自定义操作.

using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace TiGra
    {
    /// <summary>
    /// Custom install actions that must be carried out during product installation.
    /// </summary>
    [RunInstaller(true)]
    public class MyInstaller : Installer
        {

        /// <summary>
        /// Custom Install Action that performs custom registration steps as well as
        /// registering for COM Interop.
        /// </summary>
        /// <param name="stateSaver">Not used<see cref="Installer"/></param>
        public override void Install(System.Collections.IDictionary stateSaver)
            {
            Trace.WriteLine("Install custom action - Starting registration for COM Interop");
#if DEBUG
            MessageBox.Show("Attach debugger to this process now, if required", "Custom Action Debug", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
#endif
            base.Install(stateSaver);
            RegistrationServices regsrv = new RegistrationServices();
            if (!regsrv.RegisterAssembly(this.GetType().Assembly, AssemblyRegistrationFlags.SetCodeBase))
                {
                Trace.WriteLine("COM registration failed");
                throw new InstallException("Failed To Register for COM Interop");
                }
            Trace.WriteLine("Completed registration for COM Interop");
            }

        /// <summary>
        /// Custom Install Action that removes the COM Interop component registrations.
        /// </summary>
        /// <param name="savedState">Not used<see cref="Installer"/></param>
        public override void Uninstall(System.Collections.IDictionary savedState)
            {
            Trace.WriteLine("Uninstall custom action - unregistering from COM Interop");
            try
                {
                base.Uninstall(savedState);
                RegistrationServices regsrv = new RegistrationServices();
                if (!regsrv.UnregisterAssembly(this.GetType().Assembly))
                    {
                    Trace.WriteLine("COM Interop deregistration failed");
                    throw new InstallException("Failed To Unregister from COM Interop");
                    }
                }
            finally
                {
                Trace.WriteLine("Completed uninstall custom action");
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

还有一件事要做.除非安装程序配置为执行此操作,否则自定义操作将不会运行.这是如何做:

  • 在Visual Studio安装程序项目中,右键单击项目名称,然后选择"视图" - >"自定义操作".你会看到一个像这样的树视图:
    • 自定义操作
      • 安装
      • 卸载
      • 承诺
      • 回滚
  • 右键单击最顶层节点(自定义操作),然后选择添加自定义操作.
  • 导航到包含使用[RunInstaller(true)]属性修饰的类的文件或项目输出,突出显示它,然后单击"确定".
  • 然后,您的项目输出应显示在四个节点中的每个节点下.这意味着将在四个安装程序阶段的每个阶段调用您的自定义操作类.

如果需要更好地控制此操作,可以将自定义操作类添加到某些安装程序阶段,而不是其他阶段.例如,如果安装和卸载由不同的程序集处理,则迁移在"安装"和"提交"节点下添加一个程序集,在"卸载"和"回滚"节点下添加另一个程序集.

就这样,您的自定义操作现在将在安装过程中调用.调试自定义操作的快速提示.使用条件指令(如上例所示)在调试版本中显示消息框.然后将在安装过程中显示此消息框.这有效地暂停了设置过程,直到您在消息框上单击"确定",这将为您提供将调试器附加到msiexec.exe进程的"机会之窗"(双关语).将运行几个msiexec.exe进程,您需要选择一个说它是托管代码的进程.调试器将附加并且您的断点将"亮起",因此您可以拦截执行并单步执行自定义操作.