Sunday, December 15, 2013

Shipping Visual Studio redistributable package with NSIS installer

Introduction

The Microsoft Visual C++ Redistributable Package installs runtime components of Visual C++ Libraries required to run applications developed with Visual C++ on a computer that does not have Visual C++ installed.
Its pretty obvious that most clients computers won't have any development software installed, so it is good idea to include appropriate version of redistributable package in your NSIS installer along with your application files.

Implementation

First you need to download appropriate redistributable package from Microsoft website. The package must be same version that Visual Studio you use, including service pack version.
To make things more simple, create variable in your NSIS script  with the path to folder where redistributable is contained, and rename file so you can easily recognize what version is it.
Next, you need to copy redistributable package on the client machine during installation session and run it like this:
Notice that redistributable package is running with ExecWait and not just Exec command, and that /passive and /norestart flags are used. In such a way user have no choice except to wait until redistributable is completely installed.

Improvements

This simple script will work, but there are still some issues that can be fixed.
First one - we need to run our install script as administrator on Windows Vista/7/8 to be sure it will work properly. To do so, we need add this line of code somewhere in script:
RequestExecutionLevel admin
Second (and more complex) problem is to check if user have installed redistributable already. It can be pretty annoying to run redistributable install over and over again although you have installed it before with some other application.
Fortunately for us, we can determine if required redistributable package installed or not very easy. For every package installed there is special key in Windows registry. Registry path for it looks like "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\[ProductCode]", where [ProductCode] is special code used to redistributable. For example, for Visual Studio 2010 codes are:
Visual C++ 2010 Redistributable Package (x86) - {196BB40D-1578-3D01-B289-BEFC77A11A1E}
Visual C++ 2010 Redistributable Package (x64) - {DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}
Visual C++ 2010 Redistributable Package (ia64) - {C1A35166-4301-38E9-BA67-02823AD72A1B}
Visual C++ 2010 SP1 Redistributable Package (x86) - {F0C3E5D1-1ADE-321E-8167-68EF0DE699A5}
Visual C++ 2010 SP1 Redistributable Package (x64) - {1D8E6291-B0D5-35EC-8441-6616F567A0F7}
Visual C++ 2010 SP1 Redistributable Package (ia64) - {88C73C1C-2DE5-3B01-AFB8-B46EF4AB41CD}
For other versions use Google (or just install redistributable on your own computer and find Product code in registry, using DisplayName subkey).
The best way to do this operation in NSIS script is to use some function, like this one: It will try to read Version subkey of product branch (Microsoft Visual Studio 2010 Service Pack 1 in this example) in windows registry, and store result in $R0 NSIS register.

Script code

It's time to gather all these parts together:

4 comments:

  1. Two years later... still, thanks for the post. It's got the info I needed.

    Cheers.

    ReplyDelete
  2. 3 years later also thanks for the post :)

    ReplyDelete
  3. Thank you it has been useful to me too

    ReplyDelete