1. 程式人生 > >NSIS打包的軟體開啟時已安裝檢測機制

NSIS打包的軟體開啟時已安裝檢測機制

#-- 根據 NSIS 指令碼編輯規則,所有 Function 區段必須放置在 Section 區段之後編寫,以避免安裝程式出現未可預知的問題。--#

Function .onInit

        ;檢查重複執行
  System::Call 'kernel32::CreateMutexA(i 0, i 0, t "MicroSetup") i .r1 ?e'

  Pop $R1

  ;;安裝程式已經執行

  StrCmp $R1 0 +3
  ;MessageBox MB_OK|MB_ICONINFORMATION|MB_TOPMOST "程式已經在執行。"
  Abort
Push $0
Push $1
  ReadRegStr $0 HKLM ${PRODUCT_UNINST_KEY} "UninstallString"
  ReadRegStr $1 HKLM ${PRODUCT_UNINST_KEY} "DisplayVersion"
  StrCmp $0 "" onInit.End

  IfFileExists $0 0 onInit.End

  ;;殺死安裝包啟動的程式;;

  KillProcDLL::KillProc "${PRODUCT_NAME}.exe"   MessageBox MB_YESNO|MB_ICONQUESTION "檢測到系統已經安裝了${PRODUCT_NAME} $1。$\r$\n繼續之前必須先執行解除安裝,繼續嗎?" IDNO onInit.GoAbort

  # 靜默解除安裝
  IfFileExists "$DESKTOP\${PRODUCT_NAME}.lnk" 0 +2
        Delete "$DESKTOP\${PRODUCT_NAME}.lnk"
        IfFileExists "$SMPROGRAMS\${PRODUCT_NAME} $1\${PRODUCT_NAME}.lnk" 0 +4
          Delete "$SMPROGRAMS\${PRODUCT_NAME} $1\${PRODUCT_NAME}.lnk"
          Delete "$SMPROGRAMS\${PRODUCT_NAME} $1\Uninstall.lnk"
          RMDir "$SMPROGRAMS\${PRODUCT_NAME} $1"
        IfFileExists "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk" 0 +4
                Delete "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk"
          Delete "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk"

          RMDir "$SMPROGRAMS\${PRODUCT_NAME}"

  ;;刪除資料夾和檔案;;

  RMDir /r "$INSTDIR\platforms"
  RMDir /r "$INSTDIR\imageformats"
  RMDir /r "$INSTDIR\iconengines"
  RMDir /r "$INSTDIR\bearer"
  RMDir /r "$INSTDIR\*.*"
  RMDir /r "$INSTDIR\..\${PRODUCT_NAME}"
  RMDir /r "$INSTDIR"

  MessageBox MB_OK "解除安裝完成!"
  Goto Next
onInit.GoAbort:
    Abort
onInit.End:
  Pop $1
  Pop $0
Next:
ExecShell "" "${PRODUCT_NAME}_${PRODUCT_VERSION}.exe"
FunctionEnd