Original Link: http://blogs.flexerasoftware.com/installtalk/2011/10/i-take-it-all-back-using-windows-installer-msi-rollback-actions.html#sthash.KIIUKl0s.dpuf

By Robert Dickau

Sometimes an installer just needs to do something that Windows Installer doesn't normally do. When that happens, it's a simple matter of writing a custom action, right? Unfortunately, it's not that simple. In this post, we'll look at techniques for reversing changes made by a custom action.

When you launch an installation, it first runs in what's called immediate mode or script-generation mode. As your installer runs in this immediate mode, it generates an internal to-do list of what it will do on a system—"First I'll install files, then I'll create shortcuts, then I'll write to the registry, and then..."—but doesn't yet make any system changes.

After immediate mode is done, your installer then switches to something called deferred mode or script-execution mode. In deferred mode, your installer performs the actions listed in this script: "Now I'm installing files, now I'm creating shortcuts, now I'm writing to the registry, and now I'm..." (The internal to-do list, or script, is fixed at this point, which is why you can't set property values outside of immediate mode, for example).

As your installer runs in deferred mode, it simultaneously creates a rollback script describing how to undo changes made by the standard actions. While the installer installs files, for example, it adds to the rollback script specific information about what it would have to do to get the system back to its pre-installer state: "To get the system back to how it was, I'd need to remove sample.exe and readme.txt, and also restore the original version of sample.dll that I replaced." (Windows Installer will also temporarily hold on to resources such as files that it needs to restore in case of rollback.) If the installer runs to completion, the rollback script and any cached resources are deleted. But if the installation encounters a fatal error, or if the user cancels the installation during deferred mode, the rollback script runs.

A big reason to use standard Windows Installer actions instead of custom actions is that standard actions handle their own cleanup during uninstallation or rollback. When you use a custom action, Windows Installer has no idea what the executable, DLL, InstallScript, etc., that you used did to the system, and therefore has no idea how to roll back the changes the custom action made. If your deferred custom action makes any system changes, you should create a corresponding rollback action.

Immediate mode actions don't write to the rollback script, which means that immediate-mode actions that make system changes won't get rolled back if the installation fails. This is one of several reasons not to make system changes during immediate mode

(Bonus grammar tip: The noun and adjective are one word, rollback, while the verb is two words, roll back. "If my rollback action runs, it will roll back my changes." An easy trick is to see if it's appropriate to form the past tense by adding -ed: You'd say "rolled back", not "rollbacked", and the present tense would be the same number of words. Same goes for cleanup vs. clean up, setup vs. set up, and so on. Anyway.)

With InstallShield, you mark a custom action as being a rollback action using its In-Script Execution setting.

Because the rollback script is created as deferred execution is taking place, and not beforehand, a rollback action must be placed in the Execute sequence before the action it rolls back. (Anywhere before the action being rolled back is fine, but for readability's sake and other reasons, placing the rollback action immediately before the action it rolls back seems to work best.) If you write a deferred custom action called "ChangeSomething", its corresponding rollback action "ChangeSomethingBack" should appear in the sequences immediately before it.

Double Negatives

Things start to get confusing with uninstallation custom actions. Without a condition, an action always runs, both during the initial installation as well as maintenance mode, including a complete uninstallation. By setting a condition on an action, you can specify during which modes it should run: Not Installed for the initial installation, REMOVE="ALL" for complete uninstallation, and so on.

The same holds for rollback actions. If you have a deferred action that makes system changes during the initial installation, you'll need a rollback action in case the installation fails or the user cancels it. You'll also probably need an uninstallation action that reverses the changes during uninstallation; and also an uninstallation rollback action, in case the uninstallation fails or the user cancels it, and you need to undo whatever you just undid. Luckily, the same condition logic applies: if your deferred uninstallation action uses condition REMOVE="ALL", your uninstall-rollback action can use the same condition.

And Finally...

If your deferred action saves any temporary data—similar to how the InstallFiles action temporarily caches files it might need to restore—your rollback action can clean up that data when it reverses the effects of the original action. But what if the rollback action never runs?

Windows Installer defines yet another type of action, called a commit action, which runs only if the installation successfully runs to completion. If a rollback action runs, a commit action won't run; and if a commit action runs, it's too late for rollback. Defining an action as a commit action in InstallShield also involves the In-Script Execution setting, and follows the same condition logic as other deferred and rollback actions.

To summarize, if you create a custom action that makes changes to a target system, you might wind up making several others to handle rollback, uninstallation, uninstallation rollback, and cleanup. If that's not a good argument for avoiding custom actions that duplicate Windows Installer functionality, I don't know what is.

For more information and some hands-on experience—plus information about how things get trickier with all these deferred, rollback, and commit actions needing to read property values—come visit us at our Advanced Windows Installer (MSI) Using InstallShield Training Course.

I Take It All Back: Using Windows Installer (MSI) Rollback Actions的更多相关文章

  1. 安装Windows Installer服务

    Windows Installer 5.0.810.500 下载地址: 电信:http://mdl1.mydown.yesky.com/soft/201303/WindowsInstaller.rar ...

  2. Windows Installer 服务启动错误 14007 的解决办法

    问题: 在 本地计算机 无法启动 Windows Installer 服务. 错误代码 14007: 在活动的激活上下文中没有找到任何查找密钥. 这个问题似乎涉及到 Windows Installer ...

  3. 关于SQL Server 安装程序在运行 Windows Installer 文件时遇到错误

    前几日安装sql server2008r2 的时候碰到这个问题: 出现以下错误: SQL Server 安装程序在运行 Windows Installer 文件时遇到错误. Windows Insta ...

  4. 解决ArcGIS安装之后出现的Windows installer configures问题

    ----Please wait while Windows installer configures ArcGIS Desktop Error Message错误信息 When launching A ...

  5. 【工具】清理Windows Installer冗余文件(支持64位NT6.x系统)

    样子: 支持系统: Windows NT 5.x/6.x 32及64位所有系统.需.net framework 2.0运行环境 功能: 清理上述系统中冗余的Windows Installer补丁文件. ...

  6. windows installer 出错问题解决

    在卸载程序的额时候,如果出现windows installer出错,可以通过一个Windows Installer CleanUp Utility, 有了Windows Installer Clean ...

  7. 安装TortoiseGit出现提示“您必须安装带有更新版本Windows Installer服务的Windows Service Pack”-解决方法

    我的系统是xp sp3安装TortoiseGit时出现了错误提示“您必须安装带有更新版本Windows Installer服务的Windows Service Pack”. 解决方法,到微软官方下载相 ...

  8. How to Set Directory Permissions at Install Time using an MSI Created Using Windows Installer XML (WIX)

    Original Link:  http://blogs.msdn.com/b/cjacks/archive/2008/12/04/how-to-set-directory-permissions-a ...

  9. Installation Phases and In-Script Execution for Custom Actions in Windows Installer

    用 InstallShield 依照 Custom Action Wizard 创建 Custom Action 时,会遇到下面的几个选项: In-Script Execution Install U ...

随机推荐

  1. 可学习的多人人脸识别程序(基于Emgu CV)

    源代码下载(需要安装Emgu CV,安装方法请百度) 很多朋友使用Emgu CV遇到CvInvoke()的报错,我找到一种解决方法. 把EmguCV目录下bin里面的所有dll复制到C:\WINDOW ...

  2. JavaScript要点(八) 闭包

    JavaScript 变量可以是局部变量或全局变量. 私有变量可以用到闭包. 全局变量 函数可以访问由函数内部定义的变量,如: function myFunction() { var a = 4; r ...

  3. jquery 延迟加载代码

    <!--引入以下两个js文件--> <script type="text/javascript" src="./js/jquery.min.js&quo ...

  4. PS Studio调用.exe输出错误信息的解决办法

    在一个button_click下调用了如下外部可执行文件: $button1_Click = { #TODO: Place custom script here .\PsExec.exe \\192. ...

  5. 解读BOM与COM

    概念: 1.BOM(Browser Object Model): 浏览器对象模型,从名字上就能知道它和浏览器关系密切. 浏览器的非常多行为是通过JavaScript控制的.比如打开新窗体.打开关闭标签 ...

  6. ORM之三:DbProvider与DbFactory

    这里涉及到两个关键对象,一个是DbProvider,另一个就是DbFactory.粗略草图如下:   从上图可以看出,开放给消费者的接口就是DbProvider类,不过他主要继承IDbProvider ...

  7. Ewebeditor最新漏洞及漏洞大全

    Ewebeditor最新漏洞及漏洞大全[收集] 来源:转载作者:佚名时间:2009-06-03 00:04:26 下面文章收集转载于网络:) 算是比較全面的ewebeditor编辑器的漏洞收集,如今的 ...

  8. java大数总结【转】

    java大数(2013长春网络赛)--hdu4762总结一下:1.java提交类要写Main.2.读取大数. Scanner read=new Scanner(System.in); BigInteg ...

  9. [MODx] 10. Using Babel for Muti-languages support

    1. Go to 'Extras' -> download and install 'Babel'. 2. Set up '.htaccess' file, currently, we set ...

  10. android学习日记08--Paint画笔

    Paint 要绘图,首先得调整画笔,待画笔调整好之后,再将图像绘制到画布Canvas上,这样才可以显示在手机屏幕上.Android 中的画笔是 Paint类,Paint 中包含了很多方法对其属性进行设 ...