Recent

Author Topic: accesing registry  (Read 4877 times)

diego bertotti

  • Full Member
  • ***
  • Posts: 101
accesing registry
« on: May 10, 2021, 09:41:36 pm »
hi

i have a question, but is about delphi...i know, here is for lazarus, but maybe can help me

reading win registry to know how many serial comm ports the machine have, i can do it successfully when executable is running from delphi ide, but when this exe run alone (dblclck on its name), it dont work!

that's the piece of code, for very old delphi 2.0 and tested on old delphi 6 too, with the same results;
Code: Pascal  [Select][+][-]
  1. var    
  2.   l: TStringList;
  3.   reg: TRegistry;
  4.   n: integer;
  5. begin
  6.   l := TStringList.Create;
  7.   reg := TRegistry.Create;
  8.   try
  9. {$IFNDEF VER100}
  10. {$IFNDEF VER120}
  11. //    reg.Access := KEY_READ;
  12. {$ENDIF}
  13. {$ENDIF}
  14.     reg.RootKey := HKEY_LOCAL_MACHINE;
  15.     reg.OpenKey('HARDWARE\DEVICEMAP\SERIALCOMM', false);
  16.     reg.GetValueNames(l);
  17.     for n := 0 to l.Count - 1 do
  18.       Form3.Combobox1.Items.Add(reg.ReadString(l[n]));
  19.  
  20.     ComboBox1.ItemIndex:= 0;
  21.   finally
  22.     reg.Free;
  23.     l.Free;
  24.   end;
  25.  

can someone help me?


mas steindorff

  • Hero Member
  • *****
  • Posts: 526
Re: accesing registry
« Reply #1 on: May 10, 2021, 10:20:30 pm »
I do not know about Deiph anymore but you have old synaser code that may not work even for FPC.
what I've had to do is to break up the loading of the reg, 1st into a tstringlist and then, 2nd: tstringlist into the combo box. 
you should also note I use ..keyReadOnly  as well.

Here is the same function originally written by "Lukas Gebauer" with my mods

Code: [Select]
function GetSerialPortNames: string;
var
  reg: TRegistry;
  l  : TStringList;
  n: integer;
begin
  l := TStringList.Create;
  reg := TRegistry.Create;
  try
{$IFNDEF VER100}
    reg.Access := KEY_READ;
{$ENDIF}
    reg.RootKey := HKEY_LOCAL_MACHINE;
    reg.OpenKeyReadOnly('HARDWARE\DEVICEMAP\SERIALCOMM');//, false);
    reg.GetValueNames(l);
    Result := l.CommaText;
  finally
    reg.Free;
    l.Free;
  end;
end;
 

I do suggest you find the latest version of the synaser unit as there are some other  improvements as well.  I believe FPC comes with a version that was derived from this unit but I can't be sure. 
windows 7/10 - laz 2.0 / 1.2.6 general releases

diego bertotti

  • Full Member
  • ***
  • Posts: 101
Re: accesing registry
« Reply #2 on: May 10, 2021, 10:48:21 pm »
stop the press!

first, thanks for reply.

now, the problem is not in the code.

is about rights!
if i run this exe alone but using "run like admin", then its works

mas steindorff

  • Hero Member
  • *****
  • Posts: 526
Re: accesing registry
« Reply #3 on: May 10, 2021, 10:58:39 pm »
you may wish to keep those presses running and think about WHY you need this admin rights :)
windows 7/10 - laz 2.0 / 1.2.6 general releases

diego bertotti

  • Full Member
  • ***
  • Posts: 101
Re: accesing registry
« Reply #4 on: May 10, 2021, 11:06:33 pm »
yes, i'm working on it, but if you have the answer, i'll be very happy if you share with us
« Last Edit: May 10, 2021, 11:28:22 pm by diego bertotti »

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: accesing registry
« Reply #5 on: May 11, 2021, 04:36:02 am »
yes, i'm working on it, but if you have the answer, i'll be very happy if you share with us
The problem is in the code. Read carefully, in the example from @[mas steindorff] it was TRegistry.OpenKeyReadOnly - it's safe. You use TRegistry.OpenKey - this requires the KEY_ALL_ACCESS rights.

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: accesing registry
« Reply #6 on: May 11, 2021, 02:11:23 pm »
The problem is in the code. Read carefully, in the example from @[mas steindorff] it was TRegistry.OpenKeyReadOnly - it's safe. You use TRegistry.OpenKey - this requires the KEY_ALL_ACCESS rights.

