Wix学习笔记(一)

来源:互联网 发布:ubuntu可以做什么 编辑:程序博客网 时间:2024/05/17 02:11
Title:Wix学习笔记
Date: 2015-05-18

Environment:

VS2013Update4、Win7SP1、WinXPSP3、WiX Toolset v3.9 R2



内容简介:
学习使用Wix来打安装包。


最简单的安装程序(共三步)。
为testWix添加SetupProject1安装项目。
第一步:
打开现有的C++项目,[Add New Project]->[Windows Installer XAML]->[Setup Project]添加安装项目。
第二步:打开Product.wxs源文件,并修改。
<?xml version="1.0" encoding="UTF-8"?><Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">  <!--Product节点Name属性指定了控制面板卸载程序列表里的名字--><Product Id="*" Name="SetupProject1" Language="1033" Version="1.0.0.0" Manufacturer="kagula" UpgradeCode="7b8f5cf7-d71f-4140-9616-9f5088c4e45b"><Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" /><MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." /><MediaTemplate /><Feature Id="ProductFeature" Title="SetupProject1" Level="1"><ComponentGroupRef Id="ProductComponents" /></Feature></Product><Fragment><Directory Id="TARGETDIR" Name="SourceDir"><Directory Id="ProgramFilesFolder"><Directory Id="INSTALLFOLDER" Name="SetupProject1" /><!--Name属性指定了目标安装目录--></Directory></Directory></Fragment><Fragment><ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER"><!-- TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. --><!-- <Component Id="ProductComponent"> --><!-- TODO: Insert files, registry keys, and other resources here. --><!-- </Component> -->      <Component Id="ProductComponent" Guid="B5F0C012-49D6-4C63-AFCA-0CE6C24C6D7D">        <File Id="HelloWorld" Source="../Debug/testWiX.exe" />      </Component></ComponentGroup></Fragment></Wix>

第三步:
编译并运行,即可。
输出目录在.....\testWiX\SetupProject1\bin\Debug

Note “testWiX” is Solution name,“SetupProject1” is set project name.

---------------------------------------------------------------------------------------------------------------------------------
使用预定义变量(三步)
第一步:为安装工程(SetupProject1)添加程序项目(testWiX)引用。
[SetupProject1]->[References]->[Add Reference]->[Projects]->添加testWiX项目。
第二步:Product.wxs文件下面的源码
<File Id="HelloWorld" Source="../Debug/testWiX.exe" />
替换为
<File Id="HelloWorld" Source="../$(var.testWiX.Configuration)/$(var.testWiX.TargetFileName)" />
第三步:重新编译即可。
参考资料[4]查看其它预定义变量,进一步阅读参数资料[3]。
---------------------------------------------------------------------------------------------------------------------------------
开始菜单快捷方式(三步)
第一步:编译Product.wxs文件。
在Fragment的Id为TARGETDIR的Directory子节点中插入下面的节点,指示开始菜单下要有个testWiX文件夹。
        <Directory Id="ProgramMenuFolder"><!--Directory节点Id属性[1]指示这是个什么目录?这里指的是开始菜单(目录)[2]用来Reference-->
          <Directory Id="ApplicationProgramsFolder" Name="testWiX"/><!--Name属性指示,开始菜单的什么子文件夹》-->
        </Directory>
第二步:
在什么位置,新建什么名字的Shortcut。
  添加下面的Fragment。

  <Fragment>    <DirectoryRef Id="ApplicationProgramsFolder"><!--Id属性为ApplicationProgramsFolder同上面一致-->      <!--a <Component> element to specify an atomic unit of installation...-->      <Component Id="ApplicationShortcut" Guid="{6EC4A3C2-147D-41C4-BC14-814EEAC48C37}">        <!--Shortcut节点的Name属性,指示快捷键的名称-->        <Shortcut Id="ApplicationStartMenuShortcut"             Name="testWiX"           Description="My Application Description"            Target="[INSTALLFOLDER]$(var.testWiX.TargetFileName)"                  WorkingDirectory="INSTALLFOLDER"/>        <RemoveFolder Id="ApplicationProgramsFolder" On="uninstall"/><!--Id属性为ApplicationProgramsFolder同上面一致-->        <RegistryValue Root="HKCU" Key="Software\Microsoft\testWiX" Name="installed" Type="integer" Value="1" KeyPath="yes"/>      </Component>    </DirectoryRef>  </Fragment>


