OpenKey/OpenKeyReadOnly only works sometimes

I'm being frustrated by this problem.

Connecting to remote registry using RegistryConnect, I'm able to open one key and read a value, but not another key.  The code for both is the same, same root, only the key name changes.

The really odd thing is, if I fire up RegEdit and connect remotely to the users' PC, I able to easily browse to either key and see their contents and values.  I'm not changing the root, nor is the authentication different.  I've tried using the same instance of TRegistry to read both, and then tried one for the first, then free it, then another one for the 2nd, and then free it.  Either way, same result: I could read the first key, but not the second, even though I could RegEdit to either one.  I've triple-checked the spelling of the key, and it's identical to the one I copy from RegEdit.

Anyone have any ideas as to what I can try next?

(The code will display the logon name, but will fail when trying to get 'Enabled'.)
procedure TForm1.Button1Click(Sender: TObject);
var RegFile : TRegistry;
    connected, r1, r2 : boolean;
    lun : string;
    enabled : integer;
begin
// partial code:
 
RegFile := TRegistry.Create;
RegFile.RootKey := HKEY_USERS;
connected := RegFile.RegistryConnect('\\hou-l0-kpeterso');
if connected then
  begin
     r1 := RegFile.OpenKeyReadOnly('S-1-5-21-1715567821-484763869-839522115-98312\Software\Microsoft\Windows\CurrentVersion\Explorer');
     lun := RegFile.ReadString('Logon User Name');
     if not r1 then ShowMessage('first OpenKeyReadOnly failed')
       else ShowMessage('Logon User Name: '+lun);
     r2 := RegFile.OpenKeyReadOnly('S-1-5-21-1715567821-484763869-839522115-98312\Software\America Online\AOL Instant Messenger (TM)\CurrentVersion\Proxy');
     if not r2 then ShowMessage('first OpenKeyReadOnly failed')
       else
         begin
             enabled := RegFile.ReadInteger('Enabled');
             ShowMessage('Enabled = '+IntToStr(enabled));
         end;
 end
else ShowMessage('not connected');
 
end;

Open in new window

rlairdAsked:
Who is Participating?
 
Geert GOracle dbaCommented:
The windows authentication has certainly got it's finger in this !
I got the same problem, but i had a server which had access to all accounts and pc's and ran it from there.

That solved it for me ...
0
 
Geert GOracle dbaCommented:
is this all 32 bit or a mix 64 bit - 32 bit ?

you need to put some checks in to see if it opens and a key all access
don't you get an AV when trying to read a non opened key ?
procedure TForm1.Button1Click(Sender: TObject);
var RegFile : TRegistry;
    connected, r1, r2 : boolean;
    lun : string;
    enabled : integer;
begin
// partial code:
  RegFile := TRegistry.Create(KEY_ALL_ACCESS);
  try
    RegFile.RootKey := HKEY_USERS;
    connected := RegFile.RegistryConnect('\\hou-l0-kpeterso');
    if connected then
    begin
      r1 := RegFile.OpenKeyReadOnly('S-1-5-21-1715567821-484763869-839522115-98312\Software\Microsoft\Windows\CurrentVersion\Explorer');
      if not r1 then 
        ShowMessage('first OpenKeyReadOnly failed')
      else 
      begin
        lun := RegFile.ReadString('Logon User Name');
        ShowMessage('Logon User Name: '+lun);
      end;
      r2 := RegFile.OpenKeyReadOnly('S-1-5-21-1715567821-484763869-839522115-98312\Software\America Online\AOL Instant Messenger (TM)\CurrentVersion\Proxy');
      if not r2 then 
        ShowMessage('second OpenKeyReadOnly failed')
      else
      begin
        enabled := RegFile.ReadInteger('Enabled');
        ShowMessage('Enabled = '+IntToStr(enabled));
      end;
    end else 
      ShowMessage('not connected');
  finally
    FreeAndNil(RegFile);
  end;
end;

Open in new window

0
 
rlairdAuthor Commented:
This is all 32-bit, D2007.  I don't try to read a value in a key if the key doesn't open.   But I'll try your code and see if it helps (on Monday).  Thanks!
0
 
rlairdAuthor Commented:
Ok, I tried your code but the result was the same as my code.  I'm getting the strong feeling that it has more to do with Windows authentication than my code.  You would *think* that, if I can get to it via RegEdit, then, on the same machine, same user, same login that I'm running RedEdit on, that the code I'd run would have the same access, but maybe that assumption is false?

So, I tried the old trick of using an WNetConnection of the IPC on the destination machine to give me greather authority level, but that didn't work either.

Anyone have any ideas of where I go from here?  BTW, 80%-95% of the machines I try this code on works... but there are some of the machines that give the wacky result I've been discussing.  So, I think it's definitely not the code.  Still, it's VERY frustrating, especially when I can manually use RegEdit to see the key values.
0
 
rlairdAuthor Commented:
I'm going to assume this is not really a Delphi issue, but a Windows authentication issue.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.