在 Windows 7 64 位上从 Delphi 7 读取注册表时出现问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2863931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:19:18  来源:igfitidea点击:

Problems reading registry from Delphi 7 on Windows 7 64 bit

windowsdelphi64-bitregistry32bit-64bit

提问by Tofig Hasanov

I think this question was already asked, but I couldn't find a solution which works for me. I use Delphi 7 under Windows 7 Ultimate, 64 bit. Actually I started writing application under 32 bit OS, but then changes PC, so now its 64. In my program I use registration process with Licence ID generated from PROGID value of Windows. Unfortunately it doesn't read the value, seems like it is looking in a different folder, probably redirected by Windows 64 to 32 bit registry. Can you help? This is the code I use:

我认为这个问题已经有人问过了,但我找不到适合我的解决方案。我在 64 位 Windows 7 Ultimate 下使用 Delphi 7。实际上我开始在 32 位操作系统下编写应用程序,但后来更改了 PC,所以现在是 64。在我的程序中,我使用注册过程和从 Windows 的 PROGID 值生成的许可证 ID。不幸的是它没有读取该值,似乎它在不同的文件夹中查找,可能由 Windows 64 重定向到 32 位注册表。你能帮我吗?这是我使用的代码:

 Registry := TRegistry.Create(KEY_READ OR 00);
    try
      Registry.Lazywrite := false;
      Registry.RootKey := HKEY_LOCAL_MACHINE;
      if CheckForWinNT = true then
       Begin
       if not Registry.OpenKeyReadOnly('\Software\Microsoft\Windows NT\CurrentVersion') then showmessagE('cant open');
       end
      else
        Registry.OpenKeyReadOnly('\Software\Microsoft\Windows\CurrentVersion');
      result := Registry.ReadString('ProductID'); 
      Registry.CloseKey;
    finally
      Registry.Free;
    end; // try..finally

Also, do you know how to find whether program is running under 64 bit or 32 bit computer in Delphi 7?

另外,您知道如何在Delphi 7中查找程序是在64位还是32位计算机下运行的吗?

回答by The_Fox

You already asked this question see Registry ReadString method is not working in Windows 7 in Delphi 7.

您已经问过这个问题,请参阅Registry ReadString method is not working in Windows 7 in Delphi 7

So you know that you have to add $0100 in the TRegistry.Create. The problem with your code is that you use OpenKeyReadOnly which resets the Access property of the registry to KEY_READ, so KEY_READ or $0100is lost.

所以你知道你必须在 TRegistry.Create 中添加 $0100。您的代码的问题在于您使用 OpenKeyReadOnly 将注册表的 Access 属性重置为 KEY_READ,因此KEY_READ or $0100丢失了。

Just use OpenKey instead of OpenKeyReadOnly, this won't reset your Access property.

只需使用 OpenKey 而不是 OpenKeyReadOnly,这不会重置您的 Access 属性。

回答by Blorgbeard is out

Here is some Delphi 7 code to detect whether you are running in a 64-bit OS:

以下是一些 Delphi 7 代码,用于检测您是否在 64 位操作系统中运行:

function Is64BitOS: Boolean;
type
  TIsWow64Process = function(Handle:THandle; var IsWow64 : BOOL) : BOOL; stdcall;
var
  hKernel32 : Integer;
  IsWow64Process : TIsWow64Process;
  IsWow64 : BOOL;
begin
  // we can check if the operating system is 64-bit by checking whether
  // we are running under Wow64 (we are 32-bit code). We must check if this
  // function is implemented before we call it, because some older versions
  // of kernel32.dll (eg. Windows 2000) don't know about it.
  // see http://msdn.microsoft.com/en-us/library/ms684139%28VS.85%29.aspx
  Result := False;
  hKernel32 := LoadLibrary('kernel32.dll');
  if (hKernel32 = 0) then RaiseLastOSError;
  @IsWow64Process := GetProcAddress(hkernel32, 'IsWow64Process');
  if Assigned(IsWow64Process) then begin
    IsWow64 := False;
    if (IsWow64Process(GetCurrentProcess, IsWow64)) then begin
      Result := IsWow64;
    end
    else RaiseLastOSError;
  end;
  FreeLibrary(hKernel32);
end;

(Shamelessly plagiarized from myself, here)

(无耻地抄袭我自己,在这里

It looks like you are passing KEY_WOW64_64KEY($0100), so you should be looking at the 64-bit registry branch. If you want to look at the 32-bit registry branch, you should pass KEY_WOW64_32KEY ($0200).

看起来您正在传递KEY_WOW64_64KEY($0100),因此您应该查看 64 位注册表分支。如果要查看 32 位注册表分支,则应传递 KEY_WOW64_32KEY ($0200)。

回答by PhiS

As to your side question, whether it's a 64-bit computer (which is not the same thing as running on a 64-bit OS), have a look at the answers to this question.

至于你的附带问题,它是否是 64 位计算机(这与在 64 位操作系统上运行是不一样的),看看这个问题的答案。

回答by Shadeline

I know this topic is about delphi 7, but I thought I was having problems reading the registry and came here to learn.. I ended up using Key_Read instead of all the extras suggested here.

我知道这个主题是关于 delphi 7 的,但我认为我在阅读注册表时遇到了问题,所以来到这里学习..我最终使用了 Key_Read 而不是这里建议的所有附加功能。

I'm using Delphi 2010 and I used Key_Read just fine.

我使用的是 Delphi 2010,我使用 Key_Read 就好了。

Here is my part of my source that works:

这是我的源代码的一部分:

//Search registry

reg:=TRegistry.Create(KEY_READ);

  with reg do begin

    try
      RootKey := HKEY_LOCAL_MACHINE;
      if OpenKey('\SOFTWARE\Wow6432Node\Blizzard Entertainment\World of Warcraft',false) then
        begin
          memo.Lines.Add('HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Blizzard Entertainment\World of Warcraft - exists');
          wowdir1 := readstring('InstallPath');
          memo.Lines.Add('InstallPath - ' + wowdir1);
          newline;
          closekey;
        end;
      if OpenKey('\SOFTWARE\Wow6432Node\Blizzard Entertainment\World of Warcraft',false) then
        begin
          memo.Lines.Add('HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Blizzard Entertainment\World of Warcraft - exists');
          wowdir2 := readstring('GamePath');
          memo.Lines.Add('GamePath - ' + wowdir2);
          newline;
          wowdir1 := readstring('');
          closekey;
        end;
      if OpenKey('\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\World of Warcraft',false) then
         begin
          memo.Lines.Add'HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\World of Warcraft - exists');
          wowdir3 := readstring('InstallLocation');
          memo.Lines.Add('InstallLocation - ' + wowdir3);
          newline;
          wowdir1 := readstring('');
          closekey;
        end;
    finally
     reg.Free;
    end;

I tried the other Keys that are displayed here and found I don't need KEY_WOW64_64KEY OR KEY_WOW64_32KEY. This must have been a bug that has been corrected in Delphi 2010.

我尝试了此处显示的其他密钥,发现我不需要 KEY_WOW64_64KEY 或 KEY_WOW64_32KEY。这一定是 Delphi 2010 中已更正的错误。