簡體   English   中英

我可以讓 NSIS 制作一個單獨的安裝程序來處理本地部署和系統部署嗎?

[英]Can I get NSIS to make a single installer that handles local deployment and system deployment?

Chrome 有一個安裝程序,可用於在系統范圍內安裝,或者如果用戶不是管理員,則安裝到用戶的主目錄。 當您在企業環境中進行部署時,這非常有用,您仍然希望潛在用戶安裝,即使他們不一定具有安裝權限。

NSIS 可以用來創建這樣的安裝程序嗎?

事實證明它可以。 重要的部分是:

  • RequestExecutionLevel highest :這可確保安裝程序獲得用戶帳戶可用的最高權限。 即如果他們在管理員組中,安裝程序將要求權限提升。
  • 確定用戶是否為管理員。 這是使用 UserInfo 插件實現的。 這很簡單。
  • SetShellVarContext all|current :這決定了特殊注冊表根鍵SHCTX的值。 all ,它與HKLM (系統范圍)的含義相同, current它導致HKCU SetShellVarContext還影響$SMPROGRAMS SetShellVarContext的值是指系統范圍的開始菜單還是用戶的層次結構。

這是一個安裝程序的框架,它可以根據用戶帳戶的權限在系統范圍內或本地部署。 它使用 C:\\Windows\\write.exe 作為其負載,並可選擇安裝開始菜單項和桌面快捷方式。 它還在注冊表中放置了對卸載程序的引用,以便它顯示在“添加/刪除程序”對話框中。 我使用 NSIS 3.0(測試版)來構建它,但我沒有看到任何明顯的原因為什么它不能與最近的 2.x 一起使用。

!include "MUI2.nsh"

!define PRODUCT_NAME "DummyProduct"
!define VERSION "0.0.1"

Var INSTDIR_BASE

Name "${PRODUCT_NAME}"
OutFile "${PRODUCT_NAME} Installer.exe"

InstallDir ""

; Take the highest execution level available
; This means that if it's possible to, we become an administrator
RequestExecutionLevel highest

!macro ONINIT un
    Function ${un}.onInit
        ; The value of SetShellVarContext detetmines whether SHCTX is HKLM or HKCU
        ; and whether SMPROGRAMS refers to all users or just the current user
        UserInfo::GetAccountType
        Pop $0
        ${If} $0 == "Admin"
            ; If we're an admin, default to installing to C:\Program Files
            SetShellVarContext all
            StrCpy $INSTDIR_BASE "$PROGRAMFILES64"
        ${Else}
            ; If we're just a user, default to installing to ~\AppData\Local
            SetShellVarContext current
            StrCpy $INSTDIR_BASE "$LOCALAPPDATA"
        ${EndIf}

        ${If} $INSTDIR == ""
            ; This only happens in the installer, because the uninstaller already knows INSTDIR
            ReadRegStr $0 SHCTX "Software\${PRODUCT_NAME}" ""

            ${If} $0 != ""
                ; If we're already installed, use the existing directory
                StrCpy $INSTDIR "$0"
            ${Else}
                StrCpy $INSTDIR "$INSTDIR_BASE\${PRODUCT_NAME}"
            ${Endif}
        ${Endif}
    FunctionEnd
!macroend

; Define the function twice, once for the installer and again for the uninstaller
!insertmacro ONINIT ""
!insertmacro ONINIT "un"

!define MUI_ABORTWARNING

!define MUI_COMPONENTSPAGE_NODESC
!insertmacro MUI_PAGE_COMPONENTS

!insertmacro MUI_PAGE_DIRECTORY

Var STARTMENU_FOLDER
!define MUI_STARTMENUPAGE_REGISTRY_ROOT "SHCTX"
!define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\${PRODUCT_NAME}"
!define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "Start Menu Folder"
!insertmacro MUI_PAGE_STARTMENU ${PRODUCT_NAME} $STARTMENU_FOLDER

!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

!insertmacro MUI_LANGUAGE "English"

Section "-Main Component"
    SetOutPath "$INSTDIR"

    File "C:\Windows\write.exe"

    WriteRegStr SHCTX "Software\${PRODUCT_NAME}" "" $INSTDIR

    ; These registry entries are necessary for the program to show up in the Add/Remove programs dialog
    WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "DisplayName" "${PRODUCT_NAME}"
    WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "UninstallString" '"$INSTDIR\Uninstall.exe"'
    WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "NoModify" 1
    WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "NoRepair" 1

    WriteUninstaller "$INSTDIR\Uninstall.exe"

    !insertmacro MUI_STARTMENU_WRITE_BEGIN ${PRODUCT_NAME}
        CreateDirectory "$SMPROGRAMS\$STARTMENU_FOLDER\"
        CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\${PRODUCT_NAME}.lnk" "$INSTDIR\write.exe"
    !insertmacro MUI_STARTMENU_WRITE_END
SectionEnd

Section "Desktop shortcut"
    CreateShortCut "$DESKTOP\${PRODUCT_NAME}.lnk" "$INSTDIR\write.exe"
SectionEnd

Section "Uninstall"
    Delete "$INSTDIR\write.exe"

    Delete "$INSTDIR\Uninstall.exe"

    RMDir /r "$INSTDIR"

    !insertmacro MUI_STARTMENU_GETFOLDER ${PRODUCT_NAME} $STARTMENU_FOLDER
    Delete "$SMPROGRAMS\$STARTMENU_FOLDER\${PRODUCT_NAME}.lnk"
    RMDir /r "$SMPROGRAMS\$STARTMENU_FOLDER"

    Delete "$DESKTOP\${PRODUCT_NAME}.lnk"

    DeleteRegKey /ifempty SHCTX "Software\${PRODUCT_NAME}"

    DeleteRegKey SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
SectionEnd

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM