How to Install the Visual C++ Redist Using NSIS - visual-studio

My C++ application needs to install the Visual Studio C++ redistributable. I'm using Visual Studio 2019 Community edition. I use NSIS (version 3.04) to create my install program. Should I try to detect if the redist is installed and only install it if it is not up to date?

There are a melange of answers about how to do this, including many methods of how to detect if the redist is installed. I'm not going to say that all of them are incomplete and don't work in a future proof method, but I have not had success with them. So, I think the best thing to do is just install the redist always and let Microsoft take care of it. As of March 2020 this will add 14MB to your installer program, but maybe in this age of high speed Internet that isn't the big deal it once was. Luckily this is quite simple, and hopefully this question will keep you from following all the dated references and links that I did.
Instructions on the redist from Microsoft can be found here: Redistributing Visual C++ Files
To turn this into NSIS:
Find the file you want to redistribute in your Visual Studio install. For me this is:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Redist\MSVC\14.25.28508\vcredist_x86.exe
The version number is definitely subject to change (14.25.28508) in the future, likely just when you install updates to Visual Studio, so remember that you'll need to update that path when your install program breaks. You'll also need to choose between vcredist_x86.exe and vcredist_x64.exe depending on whether your build your application as 32-bit or 64-bit.
Add a section like this to your NSIS install file, probably before you do the main installation. All it does is copy the redist file into the filesystem, run it, wait for completion, then delete the redist file.
Section "Visual Studio Runtime"
SetOutPath "$INSTDIR"
File "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Redist\MSVC\14.25.28508\vcredist_x86.exe"
ExecWait "$INSTDIR\vcredist_x86.exe"
Delete "$INSTDIR\vcredist_x86.exe"
SectionEnd
Substitute the proper path for the redist file you want to use.
As written (and current redist program behavior), this will pop up a dialog that the user will have to follow through to install the redist. You can substitute quiet mode:
ExecWait '"$INSTDIR\vcredist_x86.exe" /quiet'
However I did not have good results with that. YMMV.

to get the additional switches to work you include them within the quotes.
so the above ExecWait command would be the following:
ExecWait "$INSTDIR\vcredist_x86.exe /install /quiet"
I am using this currently, however with the "/quiet" switch the actual NSI installer progress hangs until redist is installed.
Using "/passive" instead will give a progress report on the redist installation

I agree with gdunbar that detection methods are both complex and not so reliable, good summary of such methods is in this thread: Detect if Visual C++ Redistributable for Visual Studio 2012 is installed
However I didn't want to always include the VC redist in the installer, as it significantly increase installer size (e.g. VC++ 2019 redistributable is 24Mb, while my installer without it was 1Mb). So I ended up using what I think is quite reliable method of trying to run the executable, and downloading installer on the fly if needed. Below the details for VC++ 2019 redistributable:
Create console windows app, e.g. Visual Studio 2019 wizard create this code:
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
return 0;
}
The size of this app is 12K (compiled with "Favor small size" option).
Using the following code in NSIS to run it and download the installer if needed:
InitPluginsDir
;the hello word console app
;need to use nsExec plugin to prevent error popup (if redist is missing)
File /oname=$PLUGINSDIR\checkRedist.exe checkRedist.exe
nsExec::Exec '"$PLUGINSDIR\checkRedist.exe"'
Pop $0
${If} $0 != 0
DetailPrint "VC++ Redistributable package is missing!"
inetc::get "https://aka.ms/vs/16/release/vc_redist.x64.exe" $PLUGINSDIR\vcredist.exe
DetailPrint "Installing Visual Studio Redistributable package..."
ExecWait '"$PLUGINSDIR\vcredist.exe" /q /norestart'
DetailPrint "Done"
${Else}
DetailPrint "VC++ Redistributable already installed"
${EndIf}

Related

Visual Studio Exe App compilation path change

