3
votes

Comment vérifier que Visual C ++ 2017 redistribuable x86 est installé ou n'utilise pas NSIS

Avant d'installer le logiciel, je dois vérifier si le Visual C ++ 2017 redistribuable (x86) est installé ou non. S'il n'est pas installé, lors de l'installation du logiciel, je peux d'abord installer l'exécutable redistribuable.

Lorsque je l'ai installé manuellement, il s'affiche dans le chemin ci-dessous:

!insertmacro MUI_LANGUAGE "English"

Section "MyApp"

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

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

S'il vous plaît aidez-moi à vérifier le chemin ci-dessus en utilisant NSIS?

Pour que si l'exécutable n'est pas présent avant d'installer le logiciel, je puisse l'installer avec le code ci-dessous:

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


0 commentaires

3 Réponses :


1
votes
!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" "MajorVersion"
ReadRegDWORD $2 HKLM "SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x86" "MinorVersion"
${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

2 commentaires

Pour 32 bits, vous devrez vérifier HKLM \ SOFTWARE \ WOW6432Node \ Microsoft \ VisualStudio \% vs-version‌% \ VC \ Runtimes \ x86, car il ne sera pas présent dans le chemin du registre 64 bits. (Curieusement, les paramètres de registre x64 sont présents dans les deux emplacements.)


@RobH: Le code réel que j'ai publié sera lu à partir du registre 32 bits si le programme d'installation est 32 bits.



0
votes

La méthode également correcte (et recommandée par de nombreux développeurs) consiste simplement à installer le Runtime en silence.

Cela n'endommagera aucun composant ou logiciel et c'est beaucoup plus rapide et plus facile.


1 commentaires

L'inconvénient est que vous devez extraire le redistribuable.



2
votes

Version plus simple, utilisant la clé de chaîne "Version" et une comparaison de chaînes.

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  

Inconvénient: cela pourrait théoriquement donner un faux négatif si "v14.100.x.y" est installé. Mais l'impact serait seulement que vous essayez d'installer le redistribuable 14.16, ce qui ne fait rien.

[Edit] Code bonus: si vous créez vswhere , vous pouvez l'utiliser pour extraire le redistribuable de votre répertoire d'installation 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

Notez que la section est facultative, elle est activée conditionnellement en utilisant le test détaillé avant:

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


0 commentaires