visual-studiovisual-c++installationnsisvcredist

How to Install the Visual C++ Redist Using NSIS


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?


Solution

  • 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.