Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why wont TRegIniFile read HKEY_LOCAL_MACHINE if not a local administrator?

365 views
Skip to first unread message

Chris Mann

unread,
Feb 26, 1999, 3:00:00 AM2/26/99
to
Hi guys,

I'm not sure if this is the correct board to ask this, but I will anyway.
If not the correct board, please post the correct one in response to this...

I am using the TRegIniFile component in Delphi 2 to read entries from the
HKEY_LOCAL_MACHINE/SOFTWARE tree and am finding that it fails to read
anything, always returning the default value, if the current NT user is not
a member of the administrator group. Is this a problem or a feature? -
Example code call is below:

...
var TmpIni: TRegIniFile;
...
TmpIni.OpenKey('SOFTWARE', TRUE);
TmpIni.OpenKey(DDM_REG_KEY, TRUE);


TmpIni := TRegIniFile.Create('Software');
TmpIni.RootKey := HKEY_LOCAL_MACHINE;
TmpIni.OpenKey('SOFTWARE', TRUE);
TmpIni.OpenKey('MY_SOFTWARE', TRUE);

CertNoEdit.Text := TmpIni.ReadString('Security', 'License No.',
'');
PasswordEdit.Text := TmpIni.Readstring('Security', 'Password', '');

Tmpini.Free;
...

Many thanks for any help,

Chris Mann.


Steve Schafer (TeamB)

unread,
Feb 26, 1999, 3:00:00 AM2/26/99
to
On Fri, 26 Feb 1999 12:32:12 -0000, "Chris Mann"
<Chr...@Definitive.co.uk> wrote:

> I am using the TRegIniFile component in Delphi 2 to read entries from the
>HKEY_LOCAL_MACHINE/SOFTWARE tree and am finding that it fails to read
>anything, always returning the default value, if the current NT user is not
>a member of the administrator group. Is this a problem or a feature? -

TRegIniFile attempts to open a registry key with KEY_ALL_ACCESS, which
fails on the HKEY_LOCAL_MACHINE tree if you don't have Administrator
permissions. The workaround is to use the lower-level API functions
(e.g., RegOpenKeyEx) instead of the TRegIniFile or TRegistry classes,
and use a KEY_READ or KEY_QUERY_VALUE access specifier.

-Steve


Dick Tergood

unread,
Feb 26, 1999, 3:00:00 AM2/26/99
to
I've had plenty of good luck using the TRegistry class for reading data
from the very same hive you've mentioned. It's an easy class to use.

Chris Mann wrote in message <7b6483$i7...@forums.borland.com>...


>Hi guys,
>
> I'm not sure if this is the correct board to ask this, but I will
anyway.
>If not the correct board, please post the correct one in response to
this...
>

> I am using the TRegIniFile component in Delphi 2 to read entries from
the
>HKEY_LOCAL_MACHINE/SOFTWARE tree and am finding that it fails to read
>anything, always returning the default value, if the current NT user is
not
>a member of the administrator group. Is this a problem or a

Alex

unread,
Feb 27, 1999, 3:00:00 AM2/27/99
to
On Fri, 26 Feb 1999 12:32:12 -0000, Chris Mann
<Chr...@Definitive.co.uk> wrote:

>Hi guys,
>
> I'm not sure if this is the correct board to ask this, but I will anyway.
>If not the correct board, please post the correct one in response to this...
>
> I am using the TRegIniFile component in Delphi 2 to read entries from the
>HKEY_LOCAL_MACHINE/SOFTWARE tree and am finding that it fails to read
>anything, always returning the default value, if the current NT user is not

>a member of the administrator group. Is this a problem or a feature? -

Chris!

