Skip to content

Commit a78c49a

Browse files
authored
Merge pull request #1103 from Romanitho/fix-visual-c++
Improve VC++ Redistributable installation logic
2 parents 0a55bb9 + e2ccbc8 commit a78c49a

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

Sources/Winget-AutoUpdate/functions/Install-Prerequisites.ps1

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,55 @@ function Install-Prerequisites {
2121

2222
try {
2323

24-
Write-ToLog "Checking prerequisites..." "Yellow"
25-
26-
# === Check Visual C++ 2015-2022 Redistributable ===
27-
$Visual2022 = "Microsoft Visual C++ 2015-2022 Redistributable*"
28-
$VisualMinVer = "14.40.0.0"
29-
$path = Get-Item HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.GetValue("DisplayName") -like $Visual2022 -and $_.GetValue("DisplayVersion") -gt $VisualMinVer }
30-
31-
if (!($path)) {
32-
try {
33-
Write-ToLog "MS Visual C++ 2015-2022 is not installed" "Red"
34-
35-
# Determine processor architecture for correct installer
36-
if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") {
37-
$OSArch = "arm64"
38-
}
39-
elseif ($env:PROCESSOR_ARCHITECTURE -like "*64*") {
40-
$OSArch = "x64"
24+
Write-ToLog "Checking Microsoft Visual C++ 2015-2022 Redistributable..."
25+
26+
$MinVersion = [version]"14.50.0.0"
27+
$osArch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString().ToLowerInvariant()
28+
29+
$regPath = "HKLM:\SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\$osArch"
30+
$needsInstall = $true
31+
32+
if (Test-Path $regPath) {
33+
$v = Get-ItemProperty $regPath
34+
if ($v.Installed -eq 1 -and
35+
$null -ne $v.Major -and
36+
$null -ne $v.Minor -and
37+
$null -ne $v.Bld -and
38+
$null -ne $v.Rbld) {
39+
try {
40+
$ver = [version]"$($v.Major).$($v.Minor).$($v.Bld).$($v.Rbld)"
41+
if ($ver -ge $MinVersion) {
42+
Write-ToLog "VC++ $osArch already installed ($ver)" "Green"
43+
$needsInstall = $false
44+
}
4145
}
42-
else {
43-
$OSArch = "x86"
46+
catch {
47+
Write-ToLog "VC++ $osArch registry version information is invalid. Forcing reinstall." "Yellow"
4448
}
49+
}
50+
}
51+
52+
if ($needsInstall) {
53+
try {
54+
Write-ToLog "Installing VC++ Redistributable ($osArch)..."
55+
$VCRedistUrl = "https://aka.ms/vs/17/release/VC_redist.$osArch.exe"
56+
$installer = "$env:TEMP\VC_redist.$osArch.exe"
4557

46-
# Download and install VC++ Redistributable
47-
$SourceURL = "https://aka.ms/vs/17/release/VC_redist.$OSArch.exe"
48-
$Installer = "$env:TEMP\VC_redist.$OSArch.exe"
49-
Write-ToLog "-> Downloading $SourceURL..."
50-
Invoke-WebRequest $SourceURL -OutFile $Installer -UseBasicParsing
51-
Write-ToLog "-> Installing VC_redist.$OSArch.exe..."
52-
Start-Process -FilePath $Installer -Args "/quiet /norestart" -Wait
53-
Write-ToLog "-> MS Visual C++ 2015-2022 installed successfully." "Green"
58+
Invoke-WebRequest $VCRedistUrl -OutFile $installer -UseBasicParsing
59+
Start-Process -FilePath $installer -ArgumentList "/quiet /norestart" -Wait
60+
Write-ToLog "VC++ $osArch installed successfully." "Green"
5461
}
5562
catch {
56-
Write-ToLog "-> MS Visual C++ 2015-2022 installation failed." "Red"
63+
Write-ToLog "Failed to install VC++ $osArch" "Red"
64+
throw
5765
}
5866
finally {
59-
Remove-Item $Installer -ErrorAction Ignore
67+
Remove-Item $installer -ErrorAction SilentlyContinue
6068
}
6169
}
6270

71+
72+
6373
# === Check Microsoft.VCLibs.140.00.UWPDesktop ===
6474
if (!(Get-AppxPackage -Name 'Microsoft.VCLibs.140.00.UWPDesktop' -AllUsers)) {
6575
try {

0 commit comments

Comments
 (0)