Inno Setup: Verify that .NET 4.0 is installed

前端 未结 3 1487
野性不改
野性不改 2020-11-29 18:39

I have a component that requires .NET 4.0 to run, how can my Inno Setup installer verify that it is installed, and if not, prompt the user to install it?

3条回答
  •  再見小時候
    2020-11-29 19:19

    @Cody Gray - Thanks for your solution. Very helpful!

    In case anyone is interested, here is my take on his function using enumeration values instead of strings. This change is purely a matter of personal preference. The code includes the changes for v4.5 and seems to work properly based on my limited testing.

    David

    [Code]
    
    //
    // Enumeration used to specify a .NET framework version 
    //
    type TDotNetFramework = (
        DotNet_v11_4322,  // .NET Framework 1.1
        DotNet_v20_50727, // .NET Framework 2.0
        DotNet_v30,       // .NET Framework 3.0
        DotNet_v35,       // .NET Framework 3.5
        DotNet_v4_Client, // .NET Framework 4.0 Client Profile
        DotNet_v4_Full,   // .NET Framework 4.0 Full Installation
        DotNet_v45);      // .NET Framework 4.5
    
    //
    // Checks whether the specified .NET Framework version and service pack
    // is installed (See: http://www.kynosarges.de/DotNetVersion.html)
    //
    // Parameters:
    //   Version     - Required .NET Framework version
    //   ServicePack - Required service pack level (0: None, 1: SP1, 2: SP2 etc.)
    //
    function IsDotNetInstalled(Version: TDotNetFramework; ServicePack: cardinal): boolean;
      var
        KeyName      : string;
        Check45      : boolean;
        Success      : boolean;
        InstallFlag  : cardinal; 
        ReleaseVer   : cardinal;
        ServiceCount : cardinal;
      begin
        // Registry path for the requested .NET Version
        KeyName := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\';
    
        case Version of
          DotNet_v11_4322:  KeyName := KeyName + 'v1.1.4322';
          DotNet_v20_50727: KeyName := KeyName + 'v2.0.50727';
          DotNet_v30:       KeyName := KeyName + 'v3.0';
          DotNet_v35:       KeyName := KeyName + 'v3.5';
          DotNet_v4_Client: KeyName := KeyName + 'v4\Client';
          DotNet_v4_Full:   KeyName := KeyName + 'v4\Full';
          DotNet_v45:       KeyName := KeyName + 'v4\Full';
        end;
    
        // .NET 3.0 uses "InstallSuccess" key in subkey Setup
        if (Version = DotNet_v30) then
          Success := RegQueryDWordValue(HKLM, KeyName + '\Setup', 'InstallSuccess', InstallFlag) else
          Success := RegQueryDWordValue(HKLM, KeyName, 'Install', InstallFlag);
    
        // .NET 4.0/4.5 uses "Servicing" key instead of "SP"
        if (Version = DotNet_v4_Client) or
           (Version = DotNet_v4_Full) or
           (Version = DotNet_v45) then
          Success := Success and RegQueryDWordValue(HKLM, KeyName, 'Servicing', ServiceCount) else
          Success := Success and RegQueryDWordValue(HKLM, KeyName, 'SP', ServiceCount);
    
        // .NET 4.5 is distinguished from .NET 4.0 by the Release key
        if (Version = DotNet_v45) then
          begin
            Success := Success and RegQueryDWordValue(HKLM, KeyName, 'Release', ReleaseVer);
            Success := Success and (ReleaseVer >= 378389);
          end;
    
        Result := Success and (InstallFlag = 1) and (ServiceCount >= ServicePack);
      end;
    

提交回复
热议问题