杉宫竹苑工作室

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2684|回复: 0

Installshield 新版本安装时卸载旧版本的安装

[复制链接]
发表于 2017-3-1 15:24:13 | 显示全部楼层 |阅读模式

正式会员享受无限制浏览网站功能和高速网盘下载,赶快加入本站吧!

您需要 登录 才可以下载或查看,没有账号?立即注册

x
安装包升级问题,搞得实在头大,经过反复摸索,最终决定卸载旧的安装,安装新的程序,以达到升级目的。本方案有所局限,对于大型的安装或者配置复杂的安装不适合。本案例用IS2009,InstallScript MSI工程建立测试的。
比如:已安装1.0.0.1版本的程序,现在做了个新的1.0.0.2版本的程序。那么在第一个版本里我们就要开始做以下工作,并在以后版本要记得更改产品ID。
首先,我们需要声明两个方法:
export prototype UninstallOldVersion(); //卸载旧版本
export prototype WriteInstallGuid();//将本安装包的GUID写入注册表
在OnBegin函数里,写判断是否有旧的安装:
声明以下自定义变量
  1. string szNumName,szNumValue;
  2. number nType,nSize;
  3. //旧的安装GUID
  4. RegDBSetDefaultRoot(HKEY_LOCAL_MACHINE);
  5. if(RegDBKeyExist(szKey)>0)then
  6.   szNumName="Test";
  7.   nType=REGDB_STRING;
  8.   RegDBGetKeyValueEx(szKey,szNumName,nType,szNumValue,nSize);
  9.   oldGuid= szNumValue;//获取旧安装的产品ID
  10. endif;  
复制代码

写一个卸载函数:
  1. function UninstallOldVersion()
  2. string szPath,UninstallString;
  3. string szNumName,szNumValue;
  4. number nType,nSize;
  5. begin      
  6.   szPath=WINDIR+"Installer\"+oldGuid;
  7.   RegDBSetDefaultRoot(HKEY_LOCAL_MACHINE);
  8.   UninstallString="\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\InstallShield_"+oldGuid;
  9.   nType=REGDB_STRING;
  10.   szNumName="UninstallString";
  11.   RegDBGetKeyValueEx(UninstallString,szNumName,nType,szNumValue,nSize); //获取卸载脚本   
  12.   if(RegDBKeyExist(UninstallString)>0) then   
  13.     if(AskYesNo("Install the software detects an earlier version, in order to ensure the software is installed correctly uninstall that version!",NO)=YES) then   
  14.       SdShowMsg("Uninstalling the program, please wait ...",TRUE);   
  15.        Delay(1);   
  16.       LaunchAppAndWait(szNumValue," /S",LAAW_OPTION_WAIT);      
  17.            //刷新注册表      
  18.            LaunchAppAndWait ( "","cmd /c gpupdate /force /wait:0 ",LAAW_OPTION_WAIT|LAAW_OPTION_HIDDEN);  
  19.            SdShowMsg("", FALSE);   
  20.            SdShowMsg("Uninstall is complete",TRUE);     
  21.            Delay(2);      
  22.       SdShowMsg("", FALSE);   
  23.     LaunchAppAndWait( WINSYSDIR^"cmd.exe", "/c rd /s/q ""+szPath+""", LAAW_OPTION_WAIT | LAAW_OPTION_HIDDEN);  
  24.    else
  25.       abort;
  26.   endif;
  27. endif;
  28. end;
复制代码

我们在OnFirstUIBefore里调用UninstallOldVersion函数,以执行卸载操作;
对于安装包GUID在OnFirstUIAfter里进行写入,在OnFirstUIAfter调用WriteInstallGuid函数,以下为WriteInstallGuid函数脚本:
  1. function WriteInstallGuid()
  2. string svSubStr,productCode;
  3. number nRootKey,nType;
  4. begin
  5.      //写注册表
  6.     if(OS32=TRUE) then//判断32位还是64位
  7.   StrSub(svSubStr,UNINSTALL_STRING,57,38); //获取guid  
  8. else
  9.   StrSub(svSubStr,UNINSTALL_STRING,63,38);
  10. endif;
  11. productCode=svSubStr;
  12. nRootKey=HKEY_LOCAL_MACHINE;
  13. RegDBSetDefaultRoot(nRootKey);
  14. nType=REGDB_STRING;
  15. RegDBSetKeyValueEx(szKey,"Test",nType,productCode,-1);   
  16. end;
复制代码


