2016-05-04 40 views
0

你好,我正在重構一箇舊的安裝腳本,並遇到UAC插件創建的問題。由於!insertmacro Init "installer".onInit運行兩次。 !insertmacro Init "uninstaller"un.onInit函數也是如此。NSIS安裝程序.onInit和un.onInit由於UAC運行兩次因爲UAC

因此,安裝程序和卸載程序運行兩次,這不是我想要的行爲。 I have read that the UAC creates an inner process with elevated permissions,這是因爲它接觸C:/驅動器所需的,但外部進程也運行安裝程序。

由於安裝腳本很長,我只能粘貼.onInit函數。整個.nsi腳本可以找到here

註釋掉!insertmacro確保.onInit函數運行一次,但不再運行安裝程序。那麼如何才能使安裝程序和卸載程序只運行一次,並具有正確的(管理員)權限?

我明白任何建議或答案:)

Function .onInit 
MessageBox MB_OK "In .onInit" 
    SetShellVarContext all 

    !insertmacro Init "installer" 

    System::Call 'kernel32::CreateMutexA(i 0, i 0, t "Tribler") i .r1 ?e' 

    Pop $R0 
    StrCmp $R0 0 checkinst 

    MessageBox MB_OK "The installer is already running." 
    Abort 

    checkinst: 
    ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT}" "UninstallString" 
    StrCmp $R0 "" done 
    IfFileExists $R0 showuninstdialog done 

    showuninstdialog: 
    MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION "${PRODUCT} is already installed. $\n$\nClick `OK` to remove the previous version or `Cancel` to cancel this upgrade." /SD IDCANCEL IDOK uninst 
    Abort 

    uninst: 
    ClearErrors 
    ; Laurens (2016-03-29): Retrieve the uninstallString stored in the register. Do NOT use $INSTDIR as this points to the current $INSTDIR var of the INSTALLER, 
    ; which is the default location at this point. 
    ReadRegStr $R0 HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT}" "UninstallString" 
MessageBox MB_OK "$R0" 
    ExecWait '"$R0"' ;Do not copy the uninstaller to a temp file 
    ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT}" "UninstallString" 
    StrCmp $R0 "" done 
    Abort 
    done: 

FunctionEnd 
+0

您在此處粘貼的.onInit代碼與您鏈接的代碼不匹配!你鏈接的代碼有一個明確的問題,不能真正知道在這裏粘貼的代碼沒有測試自己是否有問題,但我不打算這樣做,直到你澄清我應該測試哪些代碼。有關於使用NSIS/UAC插件/ Windows版本的一些信息也將很不錯... – Anders

+0

@Anders我在推動並嘗試幾件事情,而我輸入這個。對不起,如果它現在有所不同。考慮鏈接中我正在運行的當前代碼中的代碼。另外,我將它構建在Windows 2008 64位服務器上,並在Windows 10機器(也是64位)上測試安裝過程。 – Gooey

+0

@Anders我已經撤消了一些我的實驗性修改並推送了它們。我現在不會推動它,因爲當人們看着它時,它確實令人困惑。 – Gooey

回答

2

您鏈接到的代碼(至少當我看着它時)在.onInit中同時調用了!insertmacro UAC_RunElevated!insertmacro Init "installer",所以難怪它跑了好幾次。致電!insertmacro UAC_RunElevated後,您必須始終檢查$0,因爲您可能需要根據其價值調用Quit

我認爲初始化宏是我寫的,所以應該正常工作(?))

我個人建議你犧牲完成頁面上運行復選框,然後你可能不必使用UAC插件...

+0

感謝您的回答安德斯,我插入了UAC_RunElevated作爲測試,看看我做的第一件事是獲得額外的權限,也許我可以添加一個檢查,看看是否有任何參數,我可以檢查作爲@idleberg提到。不幸的是,只有!insertmacro Init「安裝程序」也會使其運行兩次。 – Gooey

+0

我會檢查我是否可以在沒有UAC插件的情況下按照您的建議來做到這一點,非常感謝您的幫助和建議:) – Gooey

+0

我刪除了它,重構了這裏和那裏的文件,它運行得非常出色。謝謝! – Gooey

1

至於我記得,UAC的插件重新啓動的特殊參數的安裝程序。您可以使用.onInitGetParametersGetOptions檢查這一點,那麼顯示一條消息,有條件:

# get all commandline parameters 
${GetParameters} $0 

# parse specific option 
${GetOptions} $0 "/UAC:" $1 

# do stuff 
IfErrors 0 +2 
MessageBox MB_OK "No admint" IDOK +2 
MessageBox MB_OK "Admin" 

就個人而言,我會使用LogicLib最後一部分:

# do stuff 
${If} $1 == "" 
    MessageBox MB_OK "Not admin" 
${Else} 
MessageBox MB_OK "Admin" 
${Endif} 
+0

感謝您的建議。我已經添加了建議的行,並且在提升的權限對話框之後的第二次運行中向我展示了第一次運行時的空對話框和'/ UAC:/NCRC'。所以這聽起來像我想要後者的情況下,有沒有辦法檢查這個? – Gooey

+0

尋找/ UAC:不是正確的選項。 – Anders