Only by default, you can use the overload for the constructor to specify the access.
(I know, it's nitpicking.)

Bart

mas steindorff

  • Hero Member
  • *****
  • Posts: 526
Re: accesing registry
« Reply #7 on: May 11, 2021, 07:57:21 pm »
yes, i'm working on it, but if you have the answer, i'll be very happy if you share with us
sorry for ignoring you for so long.  figured I poke you a little (15 minutes) for still using Delphi. I did not plan on getting pulled away but ASerge came up to the task. I'm not sure if that is the fix but I do not have any admin issues on win10 with this code.
windows 7/10 - laz 2.0 / 1.2.6 general releases

diego bertotti

  • Full Member
  • ***
  • Posts: 101
Re: accesing registry
« Reply #8 on: May 11, 2021, 11:24:09 pm »
yes, i'm working on it, but if you have the answer, i'll be very happy if you share with us
The problem is in the code. Read carefully, in the example from @[mas steindorff] it was TRegistry.OpenKeyReadOnly - it's safe. You use TRegistry.OpenKey - this requires the KEY_ALL_ACCESS rights.

i saw it but registry.pas unit of old delphi 2.0 don't have this function

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: accesing registry
« Reply #9 on: May 12, 2021, 05:11:27 pm »
i saw it but registry.pas unit of old delphi 2.0 don't have this function

There are plenty registry replacements out there that doe have this function.
I have one in my repository, besed upon fpc's one.

But IIRC then D2 does not allow to overload functions/procedures/methods, so it probably won't compile in D2.

Bart

diego bertotti

  • Full Member
  • ***
  • Posts: 101
Re: accesing registry
« Reply #10 on: May 13, 2021, 11:39:49 pm »
i saw it but registry.pas unit of old delphi 2.0 don't have this function

There are plenty registry replacements out there that doe have this function.
I have one in my repository, besed upon fpc's one.

But IIRC then D2 does not allow to overload functions/procedures/methods, so it probably won't compile in D2.

Bart

you right, i try registry.pas unit of another delphi versions, and i got this error (can't be overloaded)

mas steindorff

  • Hero Member
  • *****
  • Posts: 526
Re: accesing registry
« Reply #11 on: May 13, 2021, 11:59:31 pm »
since D2 is not current nor designed for later window OS, you may need to modify the exe's properties using the run in (backward) compatibility mode.  I believe you can set the admin access right's in this new shortcut as well if your running win10 pro.
Otherwise consider upgrading to a newer (perhaps free) compiler :) 
windows 7/10 - laz 2.0 / 1.2.6 general releases

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1311
    • Lebeau Software
Re: accesing registry
« Reply #12 on: May 14, 2021, 04:10:48 am »
i have a question, but is about delphi...i know, here is for lazarus, but maybe can help me

There are Delphi forums available at https://en.delphipraxis.net.

that's the piece of code, for very old delphi 2.0 and tested on old delphi 6 too, with the same results;

The problem is that you are opening the key with KEY_ALL_ACCESS rights, which is restricted to admins only.  TRegistry.GetValueNames() uses a combination of RegQueryInfoKey() and RegEnumValue(), so the bare-minimum rights you need to open the key with is just KEY_QUERY_VALUE.  Most users don't have write access to HKEY_LOCAL_MACHINE, but they typically have read-only access.

TRegistry has an OpenKeyReadOnly() method since Delphi 4, and an Access property since Delphi 5 (maybe even 4).  For older versions, just call RegCreateKeyEx() or RegOpenKeyEx() directly, as needed.  An interposer class can be used to call the protected TRegistry.ChangeKey() method, which is needed to update the TRegistry.CurrentKey and TRegistry.CurrentPath properties.

Try something like this:

Code: Pascal  [Select][+][-]
  1. {$DEFINE HAS_TREGISTRY_OPENKEYREADONLY}
  2.  
  3. {$IFNDEF CONDITIONALEXPRESSIONS} // Delphi 6+
  4.   {$IFNDEF VER130} // Delphi 5
  5.     {$IFNDEF VER120} // Delphi 4
  6.       {$UNDEF HAS_TREGISTRY_OPENKEYREADONLY}
  7.     {$ENDIF}
  8.   {$ENDIF}
  9. {$ENDIF}
  10.  
  11. {$IFNDEF HAS_TREGISTRY_OPENKEYREADONLY}
  12. type
  13.   TRegistry = class(Registry.TRegistry)
  14.   public
  15.     function OpenKeyReadOnly(const Key: String): Boolean;
  16.   end;
  17.  
  18. function TRegistry.OpenKeyReadOnly(const Key: String): Boolean;
  19. var
  20.   TempKey: HKey;
  21.   S: string;
  22.   Relative: Boolean;
  23. begin
  24.   S := Key;
  25.   Relative := not ((S <> '') and (S[1] = '\'));
  26.  
  27.   if not Relative then Delete(S, 1, 1);
  28.   TempKey := 0;
  29.  
  30.   Result := RegOpenKeyEx(GetBaseKey(Relative), PChar(S), 0, KEY_READ, TempKey);
  31.   if not Result then
  32.   begin
  33.     Result := RegOpenKeyEx(GetBaseKey(Relative), PChar(S), 0, STANDARD_RIGHTS_READ or KEY_QUERY_VALUE or KEY_ENUMERATE_SUB_KEYS, TempKey);
  34.     if not Result then
  35.       Result := RegOpenKeyEx(GetBaseKey(Relative), PChar(S), 0, KEY_QUERY_VALUE, TempKey);
  36.   end;
  37.   if Result then
  38.   begin
  39.     if (CurrentKey <> 0) and Relative then S := CurrentPath + '\' + S;
  40.     ChangeKey(TempKey, S);
  41.   end;
  42. end;
  43. {$ENDIF}
  44.  
  45. var    
  46.   l: TStringList;
  47.   reg: TRegistry;
  48.   n: integer;
  49. begin
  50.   Form3.Combobox1.Items.BeginUpdate;
  51.   try
  52.     Form3.Combobox1.Items.Clear;
  53.     l := TStringList.Create;
  54.     try
  55.       reg := TRegistry.Create;
  56.       try
  57.         reg.RootKey := HKEY_LOCAL_MACHINE;
  58.         if reg.OpenKeyReadOnly('HARDWARE\DEVICEMAP\SERIALCOMM') then
  59.         begin
  60.           reg.GetValueNames(l);
  61.           for n := 0 to l.Count - 1 do
  62.             Form3.Combobox1.Items.Add(reg.ReadString(l[n]));
  63.           ComboBox1.ItemIndex := 0;
  64.         end;
  65.       finally
  66.         reg.Free;
  67.       end;
  68.     finally
  69.       l.Free;
  70.     end;
  71.   finally
  72.     Form3.Combobox1.Items.EndUpdate;
  73.   end;
  74. end;
  75.  
« Last Edit: May 14, 2021, 04:13:19 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018