[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

TRegistry OpenKey on Win7 64 bit

Posted on 2011-10-12
9
Medium Priority
?
1,838 Views
Last Modified: 2012-05-12
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?
0
Comment
Question by:brenlex
  • 4
  • 4
9 Comments
 
LVL 25

Expert Comment

by:epasquier
ID: 36955557
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)
0
 

Author Comment

by:brenlex
ID: 36955914
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';

0
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 36956559
The matter is the UAC priviledges....you have to provide a manifest to access the registry in read/write mode
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:epasquier
ID: 36956742
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

0
 

Author Comment

by:brenlex
ID: 36957196
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.
0
 
LVL 25

Expert Comment

by:epasquier
ID: 36957904
yes, I disabled that f$@#ing UAC long time ago
0
 

Author Comment

by:brenlex
ID: 36958004
Unfortunately I do not have the option to disable  UAC on customer sites.
0
 
LVL 25

Accepted Solution

by:
epasquier earned 2000 total points
ID: 36958923
I understand that.
I have proved that Delphi is not the problem, even Delphi 5 which is an old dog still works well with Win7 x64.
I find strange that UAC can lead to registry functions to pretend that all keys/value you try to find do exist when you have not the correct rights. I would have assumed the opposite (the functions returning it does not exist even if it does), exactly like you said.
But considering the nightmare that UAC is, I wouldn't bet it is not related to your problem.

You should try to disable UAC as well, and see if that solves your problem. Even if you cannot do it on the customer site, at least you will be certain to have found the enemy to fight, or that there is another unknown problem.
0
 

Author Comment

by:brenlex
ID: 36959183
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Assume you have an outside contractor who comes in seasonally or once a week to do some work in your office, but you only want to give him access to the programs and files he needs and keep all other documents and programs private. Can you do this o…
A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question