如何作为构建后步骤修改内容/替换 .msi 文件的二进制文件?

发布于 2024-09-10 07:42:28 字数 1983 浏览 5 评论 0原文

在 x64 系统上使用 CustomAction 构建 Visual Studio 2010 安装项目时,Visual Studio 包含错误版本的 InstallUtilLib.dll:它安装 32 位填充程序,该填充程序不适用于编译为 64 位的 CustomActions (在我的例子中是一个要求,因为它依赖于 64 位本机 dll)。

安装此类 .msi 会导致 System.BadImageFormat 异常。

根据这篇文章(64位托管使用 Visual Studio 自定义操作),解决方案是在 orca.exe 中打开生成的 .msi 并替换二进制文件“InstallUtil”。

我想自动化这个。有什么想法吗?

编辑:根据 mohlsen 提供的答案,我在解决方案中添加了以下脚本(不是安装项目本身,因为添加到安装项目的文件会进入 msi...):

Option Explicit
rem -----------------------------------------------------------
rem Setup_PostBuildEvent_x64.vbs
rem 
rem Patch an msi with the 64bit version of InstallUtilLib.dll 
rem to allow x64 built managed CustomActions.
rem -----------------------------------------------------------    

Const msiOpenDatabaseModeTransact = 1
Const msiViewModifyAssign         = 3

rem path to the 64bit version of InstallUtilLib.dll
Const INSTALL_UTIL_LIB_PATH = "C:\Windows\Microsoft.NET\Framework64\v2.0.50727\InstallUtilLib.dll"

Dim installer : Set installer = Wscript.CreateObject("WindowsInstaller.Installer")

Dim sqlQuery : sqlQuery = "SELECT `Name`, `Data` FROM Binary"

Dim database
Set database = installer.OpenDatabase(Wscript.Arguments(0), msiOpenDatabaseModeTransact)
Dim view : Set view = database.OpenView(sqlQuery)

Dim record : Set record = installer.CreateRecord(2)
record.StringData(1) = "InstallUtil"
view.Execute record

record.SetStream 2, INSTALL_UTIL_LIB_PATH

view.Modify msiViewModifyAssign, record
database.Commit

Set view = Nothing
Set database = Nothing

接下来,我编辑安装项目属性:我将 PostBuildEvent 属性设置为:

wscript.exe "$(ProjectDir)\..\Setup_PostBuildEvent_x64.vbs" $(BuiltOuputPath)

注意:右键单击解决方案资源管理器中的安装项目,然后选择“属性”会打开错误的对话框( “属性页”)。您需要“属性窗口”(CTRL+W、P)。

When building a Visual Studio 2010 Setup project with a CustomAction on x64 systems, Visual Studio includes the wrong version of InstallUtilLib.dll: It installs the 32bit shim, which will not work for CustomActions compiled as 64-bit (a requirement in my case, since it depends on 64-bit native dlls).

Installing such a .msi results in the System.BadImageFormat exception.

According to this post (64-bit Managed Custom Actions with Visual Studio), the solution is to open the resulting .msi in orca.exe and replace the binary "InstallUtil".

I'd like to automate this. Any ideas?

EDIT: based on the answer provided by mohlsen, I added following script to the solution (not the setup project itself, as files added to the setup project go into the msi...):

Option Explicit
rem -----------------------------------------------------------
rem Setup_PostBuildEvent_x64.vbs
rem 
rem Patch an msi with the 64bit version of InstallUtilLib.dll 
rem to allow x64 built managed CustomActions.
rem -----------------------------------------------------------    

Const msiOpenDatabaseModeTransact = 1
Const msiViewModifyAssign         = 3

rem path to the 64bit version of InstallUtilLib.dll
Const INSTALL_UTIL_LIB_PATH = "C:\Windows\Microsoft.NET\Framework64\v2.0.50727\InstallUtilLib.dll"

Dim installer : Set installer = Wscript.CreateObject("WindowsInstaller.Installer")

Dim sqlQuery : sqlQuery = "SELECT `Name`, `Data` FROM Binary"

Dim database
Set database = installer.OpenDatabase(Wscript.Arguments(0), msiOpenDatabaseModeTransact)
Dim view : Set view = database.OpenView(sqlQuery)

Dim record : Set record = installer.CreateRecord(2)
record.StringData(1) = "InstallUtil"
view.Execute record

record.SetStream 2, INSTALL_UTIL_LIB_PATH

view.Modify msiViewModifyAssign, record
database.Commit

Set view = Nothing
Set database = Nothing

Next, I edited the Setup projects properties: I set the PostBuildEvent property to:

wscript.exe "$(ProjectDir)\..\Setup_PostBuildEvent_x64.vbs" $(BuiltOuputPath)

Note: Right-clicking the setup project in solution explorer and then selecting "Properties" opens up the wrong dialog ("Property Pages"). You want the "Properties Window" (CTRL+W, P).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚风撩人 2024-09-17 07:42:28

不确定您希望如何通过脚本、代码等自动执行此操作。但无论如何,此功能都可以通过 Windows Installer SDK,我相信它现在是 Windows SDK 的一部分(曾经是 Platform SDK)。

无论如何,这是我过去用来手动将文件添加到 MSI 的 VBScript。已经有一段时间了,但我只是在 MSI 上运行它进行测试,并使用 Orca 进行验证,并将程序集添加到二进制表中。这应该为您指明正确的方向。

Option Explicit

Const msiOpenDatabaseModeTransact     = 1
Const msiViewModifyAssign         = 3

Dim installer : Set installer = Nothing
Set installer = Wscript.CreateObject("WindowsInstaller.Installer")

Dim sqlQuery : sqlQuery = "SELECT `Name`,`Data` FROM Binary"

Dim database : Set database = installer.OpenDatabase("YourInstallerFile.msi", msiOpenDatabaseModeTransact)
Dim view     : Set view = database.OpenView(sqlQuery)
Dim record

Set record = installer.CreateRecord(2)
record.StringData(1) = "InstallUtil"
view.Execute record

record.SetStream 2, "InstallUtilLib.dll"

view.Modify msiViewModifyAssign, record 
database.Commit 
Set view = Nothing
Set database = Nothing

希望这有帮助!

Not sure how you want to automate this, through script, code, etc. But in any case, this functionality is all available through the Windows Installer SDK, which I believe is part of the Windows SDK now (used to be the Platform SDK).

Regardless, here is a VBScript I have used in the past to manually add a file to an MSI. It has been a while, but I just ran it on a MSI to test, and verified with Orca and the assembly was added to the binary table. This should point you in the right direction.

Option Explicit

Const msiOpenDatabaseModeTransact     = 1
Const msiViewModifyAssign         = 3

Dim installer : Set installer = Nothing
Set installer = Wscript.CreateObject("WindowsInstaller.Installer")

Dim sqlQuery : sqlQuery = "SELECT `Name`,`Data` FROM Binary"

Dim database : Set database = installer.OpenDatabase("YourInstallerFile.msi", msiOpenDatabaseModeTransact)
Dim view     : Set view = database.OpenView(sqlQuery)
Dim record

Set record = installer.CreateRecord(2)
record.StringData(1) = "InstallUtil"
view.Execute record

record.SetStream 2, "InstallUtilLib.dll"

view.Modify msiViewModifyAssign, record 
database.Commit 
Set view = Nothing
Set database = Nothing

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文