第三步:安装Id为ApplicationShortcut的Component。
Product节点下Id为ProductFeature的Feature节点添加下面的代码段。
<ComponentRef Id="ApplicationShortcut" />  
编译并运行即可。
SetupProject1完整的清单如下:

<?xml version="1.0" encoding="UTF-8"?><Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">  <!--Product节点Name属性指定了控制面板卸载程序列表里的名字-->  <!--Manufacturer属性会在控制面板里面发布者栏目里显示-->  <Product Id="*" Name="SetupProject1" Language="1033" Version="1.0.0.0" Manufacturer="kagula" UpgradeCode="7b8f5cf7-d71f-4140-9616-9f5088c4e45b">    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />    <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />    <MediaTemplate />    <Feature Id="ProductFeature" Title="SetupProject1" Level="1">      <ComponentGroupRef Id="ProductComponents" />      <!-- AddShortcut->Step3: Tell WiX to install the shortcut -->      <ComponentRef Id="ApplicationShortcut" />      </Feature>  </Product>  <Fragment>    <Directory Id="TARGETDIR" Name="SourceDir">      <Directory Id="ProgramFilesFolder">        <Directory Id="INSTALLFOLDER" Name="SetupProject1">          <!--Name属性指定了目标安装目录-->        </Directory>        <!--AddShortcut->Step1:Create the folder-->        <Directory Id="ProgramMenuFolder"><!--Directory节点Id属性[1]指示这是个什么目录?这里指的是开始菜单(目录)[2]用来Reference-->          <Directory Id="ApplicationProgramsFolder" Name="testWiX"/><!--Name属性指示,开始菜单的什么子文件夹》-->        </Directory>      </Directory>    </Directory>  </Fragment>  <Fragment>    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">      <!-- TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. -->      <!-- <Component Id="ProductComponent"> -->      <!-- TODO: Insert files, registry keys, and other resources here. -->      <!-- </Component> -->      <!-- How to generate GUID? VS Main Manu->Tools->Create GUID->Registry Format->Copy->....->Paste to where be need. -->      <Component Id="ProductComponent" Guid="B5F0C012-49D6-4C63-AFCA-0CE6C24C6D7D">        <File Id="HelloWorld" Source="../$(var.testWiX.Configuration)/$(var.testWiX.TargetFileName)" />      </Component>    </ComponentGroup>  </Fragment>  <!-- Step 2: Add the shortcut to your installer package -->  <Fragment>    <DirectoryRef Id="ApplicationProgramsFolder"><!--Id属性为ApplicationProgramsFolder同上面一致-->      <!--a <Component> element to specify an atomic unit of installation...-->      <Component Id="ApplicationShortcut" Guid="{6EC4A3C2-147D-41C4-BC14-814EEAC48C37}">        <Shortcut Id="ApplicationStartMenuShortcut"             Name="testWiX"           Description="My Application Description"            Target="[INSTALLFOLDER]$(var.testWiX.TargetFileName)"                  WorkingDirectory="INSTALLFOLDER"/>        <RemoveFolder Id="ApplicationProgramsFolder" On="uninstall"/><!--Id属性为ApplicationProgramsFolder同上面一致-->        <RegistryValue Root="HKCU" Key="Software\Microsoft\testWiX" Name="installed" Type="integer" Value="1" KeyPath="yes"/>      </Component>    </DirectoryRef>  </Fragment></Wix>
---------------------------------------------------------------------------------------------------------------------------------
反安装快捷方式(二步)

第一步:

在上文shortcuts节点下添加下面这段代码即可

        <Shortcut Id="UninstallProduct"                  Name="Uninstall My Application"                  Description="Uninstalls My Application"                  Target="[System64Folder]msiexec.exe"                  Arguments="/x [ProductCode]"/>

第二步:正常情况下,现在可编译运行了。

下面是修改后的源码清单

