Link to home
Start Free TrialLog in
Avatar of brenlex
brenlexFlag for United Kingdom of Great Britain and Northern Ireland

asked on

TRegistry OpenKey on Win7 64 bit

I have an application written in Delphi 5 which looks for an existing registry key via use of TRegistry's OpenKey method.  

      LRegistry := TRegistry.Create;
      LRegistry.Access := KEY_READ;
      LRegistry.RootKey := HKEY_LOCAL_MACHINE;
      if LRegistry.ValueExists(MyRegItem) then ...

When the app is run on XP it detemines if the registry entry exists or not OK. My problem however is that the same app run in Windows 7 (64 bit) ALWAYS returns true, even when the registry entry does not exist.  I have also tried if ValueExistsReadOnly with no success.

What gives?
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

I have checked your code with Delphi XE AND Delphi 5, and there is no problem with either.
I always get False.
but the strange thing is that you don't set the current key (using  LRegistry.OpenKey)
Is that just that you forgot to mention here ? maybe result is undefined in D5 when you don't set the key (which would be a bug, but then there would be one in your code as well)
Avatar of brenlex

ASKER

Sorry above lines should read:
      LRegistry := TRegistry.Create;
      LRegistry.Access := KEY_READ;
      LRegistry.RootKey := HKEY_LOCAL_MACHINE;
      if (LRegistry.OpenKey(MyRegKey, False)) then
      begin
            if LRegistry.ValueExists(MyRegItem) then ...

Did you test in Win7.  I actually experience the same problem on both 32 bit and 64 bit Win7.  I have passed to a colleague to test also and he confirms problem too.

In fact, both the calls to OpenKey AND ValueExists return TRUE even when no registry entry exists.

Indeed, vars are specified as:
            MyRegKey = '\Software\My Company\My Product';
            MyRegItem = 'Config';

The matter is the UAC priviledges....you have to provide a manifest to access the registry in read/write mode
Win7 x64 working great even with Delphi 5. both for existing key/values as expected

here is the code of the console application I made with D5 to test this
program RegTest;
{$APPTYPE CONSOLE}
uses
  SysUtils, Registry, Windows, Dialogs;

const
 MyRegKey='\BCD00000000\Description';
 MyRegItem='KeyName';

function BoolToStr(B:Boolean):String;
begin
 if B Then Result:='True' Else Result:='False';
end;

procedure Log(S:String);
begin
 Writeln(S);
end;

Var
 LRegistry : TRegistry;

procedure ReadValue(v:String);
begin
 Log(Format('Reading Value %s : %s = %s',[v, BoolToStr(LRegistry.ValueExists(v)), LRegistry.ReadString(v)]) );
end;

Function OpenKey(key:String):Boolean;
begin
 Result:=LRegistry.OpenKey(key,False);
 Log('OpenKey '+key+' : '+BoolToStr(Result));
end;

begin
  LRegistry := TRegistry.Create;
  LRegistry.Access := KEY_READ;
  LRegistry.RootKey := HKEY_LOCAL_MACHINE;
  if OpenKey(MyRegKey) Then
   begin
    ReadValue(MyRegItem);
    ReadValue(MyRegItem+'#BLOB#'); // Value NOT existing
   end;
  OpenKey(MyRegKey+'#BLOB#'); // Key NOT existing
  LRegistry.Free;
  readln;
end.

Open in new window


and here is the expected result :
OpenKey \BCD00000000\Description : True
Reading Value KeyName : True = BCD00000000
Reading Value KeyName#BLOB# : False =
OpenKey \BCD00000000\Description#BLOB# : False

Open in new window

Avatar of brenlex

ASKER

epasquier - are you running with Admin rights?

I am testing without admin rights.  

Please advise.

***

Ferruccio68 - I am only trying to READ registry (without admin priviledges) -- I am expecting the aforementioned method calls to return FALSE if it cannot read the registry due to access rights.  This is what I want, rather than returning TRUE everytime.
yes, I disabled that f$@#ing UAC long time ago
Avatar of brenlex

ASKER

Unfortunately I do not have the option to disable  UAC on customer sites.
ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brenlex

ASKER

Cracked it.  I discovered that when I ran regedit32.exe from the C:\windows\SysWOW64 directory it actually shows my entries as existing!  Standard regedit from cmd line did not!

No code changes required :o)

Thanks for your input.