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:
{$DEFINE HAS_TREGISTRY_OPENKEYREADONLY}
{$IFNDEF CONDITIONALEXPRESSIONS} // Delphi 6+
{$IFNDEF VER130} // Delphi 5
{$IFNDEF VER120} // Delphi 4
{$UNDEF HAS_TREGISTRY_OPENKEYREADONLY}
{$ENDIF}
{$ENDIF}
{$ENDIF}
{$IFNDEF HAS_TREGISTRY_OPENKEYREADONLY}
type
TRegistry = class(Registry.TRegistry)
public
function OpenKeyReadOnly(const Key: String): Boolean;
end;
function TRegistry.OpenKeyReadOnly(const Key: String): Boolean;
var
TempKey: HKey;
S: string;
Relative: Boolean;
begin
S := Key;
Relative := not ((S <> '') and (S[1] = '\'));
if not Relative then Delete(S, 1, 1);
TempKey := 0;
Result := RegOpenKeyEx(GetBaseKey(Relative), PChar(S), 0, KEY_READ, TempKey);
if not Result then
begin
Result := RegOpenKeyEx(GetBaseKey(Relative), PChar(S), 0, STANDARD_RIGHTS_READ or KEY_QUERY_VALUE or KEY_ENUMERATE_SUB_KEYS, TempKey);
if not Result then
Result := RegOpenKeyEx(GetBaseKey(Relative), PChar(S), 0, KEY_QUERY_VALUE, TempKey);
end;
if Result then
begin
if (CurrentKey <> 0) and Relative then S := CurrentPath + '\' + S;
ChangeKey(TempKey, S);
end;
end;
{$ENDIF}
var
l: TStringList;
reg: TRegistry;
n: integer;
begin
Form3.Combobox1.Items.BeginUpdate;
try
Form3.Combobox1.Items.Clear;
l := TStringList.Create;
try
reg := TRegistry.Create;
try
reg.RootKey := HKEY_LOCAL_MACHINE;
if reg.OpenKeyReadOnly('HARDWARE\DEVICEMAP\SERIALCOMM') then
begin
reg.GetValueNames(l);
for n := 0 to l.Count - 1 do
Form3.Combobox1.Items.Add(reg.ReadString(l[n]));
ComboBox1.ItemIndex := 0;
end;
finally
reg.Free;
end;
finally
l.Free;
end;
finally
Form3.Combobox1.Items.EndUpdate;
end;
end;