1

我已经开发了一个巨大的 Windows 桌面应用程序。其中一部分包括用于管理捕获视频和图片的 DLL,以及我没有编写的另一个自定义 COM 组件互操作。

当我在全新安装的 XP 上安装程序时,在尝试访问程序的那些部分时出现错误,提示相应的类未注册。

解决方案中的每个项目都设置为 x86,因此尝试从 64 位环境注册/访问 32 位 COM 库不是问题。

用于视频捕获的两个 DLL 在安装程序的检测到的依赖项列表中。两者都设置为“注册:vsdraCOM”。

另一组 COM 组件根本没有出现在我的 Detected Dependencies 列表中。我已经为他们创建了一个互操作 DLL,那么它可以集成到该项目的 DLL 中吗?

我是否缺少一些基本的东西,或者我是否需要编写自己的自定义操作来在安装期间注册这些(并在卸载期间取消注册)?

4

2 回答 2

3

我确实编写了自己的自定义操作,但它并没有我预期的那么糟糕。

我必须注册几个不同的组件,所以我为它创建了一个私有方法。我会在这里分享我的代码,以防其他人需要帮助。

像自定义操作的所有其他示例一样,这应该作为类库放入它自己的项目中。该类应该是一个组件类类型。然后只需将项目输出复制到安装程序的自定义操作视图中的安装、回滚和卸载文件夹中。(诚​​然,因此在下面的代码中使用 Commit 方法是不必要的,但为了完成主义者的缘故,我将它留在那里)。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace BKForensics.Workbench.InstallerPermissions {
    [RunInstaller(true)]
    public partial class ServiceInstaller : System.Configuration.Install.Installer {
        // This will be the path that all of the DLLs will be located in under Program Files.
        // In the File System of the Installer Project, this is the "Application Folder"
        // The Properties have the Default Location set to [ProgramFilesFolder][Manufacturer]\[ProductName]
        private const string InstallationPath = @"C:\Program Files\Installation";

        // This is the list I had of all the DLLs and OCX files I needed to install for easy reference.
        private const string OCX = "something.ocx";
        private const string Excel = "Interop.Excel.dll";
        private const string Word = "Interop.Word.dll";
        private const string Office = "Interop.Microsoft.Office.Core.dll";

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

            // OCX needs to run with regsvr, so false for the 2nd param.
            RunRegistration(OCX, false, true);
            // The Interops need regasm.
            RunRegistration(Office, true, true);
            RunRegistration(Word, true, true);
            RunRegistration(Excel, true, true);
        }

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

            RunRegistration(OCX, false, false);
            RunRegistration(Office, true, false);
            RunRegistration(Word, true, false);
            RunRegistration(Excel, true, false);
        }

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

            // Uninstall during the Rollback, just in case something happens in the installation.
            RunRegistration(OCX, false, false);
            RunRegistration(Office, true, false);
            RunRegistration(Word, true, false);
            RunRegistration(Excel, true, false);
        }

        public override void Commit(System.Collections.IDictionary savedState) {
            base.Commit(savedState);

            // Nothing needs to be done during Commit.
        }

        static void Main() { }

        /// <summary>
        /// A method to run either regasm or regsvr32 to register a given DLL or OCX.
        /// </summary>
        /// <param name="fileName">The name of the file to register.</param>
        /// <param name="regasm">True to run regasm, false to run regsvr32.exe.</param>
        /// <param name="install">True to install, false to uninstall.</param>
    private static void RunRegistration(string fileName, bool regasm, bool install)     {
            try {
                Process reg = new Process();

                string args = string.Empty;

                if (!install) {
                    // If we're not installing, set it to uninstall.
                    args += " -u ";
                }

                if (regasm) {
                    // Use System.Runtime... to get the latest operating directory where regasm would be located.
                    reg.StartInfo.FileName = Path.Combine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(), "regasm.exe");
                }
                else {
                    reg.StartInfo.FileName = "regsvr32.exe";
                    // Run regsvr32 silently or else it displays a dialog that the OCX was registered successfully.
                    args += " /s ";
                }

                args += " \"" + Path.Combine(InstallationPath, fileName) + "\"";

                reg.StartInfo.Arguments = args;
                reg.StartInfo.UseShellExecute = false;
                reg.StartInfo.CreateNoWindow = true;
                reg.StartInfo.RedirectStandardOutput = true;

                reg.Start();
                reg.WaitForExit();
                reg.Close();
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
于 2013-04-11T16:43:51.277 回答
1

我想你回答了你自己的问题。您应该编写自定义操作来安装 COM 组件。

请参阅:什么是互操作 dll?

于 2013-04-10T20:50:37.450 回答