Chapter 45. Testing for the Availability of the .Net Framework

 

The only way to learn a new programming language is by writing programs in it.

 
 --Dennis Ritchie

Managed applications on the .NET platform have access to a wealth of features and prebuilt functionality, dramatically decreasing development time. Deploying .NET applications is also extremely simple, provided the required runtime is present. Managed applications have a dependency on the Common Language Runtime and the class framework assemblies, and will not function without them. Deploying these applications generally entails that you check to see if the operating system has the .NET runtime and, if not, install it. Some installation packagers have bootstrapper utilities that can automate this process for you, but generally you are on your own.

One solution is to take the manual approach by attempting to run the application. If it does not load, you obviously require the .NET runtime. This approach is not a clean way to handle deployment.

Another solution is to check the Win32 registry for entries, keys like:

HKEY_LOCAL_MACHINEMicrosoft.NETFrameworkpolicyv1.1
HKEY_LOCAL_MACHINEMicrosoft.NETFrameworkpolicyv2.0

While checking the registry will work in many situations, there are some cases where the installation folder has been renamed or a service pack has been applied that will not be reflected with these registry keys.

In this chapter, I present a solution that can determine the list of Common Language Runtimes that are correctly installed and allow enumeration through them. The solution will also account for service packs and renamed folder locations.

Note

The large segments of source code have been removed from this chapter and are available on the Companion Web site in order to fit this chapter into the book. Please refer to the source code of the example in order to clarify any implementation questions.

The Solution

The code in this section forms the solution for this chapter. The code compiles into a static library that can be reused across multiple projects.

Note

The full source code for the solution is available on the Companion Web site.

We do not want a dependency on mscoree, so we use LoadLibrary and GetProcAddress in order to use the needed functions. The prototype for FSGetRequestedRuntimeInfo is also copied directly into the source code so that we do not have a dependency on the header file for mscoree.

typedef HRESULT (__stdcall *FPGetRequestedRuntimeInfo)
                                (LPCWSTR exe,
                                 LPCWSTR versionPtr,
                                 LPCWSTR configurationFile,
                                 DWORD startupFlags,
                                 DWORD runtimeInfoFlags,
                                 LPWSTR directory,
                                 DWORD directoryLength,
                                 DWORD *directoryLengthPtr,
                                 LPWSTR version,
                                 DWORD buffer,
                                 DWORD* length);

The constructor for the version check class first determines whether the .NET framework is installed (in any shape or form); then it determines the base installation path for the .NET framework.

Note

The source code for the constructor is available on the Companion Web site.

The following method is used to find out where the .NET framework base installation path is. This is done by checking the Win32 registry for the following key:

HKEY_LOCAL_MACHINESOFTWAREMicrosoft.NETFrameworkInstallRoot

This method caches the installation path as well, so that multiple version checks do not need to reexecute the code to search the registry.

BOOL CLRVersionCheck::GetInstallationBasePath(TCHAR* basePath, DWORD bufferSize)
{

Note

The source code for this method is available on the Companion Web site.

}

The following method returns a list of CLR versions after querying the .NET framework base installation path.

size_t CLRVersionCheck::EnumerateVersions(std::vector<std::string>& versionList)
{

Note

The source code for this method is available on the Companion Web site.

}

The following method enumerates the directories located in the .NET framework base installation path. It is important to note that not all of these entries will be valid CLR versions, because EnumerateVersions() will handle the filtering and validation.

size_t CLRVersionCheck::EnumerateVersionDirectories(std::vector<std::string>&
                                                    versionList)
{

Note

The source code for this method is available on the Companion Web site.

}

The following method is used to determine whether a CLR version is actually valid and active within the operating system. This method is given a version number, and the GetRequestedRuntimeInfo() method of mscoree is used to validate it.

BOOL CLRVersionCheck::IsVersionAvailable(LPCWSTR frameworkVersion)
{

Note

The source code for this method is available on the Companion Web site.

}

Example Usage

Using the version check library is very easy. To start, link to the static library file (.lib) and include the library header (CLRVersionCheckLib.hpp).

The following code shows a simple console application that enumerates a list of available .NET framework versions and displays the results.

#include "stdafx.h"
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{

Note

The source code for this method is available on the Companion Web site.

Figure 45.1 shows the console application in action.

Screenshot of the console example in action.

Figure 45.1. Screenshot of the console example in action.

Conclusion

This chapter discussed how to determine which versions of the .NET framework are installed on an operating system, and that they are valid and active. There are a couple ways to approach this problem, and each method has viable pros and cons. This method may require more code than simply checking the registry for the policy entries, but you can be guaranteed that the version list returned from the library only contains valid and active CLR versions. This technique is extremely useful for application deployment strategies, though there are other reasons for using this solution as well.

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.139.168