如何向现有的大型wix脚本中添加C#方法

3
我们有一个现有的wix脚本,它非常复杂和冗长。所有的CustomActions都是使用内联vbscript执行的。
我想将其中一些操作从vbscript切换到C#。所有的例子都是以“在VisualStudio中创建一个wix项目…”开始的。是否有任何关于如何向现有的wix项目添加C#代码的示例?其中一个是使用旧版wix命令行应用程序构建的吗?

这个回答解决了你的问题吗?如何在C#中创建自定义操作并将其绑定到Wix安装项目 你可以使用Wix自定义操作创建一个新项目,并在主(现有)Wix项目中引用它。 - undefined
好的,下面添加了一个描述。也许制作一个YouTube视频会更好,但现在就是这样了。我找了一些YouTube视频,有一个来自Advanced Installer的视频(链接:https://www.youtube.com/watch?v=LwUSK58Xf40)。这个视频使用了WiX模板来进行C#自定义操作,但使用了Advanced Installer来包含DLL文件。 - undefined
WiX视频:PainterMesta Automation(不管那是什么)。Keith ReichertAngel Six。似乎都没有涉及自定义操作项目。 - undefined
首先要理解WiX / Windows Installer不是“脚本”,它是一种声明式编程语言,而不是命令式语言。尽量避免使用自定义操作,并谨慎添加它们。 - undefined
1个回答

6

首先,宣传一下C++自定义操作! :-).

还有:“WiX快速入门”(指向一些好的WiX和MSI资源的指针)。


