Partager via


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.

Comments

  • Anonymous
    November 24, 2009
    For anyone reading this in the future, here is a link to a starting point you can use to convert this sample code to C# - http://geekswithblogs.net/lorint/archive/2006/01/30/67654.aspx.One note of caution here - if you write this detection code in C#, then if the computer you run it on doesn't have any version of the .NET Framework installed on it, the code won't be able to run.  If you know for sure that at least one version of the .NET Framework will always be present on the computers that you run it on, it should probably be OK to write the detection code in C# though.Also, if you need to detect the .NET Framework version or service pack level within an MSI, you should not use this detection code or a custom action.  You can instead use the AppSearch, RegLocator and LaunchCondition tables.  There are built-in detection properties in WiX that you can use for this if you use WiX to create your MSI.  You can find more information about these detection properties at http://wix.sourceforge.net/manual-wix3/wixnetfxextension.htm.

  • Anonymous
    April 25, 2010
    When compiling the "more thorough" version in VS2008Express, I get.detectfx40.cpp(634) : error C2373: 'wWinMain' : redefinition; different type modifiers       C:Program FilesMicrosoft SDKsWindowsv6.0Aincludewinbase.h(2573) : see declaration of 'wWinMain'Any ideas?  Thanks.

  • Anonymous
    April 25, 2010
    Hi BosReg - I'm not sure what would cause that error.  I tested both of the samples that I posted in VS 2008 Professional, but not VC++ 2008 Express, but I don't know of any reason why it wouldn't work there too.  Did you make any changes to the code before trying to compile it?

  • Anonymous
    April 26, 2010
    Hi.  Thanks for the answer.  I did a cut/paste from what is displayed in the browser of both sets of code.  I also "Save As..."'d both and loaded them into different new projects.  I made no changes.  All combinations give the same error.  I looked at the winbase.h refered to in the error, and the wWinMain declaration looks just like the _tWinMain in your code.  This has all the earmarks of being something other than WinMain, but I'm not a cpp programmer, so I don't know tha usual suspects.

  • Anonymous
    April 26, 2010
    Hi BosReg - Just to confirm, are you also using the instructions at http://blogs.msdn.com/astebner/archive/2009/01/31/9387659.aspx when you create the Visual Studio project to build this code?

  • Anonymous
    April 26, 2010
    Yes, those instructions.

  • Anonymous
    April 26, 2010
    Hi BosReg - I'm sorry, but I'm not sure what is causing that compilation error.  I tried again to make sure, and the steps at http://blogs.msdn.com/astebner/archive/2009/01/31/9387659.aspx worked fine for me on multiple computers.  After I created the new VC++ project in Visual Studio and followed the steps to turn off pre-compiled headers, I selected all of the text in the .cpp file that is created with the same name as the project, then copied and pasted all of the code from http://public.blu.livefilestore.com/y1pn7xJa-v5xknXclEy7vYuB-sEU42_6c5giI0xc5bslWTPScZqavABO-WhcXAGuEECzBtnpzuXH8_jir47d2kFkg/detectfx_new.cpp.txt into that .cpp file, saved it, and pressed F5 to build and run it and it worked fine.

  • Anonymous
    May 02, 2010
    hi, i've successfully compiled your "detectfx_new" samplecode with "VC++ 2008 Express Ed SP1". no problems.thx for your work.  

  • Anonymous
    July 13, 2010
    The comment has been removed

  • Anonymous
    July 13, 2010
    Hi Jimmychan - Your best option in this scenario is to use a setup chainer to install the .NET Framework (if it doesn't exist) and then install your application.  You can use the Visual Studio bootstrapper for this type of scenario, you can use other setup chainers that are available, or you could write something yourself to do this.  You can look at msdn.microsoft.com/.../h4k032e1.aspx for some information about using the Visual Studio bootstrapper.

  • Anonymous
    July 14, 2010
    Hi. Thanks for your response. But my purpose is create a portable app with .NETFramework inside the folder. Run smooth without notify user anything. I see your code is best to suit my needs with some modification of course. But the problem is i'm new to c++ and want to launch my app instead launch messagebox. Any ideas? jimmychan: micadyelite at gmail dot com.

  • Anonymous
    July 15, 2010
    Hi Jimmychan - In order to accomplish this, you will need to update the sample code to call CreateProcess or some other Windows API to launch your program if it detects the .NET Framework.  You can find documentation about these APIs on MSDN – for example, msdn.microsoft.com/.../ms682425.aspx.

  • Anonymous
    November 12, 2010
    The comment has been removed

  • Anonymous
    November 12, 2010
    Hi Alex - I haven't had any problems building the sample code on one computer and running it on another computer.  It is written in Visual C++ and it doesn't have any dependencies on the .NET Framework, so it doesn't require the .NET Framework to be on the computer in order to run correctly.  What is the exact error message that you're seeing in this scenario on your computer?

  • Anonymous
    November 12, 2010
    The message I get is"The application has failed to start because the application configuration is incorrect. Reinstalling the application may fix the problem".I believe it has nothing to do with .NET. Something with CRT dll ?

  • Anonymous
    November 12, 2010
    The comment has been removed

  • Anonymous
    November 12, 2010
    Thanks !I did exactly what you told me before reading your post. Just couldn't afford waiting for your reply and searched on the Web :) Too bad the file's size increased almost twice as much. But I can live with that.

  • Anonymous
    November 12, 2010
    Hi Alex - Yeah, file size is one of the trade offs you have to make if you decide to statically link the VC runtime files.  Depending on where this code ends up being used, you may not need to do that - for example, if you're going to run this .NET Framework detection code within an application that already depends on the VC runtime files and that has logic to make sure it is present on the user's computer or something like that.

  • Anonymous
    November 16, 2010
    Hey Aaron - thanks for the great code, it works great! One thing I'm having an issue with (i'm not well versed in C++ so forgive me if this is a stupid question) is that I can only build this in VS2010 if it is set to be a windows app, not a console app. I get the following error when set to console:error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartupAny thoughts?

  • Anonymous
    November 22, 2010
    Hi Jeff - The sample code I have posted is for a Win32 application, not a console application.  If you want to use a console application instead, you'll need to replace the _tWinMain function signature in the sample code with something like the following:int _tmain(int argc, _TCHAR* argv[])

  • Anonymous
    July 06, 2011
    Hi Aaron!Thank you for your awesome work! I created NSIS plugin based on detectfx_new.cpp. Short description is available here: nsis.sourceforge.net/DotNetChecker_plug-in. Source code is on GitHub: github.com/.../NsisDotNetChecker.Hope you do not mind (I found no license for your code).

  • Anonymous
    July 07, 2011
    Hi  Alexey Sitnikov - Thanks for sharing this link.  I don't mind at all that you used this sample code like this.  There is not any license associated with this code - I created it for sample purposes only, and anyone is free to re-use it as they see fit.

  • Anonymous
    January 18, 2012
    The links are broken... can you please fix them?

  • Anonymous
    January 18, 2012
    Hi SkyDriver - The links appear to be working fine for me.  You might need to go to http://skydrive.live.com and log in before they'll work for you, so I'd suggest trying that.

  • Anonymous
    June 21, 2014
    I cannot install .net framework v4everytime it rolls back after a specific time and the installation says that it is insuccessfull due to fatal error.i have tried by enabiling the .net framework 3.5 options at Turn windows feature on or off..i am running a windows 7 32 bit..plz help..

  • Anonymous
    June 21, 2014
    Hi Soham - My first suggestion is to try installing the latest version of the .NET Framework 4 family, which is the .NET Framework 4.5.2.  You can download it from www.microsoft.com/.../details.aspx.If that doesn't install either, then can you please use the tool described at blogs.msdn.com/.../6458047.aspx to collect all of your setup log files, upload the file named %temp%vslogs.cab that this tool will create to a file server (such as http://onedrive.live.com), and then reply back here and provide a link that I can use to download your log files and take a closer look?

  • Anonymous
    September 29, 2014
    On my windows 7x64 OS, 4.5.2 shows version 378758, not 378675 as your code comment says is required by Win8.1.Cheers

  • Anonymous
    September 30, 2014
    The comment has been removed

  • Anonymous
    February 15, 2015
    Hi,I have a requirement to check if the .NET framework of the system is compatible with the .NET Framework of the Application Developed. Any solution??Thanks

  • Anonymous
    February 16, 2015
    Hi Niranjana - There isn't a great way to do this.  I did some web searches and it looks like the best ways to determine what version of the .NET Framework that a given app is built against is to use ildasm.exe or Reflector.  There is more information about these options at geekswithblogs.net/.../determine-.net-framework-version-of-an-application.aspx and stackoverflow.com/.../how-do-i-check-the-net-version-of-an-app.

  • Anonymous
    April 26, 2015
    Hello Stebner ,I want to do after installing .Net framework restart, and then continue the installation process, how to do it? Thanks.

  • Anonymous
    April 27, 2015
    Hi Veary - Are you creating an installer that will chain the .NET Framework installer and then install your application?  If so, then it is typically safe to suppress the reboot prompt at the end of .NET Framework setup, proceed with the installation of your application, and then prompt the user to reboot at the end of the overall installation process.

  • Anonymous
    April 27, 2015
    Thanks a lot for you answer, Stebner,But I am using .netframework based softwares in my installation program, and I have to restart or the computer will report error.

  • Anonymous
    April 28, 2015
    Hi Veary - In that case, then your setup chainer will need to register itself to restart after the computer reboots.  Typically, that is done by using one of the Windows Run or RunOnce registry keys.  You can find more information about those registry keys at msdn.microsoft.com/.../aa376977(v=vs.85).aspx.

  • Anonymous
    April 29, 2015
    Thanks you very much,My friend.

  • Anonymous
    May 03, 2015
    Thank you very muth, Stebner.

  • Anonymous
    June 01, 2015
    Hi Aaron I'm have windows 8.1 pro portuguese installed. When i try to run a program called "OnMOFA" says: " This application requires one of the following version of the .Net framework: .net framework version =v4.5. do you want to install this .net framework versin now? Please help. Best regards.

  • Anonymous
    June 02, 2015
    Hi Mady Mussa - Windows 8.1 includes the .NET Framework 4.5 as a part of the operating system, and there is not a way to uninstall it.  In my past experience, when an application shows this type of error, it is typically caused by a .NET Framework detection logic problem in the application, not by a problem with the .NET Framework itself.  I recommend searching on the web site for the application's manufacturer to see if they have any known issues or patches for this issue.

  • Anonymous
    August 23, 2015
    Your code is perfect, it runs without any error. Thanks for sharing and saving our time :)

  • Anonymous
    May 09, 2017
    I have created an executable that can be used to run a program if .NET is installed or shows an error if not. You can find the code here: https://github.com/AButler/dot-net-bootstrapperFeel free to submit pull requests for new features

  • Anonymous
    May 30, 2017
    Most checks are based on access to HKLM which is inaccessible by regular users without administrative privileges.This is problematic when an app is run by normal users but one does want to gather statistics or urge them to update to newer frameworks.Does anyone have a solution to this problem?

    • Anonymous
      May 31, 2017
      Hi GieltjE - The sample code that I posted only requires read access to HKLM, and it should work fine without administrative privileges.
      • Anonymous
        June 02, 2017
        Sorry, must have mistyped something in my previous attempts, reading works just fine.
        • Anonymous
          June 22, 2017
          I have using windows server 2012R2 but i m facing the error message "Cannot start WINC Agent on local host because it requires .NET library version 4.5 to run, and it is not available". and showing another error message "Smart connector can`t detect .Net framework 4.5"Please provide the resolution.
          • Anonymous
            June 22, 2017
            The comment has been removed