<?xml version="1.0" encoding="UTF-8"?><Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">  <!--Product节点Name属性指定了控制面板卸载程序列表里的名字-->  <!--Manufacturer属性会在控制面板里面发布者栏目里显示-->  <Product Id="*" Name="SetupProject1" Language="1033" Version="1.0.0.0" Manufacturer="kagula" UpgradeCode="7b8f5cf7-d71f-4140-9616-9f5088c4e45b">    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />    <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />    <MediaTemplate />    <Feature Id="ProductFeature" Title="SetupProject1" Level="1">      <ComponentGroupRef Id="ProductComponents" />      <!-- AddShortcut->Step3: Tell WiX to install the shortcut -->      <ComponentRef Id="ApplicationShortcut" />    </Feature>  </Product>  <Fragment>    <Directory Id="TARGETDIR" Name="SourceDir">      <Directory Id="ProgramFilesFolder">        <Directory Id="INSTALLFOLDER" Name="SetupProject1">          <!--Name属性指定了目标安装目录-->        </Directory>        <!--AddShortcut->Step1:Create the folder-->        <Directory Id="ProgramMenuFolder">          <!--Directory节点Id属性[1]指示这是个什么目录?这里指的是开始菜单(目录)[2]用来Reference-->          <Directory Id="ApplicationProgramsFolder" Name="testWiX"/>          <!--Name属性指示,开始菜单的什么子文件夹》-->        </Directory>      </Directory>    </Directory>  </Fragment>  <Fragment>    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">      <!-- TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. -->      <!-- <Component Id="ProductComponent"> -->      <!-- TODO: Insert files, registry keys, and other resources here. -->      <!-- </Component> -->      <!-- How to generate GUID? VS Main Manu->Tools->Create GUID->Registry Format->Copy->....->Paste to where be need. -->      <Component Id="ProductComponent" Guid="B5F0C012-49D6-4C63-AFCA-0CE6C24C6D7D">        <File Id="HelloWorld" Source="../$(var.testWiX.Configuration)/$(var.testWiX.TargetFileName)" />      </Component>    </ComponentGroup>  </Fragment>  <!-- Step 2: Add the shortcut to your installer package -->  <Fragment>    <DirectoryRef Id="ApplicationProgramsFolder">      <!--Id属性为ApplicationProgramsFolder同上面一致-->      <!--a <Component> element to specify an atomic unit of installation...-->      <Component Id="ApplicationShortcut" Guid="{6EC4A3C2-147D-41C4-BC14-814EEAC48C37}">        <Shortcut Id="ApplicationStartMenuShortcut"             Name="testWiX"           Description="My Application Description"            Target="[INSTALLFOLDER]$(var.testWiX.TargetFileName)"                  WorkingDirectory="INSTALLFOLDER"/>        <!--Add the uninstall shortcut to your installer package -->        <Shortcut Id="UninstallProduct"                  Name="Uninstall My Application"                  Description="Uninstalls My Application"                  Target="[System64Folder]msiexec.exe"                  Arguments="/x [ProductCode]"/>        <RemoveFolder Id="ApplicationProgramsFolder" On="uninstall"/>        <!--Id属性为ApplicationProgramsFolder同上面一致-->        <RegistryValue Root="HKCU" Key="Software\Microsoft\testWiX" Name="installed" Type="integer" Value="1" KeyPath="yes"/>      </Component>    </DirectoryRef>  </Fragment></Wix>


---------------------------------------------------------------------------------------------------------------------------------
桌面快捷方式(二步)

第一步:上文Directory节点Id为TARGETDIR的节点下面添加下面这行代码

<Directory Id="DesktopFolder" Name="Desktop"/>

第二步:添加下面的component

    <DirectoryRef Id="DesktopFolder">      <!--Component的Guid属性也可自动生成,免去了手动生成GUID的麻烦-->      <!--桌面什么样的快捷方式-->      <Component Id="ApplicationShortcutDesktop" Guid="*">        <Shortcut Id="ApplicationDesktopShortcut"           Name="Text under your icon"           Description="Comment field in your shortcut"           Target="[INSTALLFOLDER]$(var.testWiX.TargetFileName)"           WorkingDirectory="INSTALLFOLDER"/>        <RemoveFolder Id="DesktopFolder" On="uninstall"/>        <RegistryValue          Root="HKCU"          Key="Software/testWix"          Name="installed"          Type="integer"          Value="1"          KeyPath="yes"/>      </Component>    </DirectoryRef>

