Sample code to detect .NET Framework install state and service pack level

Introduction

Many installers and applications require that one or more versions of the .NET Framework be installed on the system in order to be able to install and function correctly.  This article provides sample C++ code that can be used in a setup program or in an application's code to detect the install state and service pack level of various versions of the .NET Framework.

Note - when used in a setup program, this sample code is intended to be used in a setup EXE bootstrapper.  It is not intended for use within an MSI.  If your setup program is a single MSI, you can use Windows Installer AppSearch and RegLocator tables to detect the install state.  If you use WiX to create your MSI, you can use the information provided in the WixNetFxExtension instead of implementing this logic yourself.  Refer to How To: Check for .NET Framework Versions in the WiX documentation for more details.

.NET Framework versions that can be detected by the sample code

The sample code available via this article supports detecting the install state and service pack level for the following versions of the .NET Framework:

  • .NET Framework 1.0
  • .NET Framework 1.1
  • .NET Framework 2.0
  • .NET Framework 3.0
  • .NET Framework 3.5
  • .NET Framework 4 (Client and Full)
  • .NET Framework 4.5
  • .NET Framework 4.5.1
  • .NET Framework 4.5.2
  • .NET Framework 4.6
  • .NET Framework 4.6.1
  • .NET Framework 4.6.2
  • .NET Framework 4.7
  • .NET Framework 4.7.1
  • .NET Framework 4.7.2

Registry-based detection code

The simple version of the sample code queries the officially documented registry values that are intended to be used to detect the presence of each version of the .NET Framework.

This sample code can be downloaded from the following locations:

Registry-based detection code and more in-depth checking that loads mscoree.dll

The more thorough version of the sample code queries the officially documented registry values like in the previous sample.  In addition, it performs an additional check that loads mscoree.dll and uses some of its APIs to query for the presence of specific versions of the .NET Framework.  The algorithm to perform the additional check was originally introduced in this blog post.

This sample code can be downloaded from the following locations:

Creating a Visual Studio project to compile the sample code

The sample code available for download via the links above is in the form of a single C++ source (.cpp) file.  If you are having trouble getting this code to compile on your system, you can refer to the instructions in this blog post to create a Visual C++ project that can be used to compile the sample code into a sample executable file.