问题
I'm working on a Inno Setup installer for an MS Office Add-In and trying to find a way to get the version and target platform (bitness) of Excel and Outlook, preferably from the Windows Registry. Although some unofficial sources list some methods to extract version information from the Windows Registry, that information appears to be unreliable.
Does anyone know if there is a reliable (and official) way to get version and platform information from the version of Office (and associated programs such Excel or Outlook) that is currently installed?
回答1:
Based on the answers by @Slava Ivanov and @MB., a code for Inno Setup is:
const
AppPathsKey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths';
const
SCS_32BIT_BINARY = 0;
SCS_64BIT_BINARY = 6;
function GetBinaryType(ApplicationName: string; var BinaryType: Integer): Boolean;
external 'GetBinaryTypeW@kernel32.dll stdcall';
function GetAppVersionAndBinaryType(
ProgramFileName: string; var Version: string; var BinaryType: Integer): Boolean;
var
ProgramPath: string;
begin
Result :=
RegQueryStringValue(HKLM, AppPathsKey + '\' + ProgramFileName, '', ProgramPath);
if not Result then
begin
Log(Format('Cannot find a path to "%s"', [ProgramFileName]));
end
else
begin
Log(Format('Path to "%s" is "%s"', [ProgramFileName, ProgramPath]));
Result := GetVersionNumbersString(ProgramPath, Version);
if not Result then
begin
Log(Format('Cannot retrieve a version of "%s"', [ProgramFileName]));
end
else
begin
Log(Format('Version of "%s" is "%s"', [ProgramFileName, Version]));
Result := GetBinaryType(ProgramPath, BinaryType);
if not Result then
begin
Log(Format('Cannot retrieve a binary type of "%s"', [ProgramFileName]));
end
else
begin
Log(Format('Binary type of "%s" is "%d"', [ProgramFileName, BinaryType]));
end;
end;
end;
end;
The code is for Unicode version of Inno Setup.
回答2:
The following are the steps to get the information ...
Use
HKEY_LOCAL_MACHINE
root and query the path of the application from the keys below ...Software\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE Software\Microsoft\Windows\CurrentVersion\App Paths\excel.exe
When query those keys
(Default)
value you will get the path to the actual file on the filesystem, for example:C:\Program Files\Microsoft Office\Root\Office16\OUTLOOK.EXE
Please note you need to query according to the OS bitness with
KEY_WOW64_64KEY
orKEY_WOW64_32KEY
flag.Use this path to an application and retrieve actual file property "Product version" for example
16.0.8625.2121
. Parse it to get major, minor and build numbers.Once again use
HKEY_LOCAL_MACHINE
withKEY_WOW64_64KEY
orKEY_WOW64_32KEY
flag to query theBitness
key ...Software\Microsoft\Office\%d.0\Outlook
Where
%d
is the major version of the product. If returned value equalx64
Outlook 64 bit version installed.
EDIT:
There are few more solutions (even some spacial for Inno Setup) can be found at Detect whether Office is 32bit or 64bit via the registry thread. Please check it out.
来源:https://stackoverflow.com/questions/47431008/getting-the-version-and-platform-of-office-application-from-windows-registry