Sunday, March 4, 2012

Inno Setup, Part 5. Checking .NET Framework versions.

In installers for programs which are using .NET Framework it is necessary to perform a check before installation if .NET Framework is installed on the machine. Such checks are not difficult and, actually they are checks of some keys in registry. These registry keys depend on .NET Framework version.

If .NET Framework is not installed, you need to install it. We prefer opening browser with appropriate URL in order user can download and install .NET Framework. Another way is to include .NET Framework installer into the main installer of the program, it may be convenient but of course this increases size of installer significantly.

Now let’s turn to the code. Here is the check for .NET Framework 3.0:
[CustomMessages]
en.dotnetmissing=Our program needs Microsoft .NET Framework 3.0. Would you like to download and install it now?
[Code]
function InitializeSetup(): Boolean;
var
    ErrorCode: Integer;
    netFrameWorkInstalled : Boolean;
    isInstalled: Cardinal;
begin
    result := true;
    isInstalled := 0;
    netFrameworkInstalled := RegQueryDWordValue(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup',
  'InstallSuccess', isInstalled);
    if ((netFrameworkInstalled)  and (isInstalled <> 1)) then netFrameworkInstalled := false;   
    if netFrameworkInstalled = false then
    begin
        if (MsgBox(ExpandConstant('{cm:dotnetmissing}'),
            mbConfirmation, MB_YESNO) = idYes) then
        begin
          ShellExec('open',
          'http://www.microsoft.com/downloads/details.aspx?familyid=10CC340B-F857-4A14-83F5-25634C3BF043&displaylang=en',
          '','',SW_SHOWNORMAL,ewNoWait,ErrorCode);
        end;
        result := false;
    end;
end;
Here is the same code for .NET Framework 4 Client Profile.
[CustomMessages]
dotnetmissing=Our program needs Microsoft .NET Framework 4.0 Client Profile. Would you like to download and install it now?
[Code]
function InitializeSetup(): Boolean;
var
    ErrorCode: Integer;
    netFrameWorkInstalled : Boolean;
    isInstalled: Cardinal;
begin
    result := true;

  isInstalled := 0;
    netFrameworkInstalled := RegQueryDWordValue(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\client',
  'Install', isInstalled);
    if ((netFrameworkInstalled)  and (isInstalled <> 1)) then netFrameworkInstalled := false;

    if netFrameworkInstalled = false then
    begin
        if (MsgBox(ExpandConstant('{cm:dotnetmissing}'),
            mbConfirmation, MB_YESNO) = idYes) then
        begin
          ShellExec('open',
          'http://download.microsoft.com/download/7/B/6/7B629E05-399A-4A92-B5BC-484C74B5124B/dotNetFx40_Client_setup.exe',
          '','',SW_SHOWNORMAL,ewNoWait,ErrorCode);
        end;
        result := false;
    end;
end; 
So comparing functions for two versions of .NET Framework we notice that they are differing only in messages, download URLs and registry keys. If necessary, you can simply make general function for several versions of .NET Framework.

Update: for example of general function which checks different versions of .NET Framework you can look at this Q & A at stackoverflow.com.

No comments:

Post a Comment