I just want to change the exe file Visual Studio compilation path change
I am doing it like this now. i created a bat file that copied file. I have added visual studio build events. I wonder if there is an easier way.
meanwhile the exe file is being copied to the network drive
I had this problem in a different context (Elixir/Phoenix, Rust), but the root cause was the same: cl.exe could not be found during compilation.
My setup was:
Windows 10, x64
Visual Studio Community 2017 already installed, but only for C# development
For some reason the solution with installing the Visual C++ Build Tools (as #cozzamara suggested) did not work. Stops during installation with some obscure error message. Guess it did not liked my existing Visual Studio installation.
This is how I solved it:
Start up the Visual Studio Installer
Check the Desktop development with C++ (screenshots here)
Execute following command before compiling:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat
From this on the command cl.exe works. Alternatively (and more conveniently for development) start the application 'Developer Command Prompt for VS 2017' or 'x64 Native Tools Command Prompt VS 2017'.
Share
I solved the problem by writing code like this in the Post build field, I just ensured that the exe was copied to the field I wanted
COPY $(TargetPath) "\x.x.x.x\ortak\yakup\TestApp.exe"
pause
$(TargetPath) = It gives the location where the exe exited, along with the exe name
"C:\yakup\project\TestApp.exe" like

VCRUNTIME140.dll is missing? [duplicate]

This question already has answers here:
PHP 7: Missing VCRUNTIME140.dll
(9 answers)
Closed 1 year ago.
I use windows 7 x64 and i need vcruntime140.dll in my program. But it's missing.
I reinstall Visual C++ Redistributable for Visual Studio 2015 x86 and x64 and restart pc but stil dll missing.(Also installed all version of Visual C++ Redistributable for Visual Studio xxxx)
I thought, it's because of windows update but finally i looked at system32 folder and vcruntime140.dll is in there.
How can i insert that dll in system path and is there a solution? Or how can i fix this problem?
Try repairing this distribution on uninstall or modify programs.
Sometimes x64 dll's are put in system32 folder wich is why this error occur, you can also use dependency walker to check that.
install Visual C++ Redistributable for Visual Studio 2015 or repair &
just go to SysWOW64 & sys32 if find vcruntime140.dll jus copy & past & rename a copy to VCRUNTIME140.dll "yes Big letter"

How to install SignTool.exe for Windows 10

How to install SignTool.exe in Visual Studio 2015 for Windown 10?
I tried to build my project but the program threw an error :
Error An error occurred while signing: SignTool.exe not found.
You need to install the Windows 10 SDK.
Visual Studio 2015 Update 1 contains it already, but it is not installed by default. You should go to Control Panel -> Programs and Features, find Microsoft Visual Studio 2015 and select "Change".
Visual Studio 2015 setup will start. Select "Modify".
In Visual Studio components list find "Universal Windows App Development Tools", open the list of sub-items and select "Windows 10 SDK (10.0.10240)".
Windows 10 SDK in VS 2015 Update 1 Setup
Of cause you can install Windows 10 SDK directly from Microsoft: https://go.microsoft.com/fwlink/?LinkID=698771
As josant already wrote - when the installation finishes you will find the SignTool.exe in the folders:
x86 -> c:\Program Files (x86)\Windows Kits\10\bin\x86
x64 -> c:\Program Files (x86)\Windows Kits\10\bin\x64\
If you only want SignTool and really want to minimize the install, here is a way that I just reverse-engineered my way to:
Download the .iso file from https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk (current download link is http://go.microsoft.com/fwlink/p/?LinkID=2022797) The .exe download will not work, since it's an online installer that pulls down its dependencies at runtime.
Unpack the .iso with a tool such as 7-zip.
Install the Installers/Windows SDK Signing Tools-x86_en-us.msi file - it's only 388 KiB large. For reference, it pulls in its files from the following .cab files, so these are also needed for a standalone install:
4c3ef4b2b1dc72149f979f4243d2accf.cab (339 KiB)
685f3d4691f444bc382762d603a99afc.cab (1002 KiB)
e5c4b31ff9997ac5603f4f28cd7df602.cab (389 KiB)
e98fa5eb5fee6ce17a7a69d585870b7c.cab (1.2 MiB)
There we go - you will now have the signtool.exe file and companions in C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x64 (replace x64 with x86, arm or arm64 if you need it for another CPU architecture.)
It is also possible to commit signtool.exe and the other files from this folder into your version control repository if want to use it in e.g. CI scenarios. I have tried it and it seems to work fine.
(All files are probably not necessary since there are also some other .exe tools in this folder that might be responsible for these dependencies, but I am not sure which ones could be removed to make the set of files even smaller. Someone else is free to investigate further in this area. :) I tried to just copy signtool.* and that didn't work, so at least some of the other files are needed.)
Here's where you'll find it:
C:\Program Files (x86)\Windows Kits\10\App Certification Kit
Screenshot:
Best solution end of 2020:
Just download Windows 10 SDK from Microsoft here:
https://go.microsoft.com/fwlink/?LinkID=698771
In setup, choose only Windows App Certification App (it's only 184 MB)
You can find signtool.exe here:
%PROGRAMFILES(X86)%\Windows Kits\10\bin\x64
Cheers!
As per the comments in the question... On Windows 10 Signtool.exe and other SDK tools have been moved into "%programfiles(x86)%\Windows Kits\".
Typical path to signtool on Windows 10.
32 bit = "c:\Program Files (x86)\Windows Kits\10\bin\x86\signtool.exe"
64 bit = "c:\Program Files (x86)\Windows Kits\10\bin\x64\signtool.exe"
Tools for SDK 8.0 and 8.1 also reside in the "Windows Kits" folder.
Another answer from 2021.
You might not need Windows SDK at all. If you have VS-2019 installed, you might already have signtool in C:\Program Files (x86)\Microsoft SDKs\ClickOnce\SignTool\signtool.exe
NOTE: The good thing about this particular signtool version (compared to the Windows SDK one), is that it's self-contained, and does not need all the dll's next to it (mssign32.dll, wintrust.dll etc, which usually lie around in Windows SDK folders).
You can even add this file to your source code repo (just one file), since this tool hasn't changed since 2016.
P.S. I had this signtool even without "ClickOnce publishing" component installed in my Visual Studio Community Edition.
Location:
C:\Program Files (x86)\Windows Kits\10\App Certification Kit\signtool.exe
In 2019, this is a quite recent link from Microsoft about how to obtain this tool:
The SignTool tool is a command-line tool that digitally signs files,
verifies signatures in files, or time stamps files. For information
about why signing files is important, see Introduction to Code
Signing. The tool is installed in the \Bin folder of the Microsoft
Windows Software Development Kit (SDK) installation path.
SignTool is available as part of the Windows SDK, which you can
download from https://go.microsoft.com/fwlink/p/?linkid=84091.
I only needed signtool, so I chose the minimal I came up with and signtool.exe is now in C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\signtool.exe
Microsoft article link:
https://learn.microsoft.com/en-us/windows/win32/seccrypto/signtool
to install just the signingtools from the winsdksetup.exe (available at the same url as the windows sdk iso mentioned above) this is an option to, straight from the Dockerfile i'm working in:
RUN powershell Start-Process winsdksetup.exe -ArgumentList '/features OptionId.SigningTools', '/q', '/ceip off', '/norestart', -NoNewWindow -Wait
so if you're in windows then that'd be:
winsdksetup.exe /features OptionId.SigningTools
winsdksetup /h gives you the options, so i won't summarise them here.
I include the dockerfile snippet, as that is what i started my day looking for the solution for.
You don't have to install the Signtool. It might already be there.
Go to C:\Program Files (x86)\ and search for signtool.exe. In my system I found it under C:\Program Files (x86)\Microsoft SDKs\ClickOnce\SignTool
No matter which version of Windows you are using, the above signtool will solve your purpose.
If you're using VS Express 2015, just go to your control panel --> programs and features --> select vs 2015 --> click change, then in the VS Express installer select 'Modify' --> select Publishing tools, and finish. Once setup completes the changes you will be able to create your installer.
You should go to Control Panel -> Programs and Features, find Microsoft Visual Studio 2015 and select "Change".
Visual Studio 2015 setup will start. Select "Modify".
In Visual Studio components list, open the list of sub-items and select "ClickOnce Publication Tools" and "Windows 10 SDK" too.
I did a modify with the Visual Studio from Control Panel, Programs and Features. The SDK was not at first apparent so I installed the Common Tools which lo and behold did include the SDK Update 3.
It's available many, many places, depending upon what is installed:
On my box, every one except the v6.0A SDK version supports the /fd option.
SignTool is available as part of the Windows SDK (which comes with Visual Studio Community 2015). Make sure to select the "ClickOnce Publishing Tools" from the feature list during the installation of Visual Studio 2015 to get the SignTool.
Once Visual Studio is installed you can run the signtool command from the Visual Studio Command Prompt.
By default (on Windows 10) the SignTool will be installed in:
C:\Program Files (x86)\Windows Kits\10\bin\x86\signtool.exe
For me in 2021 the signtool.exe was here: "C:\Program Files (x86)\Windows Kits\10\bin\x64" or in: x86
and not under: C:\Program Files (x86)\Windows Kits\10\App Certification Kit
even if I have this folder and may files in it.
I ran into an issue using this tool in a restrictive Azure DevOps Pipelines environment, where I couldn't even expand PATH or call any tools from an absolute path outside of the ADO build directories, meaning in this case anything from C:\Program Files (x86)\
My solution was to package it as a Secure File in ADO Pipelines' Library -> Secure Files section, allow my pipeline to access the file via its security settings, then download it as a build task and run it via a powershell task.
In the example below, I just query the help with the /? command. Just replace that with whatever command you want to use.
- task: DownloadSecureFile#1
displayName: "Signtool Download"
name: MSSignTool
inputs:
secureFile: 'signtool.exe'
- task : PowerShell#2
displayName: "Run Signtool"
inputs:
targetType: "inline"
script: $(MSSignTool.secureFilePath) /?
Warning 1: I'm not sure what dependencies are required for signtool.exe to work standalone, or if it even has any. The Windows 10 SDK and ADK was already installed in this build environment. If querying the help causes the tool to fail or crash, make sure one or both of those are installed. Hopefully you will have access to something like choco to install any missing dependencies.
Warning 2: Be careful if copying those code segments. ADO is pretty strict with dynamic whitespace, so if your spacing is off it will brick your whole pipelines file until you adjust the spacing to its correct position.

How to install redistributable with visual studio setup?

I wish to make my installer (visual studio setup) to install redistributable (Visual C++ 2013 redistributable x86) in case it isn't installed on the PC or install the necessary dll for my program. I don't wish to set a launch condition.
If possible, I wish that the installation of the redistributable to be silent.
Any suggestion ?
That's what the Prerequisites button is for in the setup project's Properties. You'll need to set a configuration (such as Release) before you see that button. That's where you add the VC++ runtimes. That will generate a setup.exe that users run - it will install any of those prerequisites and then install your MSI file.
To make it silent you'd need to get into the manifest file that describes the command used to install the runtime, and change it to a silent command. There used to be a tool called the Bootstrap Manifest Generator that would do that kind of thing, if you can still find it.
There's no support I know of for any of the following, but this is how the VS bootstrapper works, so mangle at your own risk :)
You could open the built setup.exe as a file with Visual Studio and examine the resources - under 41 there's a setupcfg that's the specicification for the prereqs. You'd need to export it, alter it and re-import it.
Alternatively, the template for the standard prereqs that this uses comes from the SDK in architecture-dependent locations such as Program Files (x86)\Microsoft SDKs\Windows\v8.1A\Bootstrapper\Packages\vcredist_x86\product.xml so if you go in that Xml file and find the correct VCRedistInstalled settings and command lines, make it silent, and it should propagate into the setup.exe when you do the build. This is unsafe because a) you've altered a file so that will now not be updated by any SDK updates b) The file doesn't match the one installed by the SDK and there may be installer repair issues and c) Every bootstrapper build will be affected.
You can use Merge modules and add it to your setup/msi which will install quietly

VDPROJ auto upgrading vs. uninstall/reinstall

I've seen a confusing behavior regarding the MSI files generated by a VDPROJ file. If I build my MSI in Visual Studio and then right-click and pick "Install" from within Visual Studio, it will automagically uninstall any version that is already installed and then install the new MSI.
However, if take the generated MSI and run it directly it will complain if a previous version is already installed. I have to uninstall it explicitly (in Add/Remove Programs) first.
What's the deal? Is there a command-line argument that Visual Studio executes the MSI with?
Yes Visual Stuido will be passing the REINSTALLMODE and the REINSTALL properties to the windows installer when it runs your install
something like:
msiexec /i your.msi REINSTALLMODE=vomus REINSTALL=ALL
Check the MSDN documents linked above to see what these options are doing
Edit:
Now I come to think of it. Studio may also just be uninstalling your application first by using the /x command line arg
msiexec /x <package> or <product code>
Maybe someone else can confirm which is being used?

Resources