NSIS检测已安装并卸载旧版本和静默安装.msu文件

来源:互联网 发布:mac重命名快捷键 编辑:程序博客网 时间:2024/05/29 09:48
NSIS检测已安装并卸载旧版本:

Function .onInit
   !insertmacro TIP_WHEN_AMD64_INSTALLER_RUNAT_X86
   ;安装到所有用户下,current 表示安装到当前用户下, all 表示所有用户下。
   SetShellVarContext all
  
   ClearErrors

   SetRegView 64

   ReadRegStr $UNINSTALL_PROG ${PRODUCT_UNINST_ROOT_KEY} ${PRODUCT_UNINST_KEY} "UninstallString"
  
   IfErrors  done

   ReadRegStr $OLD_VER ${PRODUCT_UNINST_ROOT_KEY} ${PRODUCT_UNINST_KEY} "DisplayVersion"
   SetRegView lastused
   MessageBox MB_YESNOCANCEL|MB_ICONQUESTION \
     "检测到本机已经安装了 ${PRODUCT_NAME} $OLD_VER。\
     $\n$\n是否先卸载已安装的版本?" \
       /SD IDYES \
       IDYES uninstall \
       IDNO done
   Abort

  uninstall:
 
   StrCpy $OLD_PATH $UNINSTALL_PROG -11

   ExecWait '"$UNINSTALL_PROG" /S _?=$OLD_PATH' $0
   DetailPrint "uninst.exe returned $0"
   Delete "$UNINSTALL_PROG"
   RMDir $OLD_PATH

 done:
 FunctionEnd

其中:

  1. StrCpy $OLD_PATH $UNINSTALL_PROG -11  相当于获取安装路径:相当于下面两行代码 ("\uninst.exe"一共占11个字符 )

  ${GetParent} $UNINSTALL_PROG $R1
  strcpy $OLD_PATH $R1


  2. ExecWait '"$UNINSTALL_PROG" /S _?=$OLD_PATH' $0  是执行相应的uninst.exe卸载文件。/S 是静默卸载。_?= 作用是将uninst.exe拷贝到临时文件夹( %temp%)之后,再在(_?=后面的)$OLD_PATH文件夹执行的。


可以参考Anders说的:


      Running the uninstaller with _?= is never going to delete the uninstaller .exe.
      When running the uninstaller without _?= what happens is:

  1. uninst.exe is copied to %temp%\~un.exe
  2. uninst.exe does Exec %temp%\~un.exe _?=instdir and quits
  3. %temp%\~un.exe performs uninstall including deleting uninst.exe in $instdir


静默安装.msu文件

 ExecWait 'wusa.exe "$INSTDIR\CamDrive\Windows6.1-KB2921916-x64.msu" /quiet /norestart' $0
 DetailPrint "Windows6.1-KB2921916-x64.msu安装返回了 $0"


参考文章:http://blog.csdn.net/crazycoder8848/article/details/13703489

                  https://nsis-dev.github.io/NSIS-Forums/html/t-362421.html