delphi - TRegistry - 为什么有些键可读而其他键不可读?

标签 delphi delphi-xe

我编写了以下代码:

var
  MainForm: TMainForm;

const
  SRootKey = HKEY_LOCAL_MACHINE;
  SKey = 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles';

implementation

{$R *.dfm}

{ TMainForm }

procedure TMainForm.GetKeys(OutList: TStrings);
var
  Reg: TRegistry;
begin
  OutList.BeginUpdate;
  try
    OutList.Clear;

    Reg := TRegistry.Create(KEY_READ);
    try
      Reg.RootKey := SRootKey;
      if (Reg.OpenKeyReadOnly(SKey)) and (Reg.HasSubKeys) then
      begin
        Reg.GetKeyNames(OutList);
        Reg.CloseKey;
      end;
    finally
      Reg.Free;
    end;
  finally
    OutList.EndUpdate;
  end;
end;

procedure TMainForm.btnScanClick(Sender: TObject);
begin
  GetKeys(ListBox1.Items);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  GetKeys(ListBox1.Items);
end;

这似乎没有做任何事情。

我可以验证注册表路径 (Windows 8.1),我什至更改了 SKey 进行测试,没有出现任何问题,但像这样的某些键不会返回任何内容。

我什至尝试以管理员身份从 Windows 运行该程序,但仍然没有任何结果。

还有什么我需要改变的吗?什么会使某些键可读而其他键不可读?

最佳答案

您的进程是 32 位,并且您正在 64 位计算机上运行它。因此,您须遵守 registry redirection .

The registry redirector isolates 32-bit and 64-bit applications by providing separate logical views of certain portions of the registry on WOW64. The registry redirector intercepts 32-bit and 64-bit registry calls to their respective logical registry views and maps them to the corresponding physical registry location. The redirection process is transparent to the application. Therefore, a 32-bit application can access registry data as if it were running on 32-bit Windows even if the data is stored in a different location on 64-bit Windows.

您正在查看的 key

HKLM\SOFTWARE

已重定向。从您的 32 位进程中,打开此 key 的尝试将被重定向到注册表的 32 位 View ,该注册表作为实现详细信息存储在

HKLM\SOFTWARE\Wow6432Node

您在这里尝试执行的是访问注册表的 64 位 View 。为此,您需要access an alternate registry view 。这意味着打开任何键时都会传递 KEY_WOW64_64KEY 键。

在 Delphi 中,您可以通过在 Access 标志中包含 KEY_WOW64_64KEY 或将其包含在传递给构造函数的标志中来实现此目的。

Reg := TRegistry.Create(KEY_READ or KEY_WOW64_64KEY);

最重要的是,对于这个特定的 key ,由于该 key 的注册表安全配置,您需要以管理员权限运行才能打开该 key 。即使您只想阅读它。

关于delphi - TRegistry - 为什么有些键可读而其他键不可读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25093726/

相关文章:

delphi XE多单元命名空间问题

delphi - 允许在顶部绘制主窗体的非模态子窗口 - Delphi

delphi - 将 RFC 822 日期转换为 TDateTime

delphi - 当我尝试从表单 A 显示表单 B 时,为什么编译器会显示 "undeclared identifier"?

delphi - 如何检测 TControl 的右拖动结束?

delphi - 将旧的 Delphi 7 代码迁移到 Delphi XE - 未找到 QForms.dcu

delphi - 如何在Delphi XE2上调试2个dll?

delphi - 在 ADO (ODBC) 中使用日期时间参数会丢失时间部分

delphi - 将变体转换为 double 时出错 [Delphi XE + IBObjects 4.9.12]

delphi - FM 中的 FindWindow