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

3

我认为这个问题已经被问过了,但是我找不到适合我的解决方案。我在Windows 7 Ultimate 64位下使用Delphi 7编写应用程序。实际上,我最初在32位操作系统下开始编写应用程序,但是后来更换了电脑,所以现在是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 位计算机上运行吗?


你是否收到了异常?是什么异常?你的消息框弹出了吗?这个键存在吗?你已经验证过了吗? - Lieven Keersmaekers
它返回了空字符串,没有任何错误。 - Tofig Hasanov
请查看以下链接:https://dev59.com/pk3Sa4cB1Zd3GeqPtkT8 - Mark Robinson
4个回答

13
您已经问过这个问题,请参见Registry ReadString method is not working in Windows 7 in Delphi 7。因此,您知道必须在TRegistry.Create中添加$0100。您代码的问题在于使用了OpenKeyReadOnly,这会将注册表的Access属性重置为KEY_READ,因此KEY_READ或$0100丢失了。只需使用OpenKey而不是OpenKeyReadOnly即可解决此问题,这不会重置您的Access属性。

9

以下是一些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)。

谢谢,这个可行(虽然我只能在64位机器上测试)。如果Windows也是64位的,你知道如何获取产品ID吗? - Tofig Hasanov
不太确定...你尝试过使用WMI吗?例如:http://stackoverflow.com/questions/502735/pinvoke-call-for-getting-the-windows-serial-number - Blorgbeard
狐狸的回答对我有帮助,似乎问题出在我打开密钥的方式上。 - Tofig Hasanov

1

我知道这个话题是关于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中已经纠正的错误。


1
关于你的附加问题,即是否为64位计算机(这与运行在64位操作系统上并不相同),请查看this question的答案。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接