Search code examples
windowsms-officeinno-setuppascalscript

How to take the Office's install directory from registry for an Inno Setup installer


I'm creating an installer for Microsoft Office, specifically for 2007 - 2013 versions. It just copy some files inside two Office's directories. My Windows is 64bit but I want to create a installer for both x64 and x86 architectures.

So I wrote the following code that tries to take from the Windows registry the Office's installation path. And, for each version of Office (2007 - 2013), it takes the installation's path and append the rest of the path I need. That's the result I want.

[Code]
function GetHKLM() : Integer;

begin
 if IsWin64 then
  begin
    Result := HKLM64;
  end
  else
  begin
    Result := HKEY_LOCAL_MACHINE;
  end;
end;

function officeInstallDir(Param: string): string;
// This function takes the type of desired directory,
// verify the version of Office and returns the correct
// directory for style or bibform.

var
    styleFolder, bibformFolder : string;

begin
    // It verifies the Office version through the registry's subkey and it sets the correct Office's path.
    if RegKeyExists(GetHKLM(), '\SOFTWARE\Microsoft\Office\15.0') then begin
        styleFolder     := '{userappdata}\Roaming\Microsoft\Bibliography\Style';
        RegQueryStringValue(GetHKLM(), '\SOFTWARE\Microsoft\Office\15.0\Common', 'InstallRoot', bibformFolder);
        bibformFolder   := bibformFolder + '\1046\Bibliography';
    end else begin
        if RegKeyExists(GetHKLM(), '\SOFTWARE\Microsoft\Office\14.0') then begin
          RegQueryStringValue(GetHKLM(), '\SOFTWARE\Microsoft\Office\14.0\Common', 'InstallRoot', styleFolder);
          styleFolder       := styleFolder + 'Bibliography\Style';
          bibformFolder := styleFolder + '1046\Bibliography';
        end else begin
          if RegKeyExists(GetHKLM(), '\SOFTWARE\Microsoft\Office\12.0') then begin
            RegQueryStringValue(GetHKLM(), '\SOFTWARE\Microsoft\Office\12.0\Common', 'InstallRoot', styleFolder);
            styleFolder     := styleFolder + 'Bibliography\Style';
            bibformFolder   := styleFolder + '1046\Bibliography';
          end
        end;
    end;

    // Set the result according Param passed (the first or second type of path).
    if Param = 'style' then begin
        result := styleFolder;
    end else begin
        result := bibformFolder;
    end;

end;

With one of the paths, I tried to set the file's installation path (DestDir) in Inno Setup like this:

[Files]
Source: "E:\Google Drive\Informática\Bibword\Bibword Estilos\*"; DestDir: "{code:officeInstallDir|style}"; Flags: ignoreversion
Source: "E:\Google Drive\Informática\Bibword\Bibword file\BIBFORM.xml"; DestDir: "{code:officeInstallDir|bibform}"; Flags: ignoreversion

But if I pass the parameters style or bibform, the function officeInstallDir should help me set the correct path for each line. But RegKeyExists or RegQueryStringValue doesn't find the registry's subkeys. I even tried using the GetHKLM() function because the 64bit node problem but no go.

Would anyone help me?


Solution

  • When running 32 bit setup on 64 bit system, Inno will automatically expand HKLM as HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node what means you don't have to manipulate here unless there is 64 bit office installed which may hold his Registry info in 64 bit registry branch. But then you can do additional check for HKLM64 if IsWin64 = true. Although you have to pass it as String but not Integer as you do in your code.

    I would call it this way (but I don't quite understand the final part of your code thus I have just pasted it):

    [Code]
    function officeInstallDir(Param: string): string;
    var
        styleFolder, bibformFolder : string;
    
    begin
        if RegKeyExists(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Office\15.0\Common\InstallRoot') then 
        begin
            RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Office\15.0\Common\InstallRoot', 'Path', bibformFolder);
            styleFolder := ExpandConstant('{userappdata}') + '\Roaming\Microsoft\Bibliography\Style';
            bibformFolder := bibformFolder + '\1046\Bibliography';
        end 
        else begin
            if RegKeyExists(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Office\14.0\Common\InstallRoot') then 
            begin
              RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Office\14.0\Common\InstallRoot', 'Path', styleFolder);
              styleFolder := styleFolder + 'Bibliography\Style';
              bibformFolder := styleFolder + '1046\Bibliography';
            end 
            else begin
              if RegKeyExists(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Office\12.0\Common\InstallRoot') then 
              begin
                RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Office\12.0\Common\InstallRoot', 'Path', styleFolder);
                styleFolder := styleFolder + 'Bibliography\Style';
                bibformFolder := styleFolder + '1046\Bibliography';
              end;
            end;
        end;
    
    
        // I quite don't get this part here:
        if Param = 'style' then 
        begin
            result := styleFolder;
        end 
        else begin
            result := bibformFolder;
        end;
    
    end;