I previously posted some sample code to detect the version(s) and service pack levels of the .NET Framework that are installed on a computer (here and here). The original version of the sample code that I wrote queries the officially documented registry values that are used to detect the presence of each specific version of the .NET Framework.
Since I posted this sample code, I have heard feedback from some customers who are including the .NET Framework as part of their setup packages. They indicated that sometimes this code reports false positives - in other words, the sample returns true for a specific version of the .NET Framework but it isn't actually installed on the system. I have seen this a couple of times in the past as well, and the root cause was that some of the registry entries used to detect the .NET Framework were orphaned on the system after an uninstall or OS reinstall scenario.
In order to help address this type of issue, I've created a new version of the sample code that adds some new checks to help guard against orphaned registry values. The logic it uses is to query the registry values like the previous sample used to, and then to supplement that with 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 underlying algorithm for this mscoree.dll-based check came from Junfeng Zhang and from a sample published on MSDN.
This algorithm should prove more reliable in detecting whether or not a specific version of the .NET Framework is installed on the system because it does not rely solely on the registry. In addition, it provides the side benefit of performing a quick health check for the .NET Framework itself because it attempts to invoke some APIs in the runtime.
The new sample code contains the following specific changes from the previous version that I published:
- Added the CheckNetfxVersionUsingMscoree and GetProcessorArchitectureFlag functions
- Added logic in WinMain to call CheckNetfxVersionUsingMscoree in addition to the original calls to check .NET Framework registry data
- Updated the code to use strsafe.h functions for string manipulations
You will need the following in order to compile this sample:
- Visual Studio 2005 or Visual C++ 2005 Express Edition
- The .NET Framework 2.0 SDK or at least the header file mscoree.h from that version of the SDK
Please let me know if you have any trouble building or running this sample or incorporating it into your product setup logic.
<update date="5/29/2009"> Fixed broken link to the sample code. </update>