如何将C#方法添加到现有的大型wix脚本中 [英] How do I add C# methods to an existing large wix script

查看:55
本文介绍了如何将C#方法添加到现有的大型wix脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个非常复杂的现有wix脚本&长.所有的CustomActions都使用内联vbscript执行.

We have an existing wix script that is pretty complex & long. All the CustomActions are performed with inline vbscript.

我想将其中一些动作从vbscript切换到C#.所有示例都从在Visual Studio中创建wix项目..."开始.关于如何将C#代码添加到现有的wix项目中,是否有任何示例?使用旧式的wix命令行应用程序在哪里构建它?

I want to switch some of those actions from vbscript to C#. All the examples everywhere start with "create a wix project in VisualStudio...". Is there any example out there about how to add in C# code to an existing wix project? One where it is built using the old school wix command line apps?

推荐答案

首先要无耻地推广C ++自定义操作! :-).

并且: "WiX快速入门" (一些指向良好的WiX和MSI资源的指针).

And: "WiX Quick Start" (some pointers to good WiX and MSI resources).


循序渐进 :我会尝试的,请尝试一下(如果您已完成,则可能要跳到最下面的源代码通过这些初步步骤-这是分步进行的,而且实际操作起来很慢-您可能会直接从WiX来源获得所需的东西):


Step-By-Step: I'll give it a try, please try this (you might want to skip to the bottom source if you are sort of done with these preliminary steps - this is step-by-step for real and very slow to get to the action - you might get what you need straight from the WiX source):

  1. 在WiX Visual Studio解决方案中, 右键单击顶部的解决方案节点 => 添加 => 新项目...

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

展开WiX Toolset节点,选择v3(前提是您使用的WiX版本)

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

双击 用于WiX v3的C#自定义操作项目""

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

在WiX项目(不是C#项目)中右键单击 参考" => 添加引用...

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

转到 项目" ,并添加对 C#项目 的引用(双击和确定)

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

进行测试构建.只要没有错误,现在就应该没有错误.

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

您应该在构建输出"窗口中看到类似 "CustomAction1.CA.dll" 的内容.将后缀 *.CA.dll 添加到Win32包装dll中,以获取原始托管代码dll.所有这些都是由WiX本身处理的-或实际上是WiX的Votive Visual Studio集成-都知道它们之间的区别:

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" -托管代码dll.
  • "CustomAction1.CA.dll" -包含本地组件和其他几个组件的本地win32包装dll. 将此版本包含在您的MSI中 .

添加以下代码段:

 <Binary Id="CustomActions" SourceFile="$(var.CustomAction1.TargetDir)\$(var.CustomAction1.TargetName).CA.dll" />

  • 以上内容应将实际的C#dll编译为MSI.您可以在Orca中打开MSI,然后在Binary表中查看.

  • The above should compile the actual C# dll into the MSI. You can open the MSI in Orca and see in the Binary table.

    这不是很好,但是我想添加对 System.Windows.Forms 的引用,并使用 MessageBox.Show ,以显示自定义操作中的对话框,以确保该对话框按预期运行.我还为在调试模式下构建的dll添加了应用程序调试器启动命令.这样,Visual Studio将被自动调用(如果所有方法都能正常工作),因此可以逐步执行代码.

    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.

    通过右键单击C#项目的引用"节点,将引用添加到 "System.Windows.Forms" ; System.Windows.Forms" .还要在源文件顶部添加 " using System.Windows.Forms;" -请参见下面的完整源代码.关键是要记住在项目级别引用 "System.Windows.Forms" .

    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.

    现在将其作为测试代码添加到自定义操作项目的 "CustomAction1" 自定义操作代码段中(有关完整源代码,请参见底部的代码部分):

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

  • 要获取标准的设置GUI(供其他阅读此文档的人使用),请添加对 WiXUIExtension 的引用.com/a/47972615/129130>如此处所述(这是创建可编译并具有GUI的基本WiX项目的分步步骤),然后将其注入您的源代码中:

  • 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" />
    

  • 我想将< MediaTemplate/> 更改为< MediaTemplate EmbedCab =是"./> 来避免外部源Cab文件(通过这种方式,将cab编译到MSI中).

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

    如果未添加任何组件,则可以添加此组件以将 notepad.exe 包含在MSI中,以进行目录下的测试安装. INSTALLFOLDER (这是在没有可用源文件的情况下安装某些东西的技巧-应在任何计算机上解析的源路径)-替换整个"TODO"部分-参见下面的完整资源:

    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>
    

  • 现在,我们需要声明实际的自定义操作并将其插入安装序列中.让我们将其添加到先前的 < Binary>下方.元素 :

  • 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>
    

  • 现在构建并测试运行MSI.您应该收到许多 "hello world" 消息.

  • Now build and test run the MSI. You should get numerous "hello world" messages.

    这是C#/托管代码自定义操作的总体 心跳" -我有时使用它们的方式.

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


    WiX源实际 :现在,综合-记住要替换所有GUID!:

    构造: $(env.SystemRoot) -在下面的WiX源中-获取环境变量 %SystemRoot% -解析为在大多数系统上为 C:\ (要列出环境变量,请打开 cmd.exe 并键入 set ,然后按 Enter ).因此,以下来源无需修改即可在所有系统上编译:

    The construct: $(env.SystemRoot) - in the WiX source below - gets the environment variable %SystemRoot% - which resolves to C:\ on most systems (to list environment variables open a cmd.exe and type set and press Enter). The below source should hence compile on all systems without modifications:

    <?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>
    

    简要步骤 :所需更改的简短摘要:

    Steps-in-brief: short summary of changes needed:

    1. 将制造商字段设置为某些内容.
    2. 按照上述指示添加和修改自定义操作结构.
    3. 向底部添加Component/File元素,以替换"TODO"元素.在那里.
    4. 设置 MediaTemplate 以使用如上所述的嵌入式驾驶室(可选,此示例不是必需的).


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


    Custom Action Code: And finally the actual C# custom action test code - updated with Debugger.Launch which will launch the debugger for a debug-build DLL. You can then attach the debugger to the correct source project and step-through the code:

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


    链接 :

    • Debugging MSI Custom Actions
    • C++ Custom Action and Common Problems
    • WiX resource links of all kinds
    • Forgot this one from earlier - simpler and might be clearer
    • C# custom actions - Windows Installer (Advanced Installer video)

    这篇关于如何将C#方法添加到现有的大型wix脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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