逐步操作:我来试一试,请尝试以下操作(如果您已经完成了这些初步步骤,可以跳到底部源代码 - 这是真正的逐步操作,非常缓慢,以达到操作 - 您可能可以直接从WiX源代码中获得所需内容):

  1. In WiX Visual Studio solution, right click solution node at top => Add => New Project...

  2. Expand WiX Toolset node, select v3 (provided that is the version of WiX you use)

  3. Double click "C# Custom Action Project for WiX v3"

  4. Right click "References" in WiX project (not in C# project) => Add Reference...

  5. Go "Projects" and add a reference to the C# project (double click and OK)

  6. Do a test build. Provided there were no errors before there should be none now.

  7. You should see something like "CustomAction1.CA.dll" in the build Output window. The suffix *.CA.dll is added to a win32 wrapper dll for the original managed code dll. All of this is handled by WiX itself - or actually the Votive Visual Studio integration for WiX - just know the difference:

    • "CustomAction1.dll" - managed code dll.
    • "CustomAction1.CA.dll" - native win32 wrapper dll containing the native one and several other components. Include this version in your MSI.
  8. Add the following snippet:

     <Binary Id="CustomActions" SourceFile="$(var.CustomAction1.TargetDir)\$(var.CustomAction1.TargetName).CA.dll" />
    
  9. The above should compile the actual C# dll into the MSI. You can open the MSI in Orca and see in the Binary table.

  10. It is not great, but I like to add a reference to System.Windows.Forms and use a MessageBox.Show to show a dialog from within the custom action to ensure it is running as expected. I also add the application debugger launch command for dlls built in debug mode. That way Visual Studio will be automatically invoked (if all works correctly) so the code can be stepped through.

  11. Add the reference to "System.Windows.Forms" by right clicking the C# project's Reference node and then add "System.Windows.Forms". Also add "using System.Windows.Forms;" to the top of the source file - see full source below. The key is to remember to reference "System.Windows.Forms" at a project level.

  12. Now add this as test code to the custom action project's "CustomAction1" custom action code snippet (see code section towards bottom for full source):

       // will launch the debugger for debug-build dlls
       #if DEBUG
         System.Diagnostics.Debugger.Launch();
       #endif
    
       MessageBox.Show("hello world");
    
  13. To get a standard setup GUI (for others who read this), add a reference to WiXUIExtension as explained here (that is a step-by-step for creating a basic WiX project that compiles and has a GUI), and then inject this into your source:

     <UIRef Id="WixUI_Mondo" />
    
  14. I like to change <MediaTemplate /> to <MediaTemplate EmbedCab="yes" /> to avoid external source cab files (with this change cabs are compiled into the MSI).

  15. If you don't have any components added, you can add this to include notepad.exe in your MSI for test installation under directory INSTALLFOLDER (just a trick to install something without having source files available - a source path that should resolve on any machine) - replace the whole "TODO" section - see full source below:

     <Component Feature="ProductFeature">
       <File Source="$(env.SystemRoot)\notepad.exe" />
     </Component>
    
  16. Now we need to declare the actual custom action and insert it into an installation sequence. Let's add this underneath the previous <Binary> element:

     <CustomAction Id="CA1" BinaryKey="CustomActions" DllEntry="CustomAction1"/>
    
     <InstallUISequence>
       <Custom Action="CA1" After="CostFinalize" />
     </InstallUISequence>
    
     <InstallExecuteSequence>
       <Custom Action="CA1" After="CostFinalize" />
     </InstallExecuteSequence>
    
  17. Now build and test run the MSI. You should get numerous "hello world" messages.

  18. That is the overall "heartbeat" of a C# / managed code custom action - the way I sometimes use them.


WiX源代码实际内容:现在,我们来综合考虑一下 - 记得替换所有GUID!:

下面的WiX源代码中,构建:$(env.SystemRoot) 获取环境变量%SystemRoot% ,在大多数系统上将解析为C:\ (要列出环境变量,请打开cmd.exe输入 set 并按 Enter)。以下源代码因此应该可以在所有系统上编译而无需修改:

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product Id="*" Name="DemoCA" Language="1033" Version="1.0.0.0" Manufacturer="test" UpgradeCode="0adf972a-5562-4a6f-a552-dd1c16761c55">
        <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />

        <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
        <MediaTemplate EmbedCab="yes" />
    
    <UIRef Id="WixUI_Mondo" />

 <!-- START CUSTOM ACTION CONSTRUCTS -->
    
    <Binary Id="CustomActions" SourceFile="$(var.CustomAction1.TargetDir)\$(var.CustomAction1.TargetName).CA.dll" />

    <CustomAction Id="CA1" BinaryKey="CustomActions" DllEntry="CustomAction1"/>

    <InstallUISequence>
      <Custom Action="CA1" After="CostFinalize" />
    </InstallUISequence>

    <InstallExecuteSequence>
      <Custom Action="CA1" After="CostFinalize" />
    </InstallExecuteSequence>

 <!-- END CUSTOM ACTION CONSTRUCTS -->

    <Feature Id="ProductFeature" Title="AddingCSharpCustomActions" Level="1">
            <ComponentGroupRef Id="ProductComponents" />
        </Feature>
    </Product>

    <Fragment>
        <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="ProgramFilesFolder">
               <Directory Id="INSTALLFOLDER" Name="AddingCSharpCustomActions"/>
            </Directory>
        </Directory>
    </Fragment>

    <Fragment>
        <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">

         <Component Feature="ProductFeature">
           <File Source="$(env.SystemRoot)\notepad.exe" />
         </Component>

        </ComponentGroup>
    </Fragment>
</Wix>

简要步骤:所需更改的简短总结:

  1. 将制造商字段设置为其他内容。
  2. 根据上述指示添加和修改自定义操作构建。
  3. 添加组件/文件元素到底部,替换那里的“TODO”部分。
  4. 按照上述描述将MediaTemplate设置为使用嵌入式cab(可选,对该示例的工作不是必须)。

自定义操作代码:最后是实际的C#自定义操作测试代码 - 更新了Debugger.Launch,它将为调试版本的DLL启动调试器。然后,您可以将调试器附加到正确的源项目并逐步执行代码:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Deployment.WindowsInstaller;
using System.Windows.Forms;

namespace CustomAction1
{
    public class CustomActions
    {
        [CustomAction]
        public static ActionResult CustomAction1(Session session)
        {

#if DEBUG
            System.Diagnostics.Debugger.Launch();
#endif

            MessageBox.Show("hello world");

            session.Log("Begin CustomAction1");

            return ActionResult.Success;
        }
    }
}

链接:


首先,感谢您的详细发布。但是...我在 Visual Studio 2019 中找不到"WiX Toolset节点"。甚至在"安装其他类型"部分也没有。是这个吗?https://wixtoolset.org/releases/ - undefined
我还没有尝试过2019版,不知道这个方法是否有效?也许你可以在虚拟机上试一试。我曾经在虚拟机上只安装了Visual Studio的图形界面以及其他几个组件,就足够运行Wix了。 - undefined
根据我找到的笔记,这是我安装的内容:**1.** .NET Framework 3.5开发工具,**2.** .NET编译器平台SDK,**3.** 随附的几个依赖包:“C#和Visual Basic”以及“.NET编译器平台SDK”等等... 4. Windows 7上已经存在.NET 3.5。**5.** 安装WiX和WiX VS集成。 - undefined
好的,我创建了一个“C# Custom Action Project for WiX v3”,并且我得到了一个项目 - 一个带有CustomAction方法的C#项目。但是没有wix项目。我该如何创建它? - undefined
我只需要插入你已经有的那个?这就是你想要的吧?使用你现有的源代码?如果是的话,请右键点击你的Visual Studio解决方案顶级节点,然后选择添加 => 现有项目... - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接