Recent

Author Topic: How to equate a name to HKEY_USERS  (Read 2901 times)

guest48704

  • Guest
How to equate a name to HKEY_USERS
« on: November 04, 2018, 03:24:37 pm »
  I am writing a registry key backup program.  I tried to use the ExportRegistryBranch with the key name 'HKEY_USERS', but it rejects it because HKEY_USERS is apparently a key word.  Can someone show how to equate a name to HKEY_USERS?

I ca do it this way:
var HighKeyLI: LongInt;
HighKeyLI := HKEY_USERS;

but I need to do it this way:
var HighKeyLI: LongInt; Key : string;
Key := 'HKEY_USERS';
HighKeyLI := Key;

Thanks

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to equate a name to HKEY_USERS
« Reply #1 on: November 04, 2018, 04:05:34 pm »
You can't assign a string to an integer--or viceversa--directly. You have to do it like this:

Code: Pascal  [Select][+][-]
  1. var
  2.   HighKeyLI: LongInt; Key : string;
  3.  
  4.   Key := 'HKEY_USERS';
  5.   HighKeyLI := HKEY_USERS

If you want to map names to (integer) values you'll have to use the proper structure, like a map, list or even a simple array of records:
Code: Pascal  [Select][+][-]
  1. type
  2.   TKeyName = packed record
  3.     Name: ShortString;
  4.     Value: LongInt;
  5.   end;
  6.  
  7. var
  8.   KeyNames: array[0..1] of TKeyName = (
  9.     (Name: 'HKEY_USERS'; Value: HKEY_USERS);
  10.     (Name: 'HKEY_CURRENT_USER'; Value: HKEY_CURRENT_USER);
  11.     );
  12.  
  13. function GetValueFromName(AName: ShortString): LongInt;
  14. var i: Integer;
  15. begin
  16.   Result := -1; { Arbitrary value chosen as error marker }
  17.   for i := Low(KeyNames) to High(KeyNames) do
  18.     if KeyNames[i].Name = AName then begin
  19.       Result := KeyNames[i].Value;
  20.       Break;
  21.     end;
  22. end;
  23.  
  24. { And elsewhere ... }
  25.   Key := 'HKEY_USERS';
  26.   HighKeyLI := GetValueFromName(Key)

Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

guest48704

  • Guest
Re: How to equate a name to HKEY_USERS
« Reply #2 on: November 04, 2018, 04:43:23 pm »
Thanks.  I would have never figured that out.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to equate a name to HKEY_USERS
« Reply #3 on: November 04, 2018, 05:12:03 pm »
Pascal is a strongly typed language so very different types are frequently assignment incompatible one with another. That's one of the reasons why there are aso many conversion functions: IntToStr, StrToInt, FloatToStr, StrToDateTime, etc.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: How to equate a name to HKEY_USERS
« Reply #4 on: November 04, 2018, 05:54:05 pm »
Pascal is a strongly typed language so very different types are frequently assignment incompatible one with another. That's one of the reasons why there are aso many conversion functions: IntToStr, StrToInt, FloatToStr, StrToDateTime, etc.
Or overloaded operators like this:
Code: Pascal  [Select][+][-]
  1. operator :=(const a:integer):string;inline;
  2. begin
  3.    str(a,result);
  4. end;
  5.  

I have a full unit ready for inclusion if required, but simple types already have easy conversion through sysutils.

Code: Pascal  [Select][+][-]
  1.   string.tointeger;
  2.   integer.tostring;
  3.  
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: How to equate a name to HKEY_USERS
« Reply #5 on: November 04, 2018, 06:06:55 pm »
Or overloaded operators like this:
[...]
I have a full unit ready for inclusion if required, but simple types already have easy conversion through sysutils.

Yes, that's another way. Although it wouldn't have been useful for the OP¡s case, it's nice having it in our arsenal :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018