第三步:

 Feature节点Id为ProductFeature的节点下面添加下面这个节点
      <ComponentRef Id="ApplicationShortcutDesktop" />

现在编译运行即可

下面是完整的代码清单

<?xml version="1.0" encoding="UTF-8"?><Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">  <!--Product节点Name属性指定了控制面板卸载程序列表里的名字-->  <!--Manufacturer属性会在控制面板里面发布者栏目里显示-->  <Product Id="*" Name="SetupProject1" Language="1033" Version="1.0.0.0" Manufacturer="kagula" UpgradeCode="7b8f5cf7-d71f-4140-9616-9f5088c4e45b">    <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />    <MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />    <MediaTemplate />    <Feature Id="ProductFeature" Title="SetupProject1" Level="1">      <ComponentGroupRef Id="ProductComponents" />      <!-- AddShortcut->Step3: Tell WiX to install the shortcut -->      <ComponentRef Id="ApplicationShortcut" />      <ComponentRef Id="ApplicationShortcutDesktop" />    </Feature>  </Product>  <Fragment>    <Directory Id="TARGETDIR" Name="SourceDir">      <Directory Id="ProgramFilesFolder">        <Directory Id="INSTALLFOLDER" Name="SetupProject1">          <!--Name属性指定了目标安装目录-->        </Directory>        <!--AddShortcut->Step1:Create the folder-->        <Directory Id="ProgramMenuFolder">          <!--Directory节点Id属性[1]指示这是个什么目录?这里指的是开始菜单(目录)[2]用来Reference-->          <Directory Id="ApplicationProgramsFolder" Name="testWiX"/>          <!--Name属性指示,开始菜单的什么子文件夹》-->        </Directory>      </Directory>      <!--桌面快捷方式存放位置-->      <Directory Id="DesktopFolder" Name="Desktop"/>    </Directory>  </Fragment>  <Fragment>    <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">      <!-- TODO: Remove the comments around this Component element and the ComponentRef below in order to add resources to this installer. -->      <!-- <Component Id="ProductComponent"> -->      <!-- TODO: Insert files, registry keys, and other resources here. -->      <!-- </Component> -->      <!-- How to generate GUID? VS Main Manu->Tools->Create GUID->Registry Format->Copy->....->Paste to where be need. -->      <Component Id="ProductComponent" Guid="B5F0C012-49D6-4C63-AFCA-0CE6C24C6D7D">        <File Id="HelloWorld" Source="../$(var.testWiX.Configuration)/$(var.testWiX.TargetFileName)" />      </Component>    </ComponentGroup>  </Fragment>  <!-- Step 2: Add the shortcut to your installer package -->  <Fragment>    <DirectoryRef Id="ApplicationProgramsFolder">      <!--Id属性为ApplicationProgramsFolder同上面一致-->      <!--a <Component> element to specify an atomic unit of installation...-->      <Component Id="ApplicationShortcut" Guid="{6EC4A3C2-147D-41C4-BC14-814EEAC48C37}">        <Shortcut Id="ApplicationStartMenuShortcut"             Name="testWiX"           Description="My Application Description"            Target="[INSTALLFOLDER]$(var.testWiX.TargetFileName)"                  WorkingDirectory="INSTALLFOLDER"/>        <!--Add the uninstall shortcut to your installer package -->        <Shortcut Id="UninstallProduct"                  Name="Uninstall My Application"                  Description="Uninstalls My Application"                  Target="[System64Folder]msiexec.exe"                  Arguments="/x [ProductCode]"/>        <RemoveFolder Id="ApplicationProgramsFolder" On="uninstall"/>        <!--Id属性为ApplicationProgramsFolder同上面一致-->        <RegistryValue Root="HKCU" Key="Software\testWiX" Name="installed" Type="integer" Value="1" KeyPath="yes"/>      </Component>    </DirectoryRef>    <DirectoryRef Id="DesktopFolder">      <!--Component的Guid属性也可自动生成,免去了手动生成GUID的麻烦-->      <!--桌面什么样的快捷方式-->      <Component Id="ApplicationShortcutDesktop" Guid="*">        <Shortcut Id="ApplicationDesktopShortcut"           Name="Text under your icon"           Description="Comment field in your shortcut"           Target="[INSTALLFOLDER]$(var.testWiX.TargetFileName)"           WorkingDirectory="INSTALLFOLDER"/>        <RemoveFolder Id="DesktopFolder" On="uninstall"/>        <RegistryValue          Root="HKCU"          Key="Software/testWix"          Name="installed"          Type="integer"          Value="1"          KeyPath="yes"/>      </Component>    </DirectoryRef>  </Fragment></Wix>


---------------------------------------------------------------------------------------------------------------------------------
如何生成setup.exe(二步)

第一步:在当前Solution中添加新的project。

[Windows Installer]->[Windows Installer XML]->[Bootstrapper Project]->缺省项目名称为Bootstrapper1

第二步:修改源文件

修改Bundle.wxs源码,修改后的代码如下。

<?xml version="1.0" encoding="UTF-8"?><Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"><Bundle Name="Bootstrapper1" Version="1.0.0.0" Manufacturer="kagula" UpgradeCode="cb5b9960-a00c-492f-9159-01a3c5870c5b"><BootstrapperApplicationRef Id="WixStandardBootstrapperApplication.RtfLicense" /><Chain><!-- TODO: Define the list of chained packages. --><!-- <MsiPackage SourceFile="path\to\your.msi" /> -->      <MsiPackage SourceFile="..\SetupProject1\bin\Debug\SetupProject1.msi"/>    </Chain></Bundle></Wix>


第三步:编译并运行

现在你会发现,安装过程终于有了界面,重复安装也有界面提示。

Bundle.wxs文件里,你也可以设置安装前提。比如说,我们的软件需要.Net Framework 4支持,在这里可以添加代码检查

DotNetFramework4有没有安装,如果没有安装,则从网络或磁盘介质中开始安装。

经测试WinXP下可以直接使用。
---------------------------------------------------------------------------------------------------------------------------------
中文支持、自定义安装界面

参考资料

http://www.tuicool.com/articles/vAJBFf

但是要注意:只需要添加“WixUtilExtension”,zh-cn.wxl文件不需要,老的反安装快捷键shortcut节点不能用了,因为它不会卸载bootstrapper程序。

Bundle.wxs文件清单如下

<?xml version="1.0" encoding="UTF-8"?><Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:bal="http://schemas.microsoft.com/wix/BalExtension">  <Bundle Name="Bootstrapper1" Version="1.0.0.0" Manufacturer="kagula" UpgradeCode="cb5b9960-a00c-492f-9159-01a3c5870c5b">    <BootstrapperApplicationRef Id="WixStandardBootstrapperApplication.RtfLicense" >      <bal:WixStandardBootstrapperApplication LicenseFile="License.rtf" ThemeFile="CustomTheme.xml" LocalizationFile="CustomLocalize.wxl"/>    </BootstrapperApplicationRef>    <Chain>      <!--TODO: Define the list of chained packages. -->      <!--SourceFile="$(var.YourMsiProject.Installer.TargetPath)"-->      <MsiPackage SourceFile="..\SetupProject1\bin\Debug\SetupProject1.msi">      </MsiPackage>    </Chain>  </Bundle></Wix>


参数资料:
[1]《官方下载地址》
http://wix.codeplex.com/
[2]《How to define a global variable in WiX》
http://stackoverflow.com/questions/8495903/how-to-define-a-global-variable-in-wix
[3]《Proprocessor》
http://wixtoolset.org/documentation/manual/v3/overview/preprocessor.html
[4]《Using Project References and Variables》
http://wixtoolset.org/documentation/manual/v3/votive/votive_project_references.html

[5]《WiX Proper Creation of Desktop Shortcut》

http://stackoverflow.com/questions/20909913/wix-proper-creation-of-desktop-shortcut

[6]《WiX Burn - Determine what items are already installed》

http://stackoverflow.com/questions/12917287/wix-burn-determine-what-items-are-already-installed

[7]《bootstrapping》

https://www.firegiant.com/wix/tutorial/net-and-net/bootstrapping/

[8]《Using Project References and Variables》

http://wixtoolset.org/documentation/manual/v3/votive/votive_project_references.html


0 0
原创粉丝点击