* * *

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

lazarus9

  • Jr. Member
  • **
  • Posts: 95
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

  • Sr. Member
  • ****
  • Posts: 354
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)

Been there, done that ... barely kept the timelines.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/fpc 3.0.4 - Ubuntu 10, Kubuntu 14, Windows XP SP3 (Home & Prof.) and various DOS incarnations.

lazarus9

  • Jr. Member
  • **
  • Posts: 95
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

  • Sr. Member
  • ****
  • Posts: 354
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.
Been there, done that ... barely kept the timelines.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/fpc 3.0.4 - Ubuntu 10, Kubuntu 14, Windows XP SP3 (Home & Prof.) and various DOS incarnations.

Thaddy

  • Hero Member
  • *****
  • Posts: 7140
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.  
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

lucamar

  • Sr. Member
  • ****
  • Posts: 354
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 :)
Been there, done that ... barely kept the timelines.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4/fpc 3.0.4 - Ubuntu 10, Kubuntu 14, Windows XP SP3 (Home & Prof.) and various DOS incarnations.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus