簡體   English   中英

如何使用 NSIS 檢查是否安裝了 Visual C++ 2017 可再發行組件 x86

[英]How to check Visual C++ 2017 redistributable x86 is installed or not using NSIS

在安裝軟件之前,我需要檢查是否安裝了Visual C++ 2017 redistributable(x86) 如果未安裝,則在先安裝軟件時,我可以安裝可再發行可執行文件。

當我手動安裝它時,它顯示在以下路徑中:

Computer\HKEY_CLASSES_ROOT\Installer\Dependencies\VC,redist.x86,x86,14.16,bundle\Dependents\{67f67547-9693-4937-aa13-56e296bd40f6}

請幫助我如何使用NSIS檢查上述路徑?

因此,如果在安裝軟件之前不存在可執行文件,我可以使用以下代碼進行安裝:

!insertmacro MUI_LANGUAGE "English"

Section "MyApp"

SetOutPath $INSTDIR
File "\Desktop\Common\vcredist_x86.exe"

ExecShell "" "$INSTDIR\vcredist_x86.exe"
SectionEnd

更簡單的版本,使用“版本”字符串鍵和字符串比較。

Section "CheckVCRedist"
ReadRegStr $0 HKLM "SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x86" "Version"
DetailPrint "Found version $0"
; Check for 14.16.27027 [sic]
${If} $0 >= "v14.16.27024.01"
   DetailPrint "The installed version is usable"
${Else}
   DetailPrint "Must install redist"
${EndIf}
SectionEnd

缺點:如果安裝了“v14.100.xy”,理論上這可能會產生誤報。 但影響只會是您嘗試安裝 14.16 可再發行組件,它什么也不做。

[編輯] 獎勵代碼:如果您構建vswhere ,您可以使用它從您的 Visual C++ 安裝目錄中提取可再發行組件:

Section /o VCRedist VCRedist_id
    ; Use the "pluginsdir"; it's really the NSIS temp dir, and it's cleaned up at the end.
    InitPluginsDir
    SetOutPath "$pluginsdir" 

    ; Finding the correct redistributable is a bit of a problem. MSVC itself ships with an
    ; appropriate redistributable. It's location will likely be similar to
    ; C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Redist\MSVC\14.16.27012
    ; but the exact location of the Redist folder can differ. That's why vswhere.exe is used.
    ; Note that the vswhere used is the local one, NOT the one from
    ; Visual Studio itself (finding that would be a chicken-and-egg problem). This version
    ; is new enough to support the -find parameter, which is what we need to find the
    ; redistributable. Stuff the result in a temporary redist.path file.
    !system 'vswhere.exe -latest -find "VC/Redist/**/vc_redist.x64.exe" > redist.path' 
    !define /file REDISTPATH redist.path
    ; Check what version we found. This is used to decide at install time whether we need to
    ; unpack this redistributable.
    !getdllversion "${REDISTPATH}" RedistVer
    !echo "Including VC++ Redistributable Version ${RedistVer1}.${RedistVer2}.${RedistVer3}.${RedistVer4}"

    SetCompress off
    File "${REDISTPATH}"
    SetCompress auto

    ; Cleanup the temporary redist.path file which held the output of vswhere -find
    !system 'del redist.path'

    ExecWait "vc_redist.x64.exe"
SectionEnd

請注意,該部分是可選的,它是使用之前詳述的測試有條件地啟用的:

Function VCRedistNeeded
    SetRegView 64 
    ReadRegStr $0 HKLM "SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x64" "Version"
    DetailPrint "Found version $0"
    ${If} $0 >= "v${RedistVer1}.${RedistVer2}.${RedistVer3}.${RedistVer4}"
       DetailPrint "VC++ redistributable already present"
    ${Else}
      DetailPrint "Installing VC++ redistributable."
      SectionSetFlags ${VCRedist_id} 1 ; Selected
    ${EndIf}
FunctionEnd  
!include LogicLib.nsh

!macro _RegKeyExistsIn _key _pack _t _f
DetailPrint '${_key} ${_pack}'
!insertmacro _LOGICLIB_TEMP
!ifndef _RegKeyExistsIn_var
    !define _RegKeyExistsIn_var
    Var /Global _RegKeyExistsIn
!endif
StrCpy $_RegKeyExistsIn 0
SetErrors
EnumRegKey $_LOGICLIB_TEMP ${_pack} $_RegKeyExistsIn
    IntOp $_RegKeyExistsIn $_RegKeyExistsIn + 1
    StrCmp $_LOGICLIB_TEMP "" +3
    StrCmp $_LOGICLIB_TEMP "${_key}" "" -3
    ClearErrors
IfErrors `${_f}` `${_t}`
!macroend

Section
${If} "Dependents{67f67547-9693-4937-aa13-56e296bd40f6}" RegKeyExistsIn 'HKCR "Installer\Dependencies\VC,redist.x86,x86,14.16,bundle"'
    DetailPrint "Found the key"
${Else}
    DetailPrint "Key does not exist"
${EndIf}
SectionEnd

但我不知道這是否是檢測可再發行組件的最佳方法,因為您正在檢查特定的次要版本和一些未知的 GUID。

文檔說你應該檢查HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\%vs-version%\\VC\\Runtimes\\{x86|x64|ARM}
舊文件

!include LogicLib.nsh

!ifmacrondef _VerCheck2=>
!macro _VerCheck2_geq_imp l1 l2 r1 r2 _t _f
!insertmacro _LOGICLIB_TEMP
!define _VerCheck2_geq_imp _VerCheck2_geq_${__COUNTER__}
StrCpy $_LOGICLIB_TEMP 0
IntCmpU ${l1} ${r1} ${_VerCheck2_geq_imp}eq "" ${_VerCheck2_geq_imp}end
StrCpy $_LOGICLIB_TEMP 1
Goto ${_VerCheck2_geq_imp}end
${_VerCheck2_geq_imp}eq:
IntCmpU ${l2} ${r2} "" "" ${_VerCheck2_geq_imp}end
StrCpy $_LOGICLIB_TEMP 1
${_VerCheck2_geq_imp}end:
!undef _VerCheck2_geq_imp
!insertmacro _= $_LOGICLIB_TEMP 0 `${_f}` `${_t}`
!macroend
!macro _VerCheck2=> _lhs _rhs _t _f
!insertmacro _VerCheck2_geq_imp ${_lhs} ${_rhs} `${_f}` `${_t}`
!macroend
!endif

Section
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x86" "Installed"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x86" "Major"
ReadRegDWORD $2 HKLM "SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x86" "Minor"
${If} $0 <> 0
    DetailPrint "Found version $1.$2"
    ${If} "$1 $2" VerCheck2=> "14 16"
        DetailPrint "The installed version is usable"
    ${Else}
        DetailPrint "Must install redist"
    ${EndIf}
${Else}
    DetailPrint "Must install redist"
${EndIf}
SectionEnd

同樣正確(許多開發人員推薦)的方法是簡單地靜默安裝運行時。

它不會損害任何組件或軟件,而且更快、更容易。

暫無
暫無

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

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