Last active
August 29, 2015 14:04
-
-
Save jcfr/3c7bef3f8b32f9f6ad4b to your computer and use it in GitHub Desktop.
Patch manifests in the redist folder of Visual Studio 9 (2008) replacing "9.0.30729.1" with "9.0.21022.8"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#REQUIRES -Version 2.0 | |
<# | |
.SYNOPSIS | |
Patch manifests in the redist folder of Visual Studio 9 (2008) | |
.DESCRIPTION | |
To avoid the dll hell associated with runtime libraries distribution. There are | |
two approaches: | |
(1) edit the manifest file to have "9.0.21022.8" from the "9.0.30729.XXX" that is there, you "trick" | |
your executable into using the newer dlls. There appears to be no ill effects from this. | |
The executables work, and you are using a newer version of the run time library. | |
(2) add -D_BIND_TO_CURRENT_VCLIBS=1 to ALL files being compiled, then all of your executable files | |
and dll files will refer to "9.0.30729.XXX" and you can use the redist files without any edits. | |
However, you have to be careful here to recompile everything with this. By default the compiler | |
will build for "9.0.21022.8". | |
This script will consider approach (1). If not already done, this script will first backup the | |
manifest files and then patch them. | |
See: | |
* http://www.kitware.com/blog/home/post/4 | |
* http://stackoverflow.com/questions/8097733/how-to-distribute-c-run-time-crt-libraries | |
Contributions are welcome. If you would like to improve this script. Contact the original author(s) | |
providing your patch and a description. After testing your changes, they will be included in the | |
distributed version of the file and you will be acknowledged as a contributor. | |
Thanks | |
.NOTES | |
File Name : fix-vc9-redist-manifests.ps1 | |
Author : Jean-Christophe Fillion-Robin (jchris.fillionr@kitware.com) | |
Prerequisite : PowerShell V2 over Vista and upper. | |
Copyright 2014 - Kitware Inc. | |
License : Apache License, Version 2.0 | |
.LINK | |
Script posted over: | |
https://gist.github.com/jcfr/3c7bef3f8b32f9f6ad4b | |
#> | |
# Default location for Visual Studio 9 (2008) installation | |
$VCINSTALLDIR='C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC' | |
$originalVersion="9.0.21022.8" | |
$updatedVersion="9.0.30729.([0-9]+)" | |
# Check if we have the expected credentials | |
# See http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/11/check-for-admin-credentials-in-a-powershell-script.aspx | |
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(` | |
[Security.Principal.WindowsBuiltInRole] "Administrator")) | |
{ | |
Write-Warning "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!" | |
Break | |
} | |
# TODO This script could be generalized to automatically walk all subdirectories of "$VCINSTALLDIR\redist" and | |
# patch the associated *.manifest files. | |
# Loop over most common architectures | |
foreach ($arch in "x86","amd64") | |
{ | |
Write-Host ("###############################################################################") | |
Write-Host "Patching architecture $arch" | |
Write-Host ("###############################################################################") | |
# Loop over known type of libraries | |
foreach ($library in "ATL", "CRT", "MFC", "MFCLOC", "OPENMP") | |
{ | |
Write-Host ("-------------------------------------------------------------------------------") | |
$src = Join-Path $VCINSTALLDIR ("redist\$arch\Microsoft.VC90.{0}\Microsoft.VC90.{0}.manifest" -f $library) | |
$dest = $src + ".backup" | |
if (Test-Path $src) | |
{ | |
$srcfile = Get-ChildItem $src | |
if (Test-Path $dest) | |
{ | |
$destfile = Get-ChildItem $dest | |
Write-Warning ("Skipping copy of [{0}.] File [{1}] already exists." -f $srcfile.Name, $destfile.Name) | |
} | |
else | |
{ | |
Write-Host ("Copying [{0}] into [{0}.backup]" -f $srcfile.Name, $srcfile.Name) | |
Copy-Item -Path $src -Destination $dest | |
} | |
$content = Get-Content $src | |
# Patch the file only if needed | |
if ($content | Select-String -pattern $originalVersion) | |
{ | |
Write-Warning ("Patching [{0}] - already done" -f $srcfile.Name) | |
} | |
else | |
{ | |
$content = $content -replace $updatedVersion, $originalVersion | |
# Writing updated string to disk | |
$content | Out-File $src -Encoding "UTF8" | |
if (Get-Content $src | Select-String -pattern $originalVersion) | |
{ | |
Write-Host ("Patching [{0}] - done" -f $srcfile.Name) | |
} | |
else | |
{ | |
Write-Error ("Patching [{0}] - FAILED" -f $srcfile.Name) | |
} | |
} | |
} | |
else | |
{ | |
Write-Host ("No manifest associated with [{0}] and [{1}]" -f $arch, $library) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!