Adding Custom Actions
你已经熟悉了创建Windows Installer安装包的基本方法,让我们进入下一阶段来添加用户自定义功能。Windows Installer XM的每一个发行版都包含一套WiX Server CustomActions,因此我们将用他们来创建范例。现在打来 wix/bin/ca 目录,拷贝 "sca*.dll" 到 "product.wxs"的目录下。
不同于将CustomAction库文件拷贝到源文件目录下,让我们先练习编写一个名为"sca.wxs"的小模块来定义CustomActions。添加可以直接读取CustomActions Server表和调度不同行为的CustomAction。
<?xml version='1.0'?>
<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>
<Fragment Id="ServerCustomActions">
<CustomAction Id='ConfigureIIs' BinaryKey='ScaSchedule' DllEntry='ConfigureIIs' Execute='immediate'
Return='check'/>
<CustomAction Id='ConfigureSql' BinaryKey='ScaSchedule' DllEntry='ConfigureSql' Execute='immediate'
Return='check'/>
<Binary Id='ScaSchedule' src='scasched.dll'/>
</Fragment>
</Wix>
这段代码可以编译,但不可以链接。记住,链接需要有一个入口节,而<Fragment/>不是一个入口节。我们需要与一个包含<Product/> 或 <Module/>的源文件来链接该文件。在正确处理链接问题之前,先让我们添加其他的CustomActions,这些CustomActions与已经添加的直接CustomActions一样重要。
<?xml version='1.0'?>
<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi'>
<Fragment Id="ServerCustomActions">
<CustomAction Id='ConfigureIIs' BinaryKey='ScaSchedule' DllEntry='ConfigureIIs' Execute='immediate'
Return='check'/>
<CustomAction Id='ConfigureSql' BinaryKey='ScaSchedule' DllEntry='ConfigureSql' Execute='immediate'
Return='check'/>
<CustomAction Id='ErrorOut' BinaryKey='ScaExecute' DllEntry='ErrorOut' Execute='deferred'
Return='check'/>
<CustomAction Id='StartMetabaseTransaction' BinaryKey='ScaExecute'
DllEntry='StartMetabaseTransaction' Execute='deferred' Return='check'/>
<CustomAction Id='RollbackMetabaseTransaction' BinaryKey='ScaExecute'
DllEntry='RollbackMetabaseTransaction' Execute='rollback' Return='check'/>
<CustomAction Id='CommitMetabaseTransaction' BinaryKey='ScaExecute'
DllEntry='CommitMetabaseTransaction' Execute='commit' Return='check'/>
<CustomAction Id='CreateMetabaseKey' BinaryKey='ScaExecute'
DllEntry='CreateMetabaseKey' Execute='deferred' Return='check'/>
<CustomAction Id='DeleteMetabaseKey' BinaryKey='ScaExecute'
DllEntry='DeleteMetabaseKey' Execute='deferred' Return='check'/>
<CustomAction Id='CreateAspApp' BinaryKey='ScaExecute'
DllEntry='CreateAspApp' Execute='deferred' Return='check'/>
<CustomAction Id='WriteMetabaseValue' BinaryKey='ScaExecute'
DllEntry='WriteMetabaseValue' Execute='deferred' Return='check'/>
<CustomAction Id='WriteMetabaseMultiString' BinaryKey='ScaExecute'
DllEntry='WriteMetabaseMultiString' Execute='deferred' Return='check'/>
<CustomAction Id='DeleteMetabaseMultiString' BinaryKey='ScaExecute'
DllEntry='DeleteMetabaseMultiString' Execute='deferred' Return='check'/>
<CustomAction Id='CreateDatabase' BinaryKey='ScaExecute'
DllEntry='CreateDatabase' Execute='deferred' Return='check'/>
<CustomAction Id='DropDatabase' BinaryKey='ScaExecute'
DllEntry='DropDatabase' Execute='deferred' Return='check'/>
<CustomAction Id='ExecuteSqlStrings' BinaryKey='ScaExecute'
DllEntry='ExecuteSqlStrings' Execute='deferred' Return='check'/>
<CustomAction Id='RollbackExecuteSqlStrings' BinaryKey='ScaExecute'
DllEntry='ExecuteSqlStrings' Execute='rollback' Return='check'/>
<Binary Id='ScaSchedule' src='scasched.dll'/>
<Binary Id='ScaExecute' src='scaexec.dll'/>
</Fragment>
</Wix>
好了。我们已经完成了"sca.wxs"的编辑。我们已经成功定义了全部的针对WiX Server CustomActions的入口点。那么,怎样在product.wxs中添加代码来调用WiX Server CustomActions?让我们添加一段测试代码来调用CustomAction以在安装过程中产生一个错误。这就是"ErrorOut" CustomAction。
<?xml version='1.0'?>
<Wix xmlns='http://schemas.microsoft.com/wix/2006/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='Test Program'>
<Component Id='MyComponent' Guid='12345678-1234-1234-1234-123456789012'>
<File Id='readme' Name='readme.txt' DiskId='1' src='readme.txt' />
</Component>
<Merge Id='MyModule' Language='1033' src='module.msm' DiskId='1' />
</Directory>
</Directory>
</Directory>
<Feature Id='MyFeature' Title='My 1st Feature' Level='1'>
<ComponentRef Id='MyComponent' />
<MergeRef Id='MyModule' />
</Feature>
<InstallExecuteSequence>
<Custom Action='ErrorOut' After='InstallFiles'/>
</InstallExecuteSequence>
</Product>
</Wix>
这3行就是调用"ErrorOut" CustomAction所需要的全部代码。现在用light.exe来链接两个文件。以下是编译,链接和安装的步骤。
C:/test> candle product.wxs module.wxs sca.wxs
Microsoft (R) Windows Installer Xml Compiler version 1.0.1256.19889
Copyright (C) Microsoft Corporation 2003. All rights reserved.
product.wxs
module.wxs
sca.wxs
C:/test> light module.wixobj
Microsoft (R) Windows Installer Xml Linker version 1.0.1256.19889
Copyright (C) Microsoft Corporation 2003. All rights reserved.
C:/test> light product.wixobj sca.wixobj ut product.msi
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
当MSI开始回滚安装时,没有任何警告。记着,当安装完文件之后,"ErrorOut" CustomAction被调用,迫使安装失败。MSI于是删除已安装的文件并悄无声息地退出。对于有兴趣的读者,可以添加成功和失败的对话框作为练习。