Lazarus

Programming => General => Topic started by: guest48704 on November 04, 2018, 03:24:37 pm

Title: How to equate a name to HKEY_USERS
Post by: guest48704 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
Title: Re: How to equate a name to HKEY_USERS
Post by: lucamar 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)

Title: Re: How to equate a name to HKEY_USERS
Post by: guest48704 on November 04, 2018, 04:43:23 pm
Thanks.  I would have never figured that out.
Title: Re: How to equate a name to HKEY_USERS
Post by: lucamar 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.
Title: Re: How to equate a name to HKEY_USERS
Post by: Thaddy 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.  
Title: Re: How to equate a name to HKEY_USERS
Post by: lucamar 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 :)
TinyPortal © 2005-2018