使用wix

本文指导如何使用Windows Installer XML (WiX) 工具集创建首个安装包,涵盖从源文件编写到安装包构建的全过程,并介绍如何安装及验证安装包。
 

Authoring Your First .wxs File

Pick your favorite XML editor—for all of the examples, I’ll use notepad--and create a new file called “product.wxs”. Nothing about that name is special, but the .wxs extension lets us know that this is a Windows Installer Xml Source File. Now, let’s add the three lines of text all .wxs files have:

<?xml version='1.0'?>
<Wix xmlns='http://schemas.microsoft.com/wix/2003/01/wi'>
</Wix>

That forms the outer skeleton for our source file and, honestly, any other source file we ever want to get compiled. You can feed this empty source file to candle.exe and get out an empty object file. Tell you what, let's do that. Follow the following steps and you should see very similar output:

C:/test> candle product.wxs
Microsoft (R) Windows Installer Xml Compiler version 1.0.1220.15022
Copyright (C) Microsoft Corporation 2003. All rights reserved


C:/test> type product.wixobj
<?xml version="1.0" encoding="utf-8"?><wixObject 
xmlns="http://schemas.microsoft.com/wix/2003/04/objects"
src="C:/test/product.wxs" />

C:/test>

Let's notice a couple things before continuing. First, notice that when there is no error candle doesn't print any text other than its header. In fact, you can even suppress the header output by specifying "-nologo" on the command line. In that case, candle will print nothing unless there is a failure. Second, notice that the path to the original source file is stored in the .wixobj file. This can be useful when tracking down where an error is coming from. In fact, the linker uses that "src" attribute to print more informative error messages when it encounters a problem.

Okay, now that we've seen an empty source file create an empty object file, let's create an installable Windows Installer package. Add the following content to your product.wxs file:

<?xml version='1.0'?>
<Wix xmlns='http://schemas.microsoft.com/wix/2003/01/wi'>
   <Product Id='12345678-1234-1234-1234-123456789012' Name='Test Package' Language='1033' 
            Version='1.0.0.0' Manufacturer='Microsoft Corporation'>
      <Package Id='12345678-1234-1234-1234-123456789012'
               Description='My first Windows Installer package'
               Comments='This is my first attempt at creating a Windows Installer database'
               Manufacturer='Microsoft Corporation' InstallerVersion='200' Compressed='yes' />

      <Directory Id='TARGETDIR' Name='SourceDir'>
         <Component Id='MyComponent' Guid='12345678-1234-1234-1234-123456789012' />
      </Directory>

<Feature Id='MyFeature' Title='My 1st Feature' Level='1'> <ComponentRef Id='MyComponent' /> </Feature> </Product>
</Wix>

This should allow us to create a MSI with a ProductCode of "{12345678-1234-1234-1234-123456789012}" with ProductLanguage of "1033" and a ProductVersion of "1.0.0.0". All of that information is taken from the <Product/> element. The <Package/> element defines all of the information that goes in our MSI's summary information stream. Finally, a simple <Directory/> and <Feature/> tree is created with a single <Component/>. This is enough to get our MSI registered on the machine.

So let's compile, link, and install then take a look at the registered packages for our MSI. Follow the instructions:

Note: This MSI requires admin privileges and will silently fail if you are not installing as an Administrator.

C:/test> candle product.wxs
Microsoft (R) Windows Installer Xml Compiler version 1.0.1220.15022
Copyright (C) Microsoft Corporation 2003. All rights reserved
 
product.wxs
 
C:/test> light product.wixobj
Microsoft (R) Windows Installer Xml Linker version 1.0.1220.15022
Copyright (C) Microsoft Corporation 2003. All rights reserved
 
C:/test> msiexec /i product.msi
 
C:/test> //delivery/tools/msiconfig.exe
.
.
.
{12345678-1234-1234-1234-123456789012} Test Package
.
.
.

You should see your "Test Package" listed with all the other Windows Installer packages installed on your machine. You can also go to Add/Remove Programs in the Control Panel and see "Test Package" registered there. Go ahead and remove the package now, so we don't forget it later.

Great! Now that we have a package that installs and uninstalls properly, let's actually install something. So, create a new text file called "readme.txt" next to your "product.wxs" file and type a message to yourself in there. "Hello, World!" is a favorite. Then, we need to modify the product.wxs to tell it about the file:

