How to Install the Visual C++ Redist Using NSIS
Asked Answered
L

3

7

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?

Logistic answered 29/5, 2020 at 18:7 Comment(1)
Thanks Perry, done!Logistic
L
7

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.

Logistic answered 29/5, 2020 at 18:7 Comment(1)
You can't put switches in the file instruction. You must put it in the Exec instruction. You must also quote it correctly, see https://mcmap.net/q/588747/-exec-not-working-in-nsis-installerSignora
L
5

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:

  1. 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).

  2. 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}
    
Labe answered 6/11, 2021 at 19:21 Comment(0)
R
4

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

Ringleader answered 22/4, 2021 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.