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

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 (39)

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

  2. BosReg says:

    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.

  3. 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?

  4. BosReg says:

    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.

  5. 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?

  6. BosReg says:

    Yes, those instructions.

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

  8. theodore06 says:

    hi, i’ve successfully compiled your "detectfx_new" samplecode with "VC++ 2008 Express Ed SP1". no problems.

    thx for your work.  

  9. jimmychan says:

    hi, I'm interest to install dotnetframework silently when dotnetframework not exist, then launch my c# app. If dotnetframework exist just launch my c# app immediately instead launch a message box. How to modify your code since i'm totally new to c++? Thanks astebner.

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

  11. jimmychan says:

    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.

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

  13. Alex says:

    Hi Aaron!

    Your code works great. But the problem is it only works on the computer it was compiled on. That has little value for me since I want to run this program on a target computer as part of the application setup process. I tested the program on Virtual PC running Windows XP SP2 just recently installed ( no .NET Framework set up yet ), and it doesn't execute. Instead it gives an error message. I have a feeling it still depends on the availability of .NET on the target machine in order to execute properly. What would you recommend ? Is there a way for me to build your code in Visual Studio so that the resulting program contains no dependecies on .NET Framework or anything else that causes this program to fail on old Windows OS ( such as XP, for example )? Thanks !

  14. 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?

  15. Alex says:

    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 ?

  16. Hi Alex – This error is caused by the Visual C++ runtime files not being available on the target computer.  I took a look at my steps at blogs.msdn.com/…/9387659.aspx and there is a step that I use when I build the code myself that I forgot to list there.  I use the Visual C++ /MT compiler option to statically link to the VC++ runtime files to avoid this type of problem.  This is described more at msdn.microsoft.com/…/2kzt1wy3.aspx.

    You can add this switch in the Visual Studio IDE by right-clicking on your project in the VS solution explorer, choosing Properties, then going to Configuration Properties | C/C++ | Command Line and putting /MT in the additional options text box.

  17. Alex says:

    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.

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

  19. Jeff says:

    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 ___tmainCRTStartup

    Any thoughts?

  20. 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[])

  21. Alexey Sitnikov says:

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

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

  23. SkyDiver says:

    The links are broken… can you please fix them?

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

  25. Soham says:

    I cannot install .net framework v4

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

  26. 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 http://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?

  27. Dougal says:

    On my windows 7×64 OS, 4.5.2 shows version 378758, not 378675 as your code comment says is required by Win8.1.

    Cheers

  28. Hi Dougal – I'm assuming you meant 4.5.1, not 4.5.2 in your comment – correct?  If so, the version information you're seeing is what is expected here.  Windows 8.1 comes with the .NET Framework 4.5.1 installed as an OS component, and the version number for the OS component version of 4.5.1 is different than the version number for the redistributable version of 4.5.1 (which is what you have installed on your Windows 7 PC).

  29. Niranjana says:

    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

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

  31. Veary says:

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

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

  33. Veary says:

    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.

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

  35. Veary says:

    Thanks you very much,My friend.

  36. Veary says:

    Thank you very muth, Stebner.

  37. Mady Mussa says:

    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.

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

  39. Muhammad Jahanzaib says:

    Your code is perfect, it runs without any error. Thanks for sharing and saving our time 🙂

Skip to main content