There is a known bug in the Delphi implementation of registry under NT
(see Seve Shafer's posting).
However, you can find a freeware Delphi NT Registry unit at
http://www.jgsoftware.com/nt.htm.

Hope it helps...

Alex
ErgoLight Usability Software
Visit us at www.ergolight-sw.com

Stefan Hoffmeister

unread,
Feb 27, 1999, 3:00:00 AM2/27/99
to
: ale...@remove.this.ibm.net (Alex) wrote:

>There is a known bug in the Delphi implementation

This is not a bug. This is by design.

>of registry under NT
>(see Seve Shafer's posting).
>However, you can find a freeware Delphi NT Registry unit at
>http://www.jgsoftware.com/nt.htm.

Now that is right.


--
Stefan Hoffmeister http://www.econos.de/
No private email, please, unless expressly invited.

Primoz Gabrijelcic

unread,
Feb 27, 1999, 3:00:00 AM2/27/99
to
>However, you can find a freeware Delphi NT Registry unit at
>http://www.jgsoftware.com/nt.htm.


You can also code a replacement OpenKeyReadOnly routine. We're using this in
mwEdit highlighters:

TBetterRegistry = class(TRegistry)
{$IFNDEF VER120}
function OpenKeyReadOnly(const Key: string): Boolean;
{$ENDIF}
end;
function TBetterRegistry.OpenKeyReadOnly(const Key: string): Boolean;
var
TempKey: HKey;
S: string;
Relative: Boolean;
begin
S := Key;
Relative := IsRelative(S);

if not Relative then Delete(S, 1, 1);
TempKey := 0;
Result := RegOpenKeyEx(GetBaseKey(Relative), PChar(S), 0,
KEY_READ, TempKey) = ERROR_SUCCESS;
if Result then
begin
if (CurrentKey <> 0) and Relative then S := CurrentPath + '\' + S;
ChangeKey(TempKey, S);
end;
end; { TBetterRegistry.OpenKeyReadOnly }

Primoz

John Rumm

unread,
Mar 1, 1999, 3:00:00 AM3/1/99
to
In article <7b6483$i7...@forums.borland.com>,
"Chris Mann" <Chr...@Definitive.co.uk> wrote:

> I am using the TRegIniFile component in Delphi 2 to read entries from the
> HKEY_LOCAL_MACHINE/SOFTWARE tree and am finding that it fails to read
> anything, always returning the default value, if the current NT user is not
> a member of the administrator group. Is this a problem or a feature? -

I have spent some time looking at this problem with the TRegistry component.
So have Borland it would seem - each version of registry.pas has a few subtle
variations in this area. Delphi 3 still had problems reading and writing to
that hive without admin rights. Delphi 3.02 allowed you to read, but not
write.

The following fix will allow you to read and write without admin rights, just
include the using and then use TNewRegistry in place of TRegistry:-

unit NewRegistry;

(*
* Name : NewRegistry.pas
*
* Description : Fixes problem with VCL TRegistry
*
*)

interface

uses windows, registry, SysUtils ;

type

TNewRegistry = class( TRegistry )
private
function IsRelative(const Value: string): Boolean;

public
function OpenKey(const Key: string; CanCreate: Boolean): Boolean;
end;

implementation

function TNewRegistry.IsRelative(const Value: string): Boolean;
begin
Result := not ((Value <> '') and (Value[1] = '\'));
end;

// The change made to the VCL version of this procedure, is to retry the open
// with access rights that do not require the same level of privilige
// if the first attempt with KEY_ALL_ACCESS fails.

function TNewRegistry.OpenKey(const Key: string; CanCreate: Boolean): Boolean;


var
TempKey: HKey;
S: string;

Disposition: Integer;
Relative: Boolean;
ErrorCode: Integer;


begin
S := Key;
Relative := IsRelative(S);
if not Relative then Delete(S, 1, 1);

if S[ Length( S ) ] = '\' then SetLength( S, Length( S ) - 1 );
TempKey := 0;
if not CanCreate or (S = '') then
begin
// Open key with full rights
ErrorCode := RegOpenKeyEx(GetBaseKey(Relative), PChar(S), 0,
KEY_ALL_ACCESS, TempKey);

// if we fail try again - less rights requested
if ErrorCode <> ERROR_SUCCESS then
ErrorCode := RegOpenKeyEx(GetBaseKey(Relative), PChar(S), 0,
KEY_READ or KEY_WRITE, TempKey);

Result := ErrorCode = ERROR_SUCCESS;
end
else
begin
// Create key with full rights
ErrorCode := RegCreateKeyEx(GetBaseKey(Relative), PChar(S), 0, nil,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nil, TempKey, @Disposition) ;

// if we fail try again - less rights requested if ErrorCode <>
ERROR_SUCCESS then ErrorCode := RegCreateKeyEx(GetBaseKey(Relative),
PChar(S), 0, nil, REG_OPTION_NON_VOLATILE, KEY_READ or KEY_WRITE, nil,
TempKey, @Disposition) ;

Result := ErrorCode = ERROR_SUCCESS;
end;

if Result then
begin
if (CurrentKey <> 0) and Relative then S := CurrentPath + '\' + S;
ChangeKey(TempKey, S);
end;
end;


end.

John Rumm

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

0 new messages