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

发布于 2024-09-01 22:56:43 字数 854 浏览 2 评论 0原文

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

 Registry := TRegistry.Create(KEY_READ OR $0100);
    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

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

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:

 Registry := TRegistry.Create(KEY_READ OR $0100);
    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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忱杏 2024-09-08 22:56:43

您已经问过这个问题,请参阅 Registry ReadString该方法在 Windows 7 中的 Delphi 7 中不起作用

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

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

You already asked this question see 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 $0100 is lost.

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

静水深流 2024-09-08 22:56:43

下面是一些 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;

无耻地抄袭我自己,此处

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

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

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).

明媚如初 2024-09-08 22:56:43

至于你的附带问题,是否是 64 位计算机(这与在 64 位操作系统上运行不同),请查看 这个问题

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.

伏妖词 2024-09-08 22:56:43

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

我正在使用 Delphi 2010 并且我使用 Key_Read 就很好。

这是我的源代码中有效的部分:

//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;

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

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.

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文