While reading Jeffrey Richters book Applied Microsoft® .NET Framework Programming I've found some interesting hints towards that topic. Towards the topic Loading the Common Language Runtime (p. 41) he mentioned to examine the registry path (and subkeys) HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\policy
to check for installed runtimes. Therefore I've found an interesting KB article towards that topic. Furthermore in the section How the Runtime Resolves Type References (p. 132) he mentioned that mscorlib.dll is tied to the CLR version. So i think it should be possible to check the found registry keys against this file and its version to ensure that the found key is installed CLR version.
In the following you will find my conclusion about that in code. :) I think Clrver does something similar. And I think this solution should be also applicable for a native C++ application since my following code is C# and is just using very basic framework functions.
List<string> installedRuntimes = new List<string>();
Regex rxVersion = new Regex(@"^[v](\d{1,5})([\.](\d{1,5})){0,3}$");
Regex rxVersionPart = new Regex(@"^\d{1,5}$");
try
{
string installPath = Convert.ToString(Registry.GetKey("HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/.NETFramework").GetValue("InstallRoot"));
string[] shortVersions = Registry.GetKey("HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/.NETFramework/Policy", false).GetSubKeyNames();
foreach (string shortVersion in shortVersions)
if (rxVersion.IsMatch(shortVersion))
{
string[] versionExtensions = Registry.GetKey("HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/.NETFramework/Policy/" + shortVersion, false).GetValueNames();
foreach (string versionExtension in versionExtensions)
if (rxVersionPart.IsMatch(versionExtension))
{
string fullVersion = shortVersion + "." + versionExtension;
if (rxVersion.IsMatch(fullVersion))
{
string clrPath = installPath + fullVersion + "\\mscorlib.dll";
if (File.Exists(clrPath) && FileVersionInfo.GetVersionInfo(clrPath).FileVersion.StartsWith(fullVersion.Substring(1))) installedRuntimes.Add(fullVersion);
}
}
}
}
catch { } // May fails while getting a specific registry key, if Microsoft changes the naming rules.
(Don't mess up with Registry.GetKey(...)
- its just a wrapper for the .NET Registry functionality, to easify it towards the way I am used to use the registry.) In the end you should have the CLR version strings inside the list installedRuntimes
as Clrver lists it.