附上工程所有脚本:

  1. //===========================================================================

  2. // Included header files ----------------------------------------------------
  3. #include "ifx.h"
  4. //卸载旧的安装
  5. #define szKey "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Infomedia_Dog\"  
  6. string oldGuid;
  7. BOOL OS64,OS32;
  8. //获取安装包位置
  9. STRING SETUPEXEDIR[MAX_PATH + 1];
  10. string setupDIR;
  11. number  nBuffer;  

  12. export prototype UninstallOldVersion(); //卸载旧版本
  13. export prototype WriteInstallGuid();//将本安装包的GUID写入注册表
  14. //---------------------------------------------------------------------------
  15. // OnBegin
  16. //
  17. // The OnBegin event is called directly by the framework after the setup
  18. // initializes.
  19. //---------------------------------------------------------------------------
  20. function OnBegin()
  21. string szNumName,szNumValue;
  22. number nType,nSize;
  23. begin  
  24.     SetTitle(IFX_PRODUCT_NAME + " -Infomedia", 0, BACKGROUNDCAPTION);  
  25.     //安装包路径
  26.     nBuffer=MAX_PATH+1;
  27.     MsiGetProperty(ISMSI_HANDLE,"SETUPEXEDIR",SETUPEXEDIR,nBuffer);
  28.        setupDIR= SETUPEXEDIR+"\\DogSetup.exe";  
  29.        //判断操作系统
  30.      if (SYSINFO.bIsWow64) then
  31.           OS64=TRUE;
  32.      else
  33.           OS32=TRUE;
  34.      endif;
  35.     //旧的安装GUID
  36.     RegDBSetDefaultRoot(HKEY_LOCAL_MACHINE);
  37.     if(RegDBKeyExist(szKey)>0)then
  38.         szNumName="Dog";
  39.         nType=REGDB_STRING;
  40.         RegDBGetKeyValueEx(szKey,szNumName,nType,szNumValue,nSize);
  41.         oldGuid= szNumValue;
  42.         
  43.     endif;   
  44. end;  

  45. function UninstallOldVersion()  
  46. string szPath,UninstallString;
  47. string szNumName,szNumValue;
  48. number nType,nSize;
  49. begin      
  50. szPath=WINDIR+"Installer\"+oldGuid;

  51. RegDBSetDefaultRoot(HKEY_LOCAL_MACHINE);
  52. UninstallString="\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\InstallShield_"+oldGuid;
  53. nType=REGDB_STRING;
  54. szNumName="UninstallString";
  55. RegDBGetKeyValueEx(UninstallString,szNumName,nType,szNumValue,nSize); //获取卸载脚本
  56.     if(RegDBKeyExist(UninstallString)>0) then   
  57.         if(AskYesNo("Install the software detects an earlier version, in order to ensure the software is installed correctly uninstall that version!",NO)=YES) then   
  58.                 SdShowMsg("Uninstalling the program, please wait ...",TRUE);
  59.                 Delay(1);
  60.                 LaunchAppAndWait(szNumValue," /S",LAAW_OPTION_WAIT);                 
  61.                 //DeleteDir("C:\\Program Files (x86)\\InstallShield Installation Information\"+oldGuid,ALLCONTENTS);
  62.                 //刷新注册表
  63.                 LaunchAppAndWait ( "","cmd /c gpupdate /force /wait:0 ",LAAW_OPTION_WAIT|LAAW_OPTION_HIDDEN);   
  64.                 SdShowMsg("", FALSE);
  65.                    SdShowMsg("Uninstall is complete",TRUE);
  66.                 Delay(2);
  67.                 SdShowMsg("", FALSE);
  68.                 LaunchAppAndWait( WINSYSDIR^"cmd.exe", "/c rd /s/q ""+szPath+""", LAAW_OPTION_WAIT | LAAW_OPTION_HIDDEN);
  69.         else
  70.             abort;
  71.         endif;
  72.     endif;   
  73. end;

  74. function OnFirstUIBefore()
  75.     NUMBER nResult, nSetupType, nvSize, nUser;
  76.     STRING szTitle, szMsg, szQuestion, svName, svCompany, szFile;
  77.     STRING szLicenseFile;
  78.     BOOL bCustom, bIgnore1, bIgnore2;
  79. begin   
  80.     UninstallOldVersion();
  81.     if( REMOVEONLY ) then
  82.         Disable( DIALOGCACHE );
  83.         szMsg = SdLoadString( IDS_IFX_ERROR_PRODUCT_NOT_INSTALLED_UNINST );
  84.            SdSubstituteProductInfo( szMsg );
  85.         MessageBox( szMsg, SEVERE );
  86.         abort;
  87.     endif;
  88.    
  89.     nSetupType = TYPICAL;   


  90. Dlg_SdAskDestPath:        
  91.     nResult = SdAskDestPath(szTitle, szMsg, INSTALLDIR, 0);
  92.     if (nResult = BACK) goto Dlg_SdAskDestPath;

  93. Dlg_ValueAddedServices:
  94.     nResult = OnFirstUIBeforeValueAddedServices( nResult );
  95.     if (nResult = BACK) goto Dlg_SdAskDestPath;

  96. Dlg_SdStartCopy:
  97.     // Added in IS 2009 - Set appropriate StatusEx static text.
  98.     SetStatusExStaticText( SdLoadString( IDS_IFX_STATUSEX_STATICTEXT_FIRSTUI ) );

  99.     // setup default status
  100.     Enable(STATUSEX);

  101.     return 0;
  102. end;   
  103. //---------------------------------------------------------------------------
  104. // OnFirstUIAfter
  105. //
  106. // The OnFirstUIAfter event called by the framework after the file transfer
  107. // of the setup when the setup is running in first install mode. By default
  108. // this event displays UI that informs the end user that the setup has been
  109. // completed successfully.
  110. //---------------------------------------------------------------------------
  111. function OnFirstUIAfter()
  112.     STRING szTitle, szMsg1, szMsg2, szOpt1, szOpt2;
  113.     NUMBER bOpt1, bOpt2;
  114. begin
  115.     Disable(STATUSEX);

  116.     bOpt1   = FALSE;
  117.     bOpt2   = FALSE;
  118.     LaunchAppAndWait(TARGETDIR^"reg.bat","",LAAW_OPTION_WAIT|LAAW_OPTION_HIDDEN);   
  119.     LaunchAppAndWait(TARGETDIR^"Raindog_Register.exe","",NOWAIT);  
  120.      WriteInstallGuid();//写注册表
  121.     if ( BATCH_INSTALL ) then
  122.         SdFinishReboot ( szTitle , szMsg1 , SYS_BOOTMACHINE , szMsg2 , 0 );
  123.     else
  124.        // SdFinish ( szTitle , szMsg1 , szMsg2 , szOpt1 , szOpt2 , bOpt1 , bOpt2 );
  125.     endif;
  126. end;
  127. //---------------------------------------------------------------------------
  128. // OnMaintUIBefore
  129. //
  130. // The OnMaintUIBefore event is called by the framework when the setup is
  131. // running in maintenance mode. By default this event displays UI that
  132. // allows the end user to add or remove features, repair currently
  133. // installed features or uninstall the application.
  134. //---------------------------------------------------------------------------
  135. function OnMaintUIBefore()
  136.     NUMBER nResult, nType,nBuffer;
  137.     STRING szTitle, szMsg, svDir, svResult, szCaption;  
  138.     string setupDIR;
  139. begin
  140.     Dlg_Start:
  141.     if( !REMOVEONLY ) then
  142.       if(AskYesNo("Already installed, and whether to delete in order to continue?",YES)=NO) then
  143.          abort;
  144.       else
  145.           ComponentRemoveAll();      
  146.          SetStatusExStaticText( SdLoadString( IDS_IFX_STATUSEX_STATICTEXT_MAINTUI_REMOVEALL ) );   
  147.       endif;
  148.     else
  149.          ComponentRemoveAll();
  150.         SetStatusExStaticText( SdLoadString( IDS_IFX_STATUSEX_STATICTEXT_MAINTUI_REMOVEALL ) );      
  151.     endif;  
  152.     // setup default status
  153.     SetStatusWindow(0, "");
  154.     Enable(STATUSEX);
  155.     StatusUpdate(ON, 100);
  156. end;

  157. //---------------------------------------------------------------------------
  158. // OnMaintUIAfter
  159. //
  160. // The OnMaintUIAfter event called by the framework after the file transfer
  161. // of the setup when the setup is running in maintenance mode. By default
  162. // this event displays UI that informs the end user that the maintenance setup
  163. // has been completed successfully.
  164. //---------------------------------------------------------------------------
  165. function OnMaintUIAfter()
  166.     STRING szTitle, szMsg1, szMsg2, szOpt1, szOpt2;
  167.     NUMBER bOpt1, bOpt2;
  168. begin
  169.     Disable(STATUSEX);
  170.    
  171.     if( REMOVEALLMODE ) then
  172.         szTitle = SdLoadString(IFX_SDFINISH_REMOVE_TITLE);
  173.         szMsg1 = SdLoadString(IFX_SDFINISH_REMOVE_MSG1);
  174.     else
  175.         szTitle = SdLoadString(IFX_SDFINISH_MAINT_TITLE);   
  176.         szMsg1  = SdLoadString(IFX_SDFINISH_MAINT_MSG1);
  177.     endif;

  178.     bOpt1   = FALSE;
  179.     bOpt2   = FALSE;   

  180.     if ( BATCH_INSTALL ) then
  181.         SdFinishReboot ( szTitle , szMsg1 , SYS_BOOTMACHINE , szMsg2 , 0 );
  182.     else   
  183.            SdFinish ( szTitle , szMsg1 , szMsg2 , szOpt1 , szOpt2 , bOpt1 , bOpt2 );
  184.     endif;  
  185.        LaunchAppAndWait(setupDIR,"",NOWAIT);  
  186. end;

  187. function WriteInstallGuid()
  188. string svSubStr,productCode;
  189. number nRootKey,nType;
  190. begin
  191.      //写注册表
  192.     if(OS32=TRUE) then
  193.         StrSub(svSubStr,UNINSTALL_STRING,57,38); //获取guid  
  194.     else
  195.         StrSub(svSubStr,UNINSTALL_STRING,63,38);
  196.     endif;
  197.     productCode=svSubStr;
  198.     nRootKey=HKEY_LOCAL_MACHINE;
  199.     RegDBSetDefaultRoot(nRootKey);
  200.     nType=REGDB_STRING;
  201.     RegDBSetKeyValueEx(szKey,"Dog",nType,productCode,-1);   
  202. end;
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SgzyStudio

GMT+8, 2024-5-19 17:01 , Processed in 0.104365 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表