<?xml version='1.0'?>
<Wix xmlns='http://schemas.microsoft.com/wix/2003/01/wi'>
   <Product Id='12345678-1234-1234-1234-123456789012' Name='Test Package' Language='1033'
            Version='1.0.0.0' Manufacturer='Microsoft Corporation'>
      <Package Id='12345678-1234-1234-1234-123456789012'
                Description='My first Windows Installer package'
                Comments='This is my first attempt at creating a Windows Installer database'
                Manufacturer='Microsoft Corporation' InstallerVersion='200' Compressed='yes' />
 
      <Media Id='1' Cabinet='product.cab' EmbedCab='yes' />
 
      <Directory Id='TARGETDIR' Name='SourceDir'>
         <Directory Id='ProgramFilesFolder' Name='PFiles'>
            <Directory Id='MyDir' Name='TestProg' LongName='Test Program'>
               <Component Id='MyComponent' Guid='12345678-1234-1234-1234-123456789012'>
                  <File Id='readme' Name='readme.txt' DiskId='1' src='readme.txt' />
               </Component>
            </Directory>
         </Directory>
      </Directory>
 
      <Feature Id='MyFeature' Title='My 1st Feature' Level='1'>
         <ComponentRef Id='MyComponent' />
      </Feature>
   </Product>
</Wix>

You should be able to compile, link, and install that MSI and see that you do get a directory called "Test Program" in your system's "Program Files" folder. In that "Test Program" directory should be the "readme.txt" file you created with the message to yourself. Spiffy, eh? Again, remember to uninstall the MSI so you can rebuild and install it again later.

Believe it or not, that's all there is to creating a Windows Installer package. Sure, you can add UI and things like that now, but we've covered the basics. Everything just comes down to filling in the right XML elements. So, let's move on and look at creating a Merge Module we can incorporate into our spiffy new package.

### 如何在 Visual Studio 中使用 Wix 工具集 Wix 是一种用于创建 Windows 安装程序的开源工具集,许多微软产品开发团队也依赖于它来交付其安装包[^2]。为了在 Visual Studio 中集成并高效使用 Wix 工具集,可以按照以下方法操作。 #### 集成 Wix 到 Visual Studio 的环境配置 1. **下载与安装 Wix Toolset** 访问官方站点获取最新版本的 Wix 工具集,并完成安装过程。此过程中会自动注册必要的扩展到支持的 Visual Studio 版本中。 2. **启用 Wix 扩展** 如果未自动检测到 Wix 支持,则需手动确认是否启用了对应的项目模板和构建工具链。这通常通过 `Tools -> Extensions and Updates` 菜单实现。 3. **新建或导入 Wix 项目** 使用 Visual Studio 提供的新建项目向导,选择 `.wxs` 文件作为主要源码形式定义安装逻辑。对于多语言支持场景,可利用 WixUIExtension 来加载不同区域设置的语言资源文件 (.wxl)[^4]。 #### 编写基础的 Wix XML 描述 下面是一个简单的示例脚本展示如何描述目标应用程序及其组件: ```xml <?xml version="1.0" encoding="UTF-8"?> <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"> <Product Id="*" Name="MyAppInstaller" Language="1033" Version="1.0.0.0" Manufacturer="Example Corp." UpgradeCode="PUT-GUID-HERE"> <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine"/> <MajorUpgrade DowngradeErrorMessage="A newer version of MyApp is already installed."/> <MediaTemplate/> <Feature Id="ProductFeature" Title="My Application Feature" Level="1"> <ComponentGroupRef Id="ProductComponents"/> </Feature> </Product> <Fragment> <Directory Id="TARGETDIR" Name="SourceDir"> <Directory Id="ProgramFilesFolder"> <Directory Id="INSTALLFOLDER" Name="MyApp"/> </Directory> </Directory> </Fragment> <Fragment> <ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER"> <Component Id="MainExecutable"> <File Source="$(var.MyApp.TargetPath)" KeyPath="yes"/> </Component> </ComponentGroup> </Fragment> </Wix> ``` 上述代码片段展示了基本的产品结构声明方式,包括目录布局、特性分组以及实际二进制文件映射等内容。 #### 构建与调试流程 一旦编写好相应的 `.wxs` 或其他关联类型的元数据文档之后,在解决方案管理器内右键点击该项目即可触发编译动作。成功完成后将会生成标准 MSI 输出产物可供部署验证之用。 值得注意的是,尽管存在像 Code Digger 这样的强大辅助分析手段可以帮助发现潜在边界条件问题[^1],但在设计复杂度较高的定制化安装方案时仍需谨慎对待每一个细节环节以免遗漏重要考量因素。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值