如何从注册表HKEY_LOCAL_MACHINE中读取?

huangapple go评论67阅读模式
英文:

How do I read from registry HKEY_LOCAL_MACHINE?

问题

我在尝试从注册表中的根键HKEY_LOCAL_MACHINE获取任何读取时遇到了困难。

var
  reg : TRegistry;
begin
  Reg := TRegistry.Create();
  Reg.RootKey := HKEY_LOCAL_MACHINE;
  if Reg.OpenKey('\SOFTWARE\Microsoft\Microsoft SQL Server',false) then
  begin
    Result := reg.ReadString('InstalledInstances');
    ....

我试图从InstalledInstances键中获取值:

如何从注册表HKEY_LOCAL_MACHINE中读取?

起初我以为代码不起作用。然后我尝试读取HKEY_LOCAL_MACHINE下的不同键,但结果相同。我是否漏掉了某种权限设置?

如果我使用相同的代码来读取HKEY_CURRENT_USER下的内容,那么一切正常,我可以获取键的值。但是在HKEY_LOCAL_MACHINE下,我无法打开任何键。

这也不允许我打开:

Reg := TRegistry.Create(KEY_ALL_ACCESS);
英文:

I'm having a hard time getting any reads from the registry with the root key HKEY_LOCAL_MACHINE.

var
  reg : TRegistry;
begin
  Reg := TRegistry.Create();
  Reg.RootKey := HKEY_LOCAL_MACHINE;
  if Reg.OpenKey('\SOFTWARE\Microsoft\Microsoft SQL Server',false) then
  begin
    Result := reg.ReadString('InstalledInstances');
    ....

I'm trying to get the values from the InstalledInstances Key:

如何从注册表HKEY_LOCAL_MACHINE中读取?

At first I thought the code doesn't work. I then tried reading different keys under HKEY_LOCAL_MACHINE with the same results. Is there some kind of permissions thing I'm not getting?

If I use the same code for reading under HKEY_CURRENT_USER, that works fine and I get the key values. But under HKEY_LOCAL_MACHINE I can't open any key.

This also doesn't allow me to open:

Reg := TRegistry.Create(KEY_ALL_ACCESS);

答案1

得分: 2

当从HKEY_LOCAL_MACHINE根节点读取值时,您需要使用TRegistry.OpenKeyReadOnly()或更改TRegistry.Access属性为KEY_READKEY_EXECUTE。否则,由于安全设置,您的应用程序将被用户帐户控制(UAC)阻止访问注册表,因为需要提升的权限才能修改HKEY_LOCAL_MACHINE根节点下的任何键。

另一种方法是以提升的权限启动您的应用程序(以管理员身份运行)。

这在读取或修改HEY_CURRENT_USER根节点下的注册表时不是必需的(尽管仍然是可取的),因为当前用户已经具备完全修改自己的注册表键所需的权限。

编辑:至于读取或写入REG_MULTI_SZ注册表值,我建议您查看使用Delphi读取和写入REG_MULTI_SZ类型注册表项

英文:

When reading values from the HKEY_LOCAL_MACHINE root node, you need to use TRegistry.OpenKeyReadOnly() or change the TRegistry.Access property to either KEY_READ or KEY_EXECUTE. Otherwise, access to the Registry by your application will be blocked by UAC (User Account Control), as it is required to have elevated privileges in order to modify any key under the HKEY_LOCAL_MACHINE root node due to security settings.

Another way is to start your application with elevated privileges (Run As Administrator).

This is not required (although it is still desirable) when reading or modifying the Registry under the HEY_CURRENT_USER root node, since the current user already has the needed privileges in order to fully modify its own Registry keys.

EDIT: As for reading or writing REG_MULTI_SZ registry values I recommend you check Read and Write registry entry of type REG_MULTI_SZ using Delphi

huangapple
  • 本文由 发表于 2020年1月6日 02:34:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59603008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定