为什么我的安装项目不执行我的自定义注册过程 [英] Why won't my Setup Project Perform my Custom Registration Process

查看:141
本文介绍了为什么我的安装项目不执行我的自定义注册过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  [ComRegisterFunction] 
public static void RegisterASCOM(Type t)
{
Trace.WriteLine );
DoRegistration(true);
}

在驱动程序项目属性 - >装配信息框中说Make COM-Visible = true。



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



使用SysInternals调试视图我可以监视什么时候上面的代码片段是通过观察注册开始文本显示在窗口中。当我构建的解决方案,我可以看到文本显示,所以我知道驱动程序正确注册。问题是,当我运行安装程序,我不认为它是做注册位。我看不到任何显示在调试视图。如果我尝试通过另一个应用程序访问我的驱动程序,我得到一个错误说无法创建ActiveX对象。为什么在安装过程中不会进行注册?



驱动程序注册了COM,但不会调用我的自定义注册方法。 / p>

有没有人和建议我可能会丢失什么?有没有其他方法我可以调试这个?



(如果任何人想要看看,我可以提供更多的代码!!)

解决方案

如果安装程序没有使用默认机制注册COM对象,您可以尝试使用自定义操作强制它。这种技术的一个额外的好处是,您可以单步执行自定义操作来了解发生了什么。



向继承自Installer的项目添加一个类,装饰有此属性

  [RunInstaller(true)] 

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

 安装()
Uninstall()
Commit()
回滚()

这是一个完整的例子,执行注册COM互操作。它撒有跟踪输出(与一些意见相反)将显示在Sysinternals DebugVw中。当构建在Debug配置中时,它会建立一个对话框,允许您附加调试器和单步执行自定义操作。

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

命名空间TiGra
{
///< summary>
///必须在产品安装期间执行的自定义安装操作。
///< / summary>
[RunInstaller(true)]
public class MyInstaller:Installer
{

///< summary>
///自定义安装执行自定义注册步骤的操作以及
///注册COM Interop。
///< / summary>
///< param name =stateSaver>未使用< see cref =Installer/>< / param&
public override void Install(System.Collections.IDictionary stateSaver)
{
Trace.WriteLine(安装自定义操作 - 开始注册COM Interop);
#if DEBUG
MessageBox.Show(现在,将调试器附加到此进程,如果需要,自定义操作调试,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);
抛出新的InstallException(无法注册COM Interop);
}
Trace.WriteLine(COM Interop的完成注册);
}

///< summary>
///自定义安装用于删除COM Interop组件注册的操作。
///< / summary>
///< param name =savedState>未使用< see cref =Installer/>< / param&
public override void Uninstall(System.Collections.IDictionary savedState)
{
Trace.WriteLine(卸载自定义操作 - 从COM Interop注销);
try
{
base.Uninstall(savedState);
RegistrationServices regsrv = new RegistrationServices();
if(!regsrv.UnregisterAssembly(this.GetType()。Assembly))
{
Trace.WriteLine(COM Interop deregistration failed);
抛出新的InstallException(无法从COM Interop注销);
}
}
finally
{
Trace.WriteLine(Completed uninstall custom action);
}
}
}
}

最后的事情要做。除非安装程序配置为这样做,否则自定义操作将不会运行。方法如下:




  • 在Visual Studio安装程序项目中,右键单击项目名称,然后选择视图 - >自定义操作。您将看到一个树视图,如下所示:


    • 自定义操作


      • 安装

      • 卸载

      • 提交

      • 回滚

      li>

  • 右键单击顶部节点(自定义操作),然后选择添加自定义操作

  • 导航到包含用[RunInstaller(true)]属性修饰的类的文件或项目输出,突出显示它并单击确定。

  • 出现在四个节点中的每一个下面。这意味着您将在四个安装程序阶段的每个阶段调用自定义操作类。



如果您需要更精细的控制,可以将自定义动作类添加到某些安装程序阶段,而不是其他阶段。例如,如果安装和卸载由不同的程序集处理,则migth在安装和提交节点下添加一个程序集,在卸载和回滚节点下添加另一个程序集。



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


I am trying to write an Setup Project/Installer for a class library driver that I wrote in C# using Visual Studio 2008. The driver project has a section of code that looks like this...

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

In the driver project Properties -> "Assembly Information" I have set checked the box that says Make COM-Visible = true.

I added a Setup Project to the solution in VS, added the output dll from the driver project so that it installs on the target machine and set the Register property of the dll to "vsdraCOM". So, my understanding is that when the installer runs it SHOULD execute the methods of the dll that are marked with [COMRegisterFunction].

Using SysInternals Debug View I can monitor when the above code snippet is hit by watching for the "Registration started" text to show up in the window. When I build the solution, I can see the text show up so I know the driver is registering properly. The problem is that when I run the installer, I don't think it is doing the registration bit. I see nothing show up in Debug View. And if i try to access my driver via another application I get an error saying it "Cannot create ActiveX object". Why does the registration not occur during the install process?

The driver does register for COM but it does NOT call my custom registration method.

Does anyone have and suggestions of what I could be missing? Is there another way I can debug this?

(I can provide more code if anyone want's to take a look!!)

解决方案

If the installer isn't registering the COM objects using the default mechanism, you can try forcing it using a custom action. An extra benefit of this technique is that you can single-step through the custom action to work out what is going on.

Add a class to your project that inherits from Installer,is decorated with this attribute

[RunInstaller(true)]  

and overrides one or more of the following methods:

Install()
Uninstall()
Commit()
Rollback()

Here's a complete example that performs registration with COM Interop. It's sprinkled with trace output which (contrary to some opinions) will show up in Sysinternals DebugVw. When built in Debug configuration, it'll put up a dialog box that allows you to attach a debugger and single step the custom action.

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");
                }
            }
        }
    }

One final thing left to do. The custom actions will not run unless the installer is configured to do so. Here's how:

  • In your Visual Studio installer project, right click on the project name and select View -> Custom Actions. You'll see a tree view something like this:
    • Custom Actions
      • Install
      • Uninstall
      • Commit
      • Rollback
  • Right click the very top node (Custom Actions) and select Add Custom Action.
  • Navigate to the file or project output that contains your class that's decorated with the [RunInstaller(true)] attribute, highlight it and click OK.
  • Your project output should then appear under each of the four nodes. This means that your custom action class will be called during each of the four installer phases.

If you need finer control over this, you can add the custom action class to some of the installer phases but not others. For example, if installation and uninstallation are handled by different assemblies, you migth add one assembly under the Install and Commit nodes and the other assembly under the Uninstall and Rollback nodes.

So that's it, your custom actions will now be called during setup. A quick tip for debugging custom actions. Use a conditional directive (as in the above example) to display a message box when in the debug build. This message box will then be displayed during setup. That effectively pauses the setup process until you click OK on the message box, this gives you what you might call "a window of opportunity" (pun intended) to attach the debugger to the msiexec.exe process. There will be several msiexec.exe processes running, you need to pick the one that says it is managed code. The debugger will attach and your breakpoints will 'light up', so you can then intercept execution and single step through your custom action.

这篇关于为什么我的安装项目不执行我的自定义注册过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