在InstallShield中判断是否已安装老版本程序

 
 

     在制作Installer中,有一些安装包,要求先把老版本的程序卸载后,才能安装最新的程序。这样就设计涉及如何检查当前电脑是否已经安装过老版本的程序,如果安装过老的程序,就应该提示用户先把老版本的程序卸载了,才能安装最新的程序。
     InstallShield里的Product Code这个选项,就是一个特定是产品编码,来确定是否是同一个软件的同一个版本。一般我们在有新的版本要发布的时候,都需要修改这个Product Code。比如我的产品:
     1.0.1:
         {2A15BA78-5070-4D58-9D87-1CB4EF9F0591}
     1.0.2:
         {60576944-6FF1-4B24-99DD-3F1C93ADA9C2}
     现在我有最新的2.0.1要发布,其Product Code为:
        {E6246A67-2A88-49C4-B949-40AC600BBDAA}
     我想安装2.0.1的时候,检查1.0.1、1.0.2...等版本是否被安装过,如果安装过,提示用户卸载了再来安装我的2.0.1。
不管怎么样我们先在InstallScript的脚本文件中,建立如下的函数:

export prototype CheckOldVersion(HWND);   

function CheckOldVersion(hMSI)
begin
    RegDBSetDefaultRoot(HKEY_LOCAL_MACHINE);
    if( (RegDBKeyExist("\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{2A15BA78-5070-4D58-9D87-1CB4EF9F0591}")>0)
     || (RegDBKeyExist("\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{60576944-6FF1-4B24-99DD-3F1C93ADA9C2}")>0) )then
         MessageBox("1Another version of this product is already installed.\nInstallation of this version cannot continue. To \nconfigure or remove the existing version of this product,\nuse Add/Remove Programs on the Control Panel.",WARNING);
         abort;
    endif;
     return ERROR_SUCCESS;
end;

     接下去按照安装工程的类型,分两种情况:
     1.Basic MSI Project
       a. 在Behavior and Logic-->Custom Action and Sequences-->Cutom Action右键,new InstallShield,名字改为CheckOldVersion


       b. Function Name选择我们刚创建的函数


       c.Custom Action and Sequences-->Sequences-->Installtion-->User Interface右键-->Insert,选择我们刚新建的Cutom Action(CheckOldVersion)


     2.InstallScript Project
     InstallScript Project中只要在OnFirstUIBefore中的Dlg_Start中插入以上代码就可以了:


当然InstallScript Project也可以使用1的方法来实现版本的检查。


附:把一个Basic MSI Project转为InstallScript Project:
      Project-->Project Converters-->Convert to InstallScript Project.

 
posted @ 2012-08-23 14:23  赤脚的懒虫  阅读(4385)  评论(0编辑  收藏  举报