How to read from registry key in a cycle
I'm making an installer that adds features to a previously installed program, kind of add-on.
The must-present program adds registry keys according to the release.
I want to read this key and check if the to-be installed add-on is compatible with the present version of the program to allow it to be installed, otherwise I want to display a notification message informing that no compatible version is present.
My code until now is:
开发者_StackOverflow中文版Result: = RegKeyExists (HKEY_LOCAL_MACHINE, 'Software\Wow6432Node\Program\5.0.0');
if Result = False Then
MsgBox ('Error: NOT program is installed', mbInformation, MB_OK);
if Result = True Then
.....`
Version numbering goes like 5.0.0, 5.0.1, 5.0.2, 5.0.3 ....
I want to check for a bunch of compatible versions on a cycle, how can I achieve this?
If I understand correctly, you want to check for a number of compatible installed versions and proceed only if a compatible version is installed?
You have different choices, if the number of target versions is not to high, the fastest is to check for a series of pre-defined versions, like this:
Warning This is not an elegant solution, just working and simple to code, being warned, look:
const
MaxCompatibleVersions = 4;
function CompatibleVersionPresent: Boolean;
var
I: Integer;
CompatibleVersions: array[1..4] of string;
begin
CompatibleVersions[1] := '5.0.0';
CompatibleVersions[2] := '5.0.1';
CompatibleVersions[3] := '5.0.2';
CompatibleVersions[4] := '5.1.0';
Result := False;
for I := 1 to MaxCompatibleVersions do
begin
Result := Result or RegKeyExists(HKEY_LOCAL_MACHINE, 'Software\Wow6432Node\Program\' + CompatibleVersions[I]);
if Result then
Break;
end;
if not Result then
MsgBox('Error, a compatible version of the program is not present, the plugin cannot be installed', mbError, MB_OK);
end;
procedure InitializeWizard();
begin
if not CompatibleVersionPresent then
Abort;
end;
Improving this is is an exercise up to the reader, some hints:
- Do not store the compatibility list as a part of the installer script, include a text file with the compatible versions list. You can extract the file to a temporary place at runtime and perform the check against that file
- Read the installed version only once and compare with the pre-defined array of strings or StringList loaded from the file.
- A better solution, IMHO, would be to read the installed version, parse it (or store it in different fields for major, minor, release) and then perform a kind of range check. What is a valid check is up to you and the constraints imposed by your compatibility schema.
精彩评论