gpt4 book ai didi

c# - 在 MSI 安装程序期间注册 DLL 和 COM 组件的问题

转载 作者:太空宇宙 更新时间:2023-11-03 21:48:06 24 4
gpt4 key购买 nike

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

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

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

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

另一组 COM 组件根本没有出现在我的“检测到的依赖项”列表中。我已经为他们创建了一个 Interop DLL,那么它可以集成到该项目的 DLL 中吗?

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

最佳答案

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

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

与自定义操作的所有其他示例一样,这应该作为类库放在它自己的项目中。该类应该是组件类类型。然后只需将项目输出复制到安装程序的自定义操作 View 中的安装、回滚和卸载文件夹中。 (诚​​然,因为在下面的代码中有 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);
}
}
}
}

关于c# - 在 MSI 安装程序期间注册 DLL 和 COM 组件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15887633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com