Changes to TRegistry key dont 'hold'

谁都会走 提交于 2019-12-13 22:14:25

问题


From my Win32 app I'm reading and writing HKEY_CURRENT_USER\Software\Embarcadero\BDS\9.0\History Lists\hlRunParameters, that is where the Delphi XE2 IDE writes run-time parameters.

This is the write code:

procedure TFrmCleanIDEParams.BtnWriteClick(Sender: TObject);
var
  lReg      : TRegistry;
  lValue,
  lKey      : String;
  i,
  lNrToWrite,
  lNrRegVals: Integer;
begin
  .....

  lKey := Trim(EdtRegKey.Text);  // '\Software\Embarcadero\BDS\9.0\History Lists\hlRunParameters'
  if lKey = '' then Exit;
  if lKey[1] = '\' then lKey := Copy(lKey,2);

  lReg := TRegistry.Create(KEY_READ or KEY_WRITE);
  lReg.RootKey := HKEY_CURRENT_USER;
  if not lReg.OpenKey(lKey,false) then
    begin
      MessageDlg('Key not found', mtError, mbOKCancel, 0);
      Exit;
    end;
  if not lReg.ValueExists('Count') then
    begin
      MessageDlg('Value ''Count'' not found', mtError, mbOKCancel, 0);
      Exit;
    end;
  lNrRegVals := lReg.ReadInteger('Count');

  lNrToWrite := CLBParams.Items.Count;  // TCheckListBox 
  lReg.WriteInteger('Count',lNrToWrite);
  for i := 0 to lNrToWrite-1 do
  begin
    lValue := 'Item' + IntToStr(i);
    lReg.WriteString(lValue,CLBParams.Items[i]);
  end;
  // Remove the rest:
  for i := lNrToWrite to lNrRegVals-1 do
    lReg.DeleteValue('Item' + IntToStr(i));
end;

Issues:

  1. In RegEdit I see the key contents changing as expected, but the Delphi IDE does not pick up these changes

  2. Some time (reboot?) later the HKEY_CURRENT_USER key has its old values

I think several things could be the reason, but I'm not sure which ones to attack:

  1. I should not use HKEY_CURRENT_USER, but HKEY_USERS. If this is the case, how do I then get the proper S-1-5-etc that I need to use?

  2. It's a Windows 7 64-bit issue, although both my program and the Delphi IDE are 32 bit. (How) do I then need to change the TRegistry.Create?
    I read this Delphi: Read 64-bits registry key from 32-bits process post but that still does not tell me if/when to use different 'access keys'.
    Do I always need to use this KEY_WOW64_64KEY value regardless of my app being 32/64 bit? I see that HKEY_CURRENT_USER\Software is shared, not redirected. (How) do I need to treat these differently?

BTW UAC is off, it would be nice if my code worked with UAC on too.


回答1:


  • The Delphi IDE will only read these values at start up. But you must make sure that you write the registry values after the IDE has finished writing to them.
  • You should be using HKEY_CURRENT_USER.
  • You should not be using an alternate registry view flag because that part of the registry is shared.
  • UAC won't have any impact here because HKEY_CURRENT_USER is writeable for the standard user token.

The only explanation that makes sense is that another process is modifying the values. My guess is that the Delphi IDE is that process.



来源:https://stackoverflow.com/questions/27637365/changes-to-tregistry-key-dont-hold

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!