Problems reading registry from Delphi 7 on Windows 7 64 bit

24,288

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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.

Share:
24,288
Tofig Hasanov
Author by

Tofig Hasanov

My main area of interest is Computer Science; however, I have also spent at least several years studying and practicing each of the following fields: 1) Psychology - particularly psychoanalysis, group dynamics and psychology of masses 2) Philosophy - Mostly interested in practical aspects, such as dialectical logic 3) Personal Development - Some might not classify it as a separate field of study, but I have spent more than 10 years doing research and consulting in this area and I know how deep and fascinating this field is. 4) Business Management - Systems and Control theory. Spent a lot of time working on startups as well. 5) Education - Tought Machine Learning, Algorithms and Data Structures and Engineering Design classes to undergraduate students in Qafqaz University. 6) Gaming - I have played lots of games, and I have studied them as well. I believe that we can borrow a lot of tricks from successful games, especially in the area of motivation, and implement them in education and other areas. As for computer science itself, my main focus is Machine Learning, Multi-Agent Systems and Crowdsourcing. It might seem that all these subjects are completely unrelated, but they are all united in the goal to understand various aspects of human mind: how we think, how we learn, how we motivate ourselves, etc. This is what excites me most. I think that to make the world a better place, we should start with understanding and improving ourselves.

Updated on August 11, 2020

Comments

  • Tofig Hasanov
    Tofig Hasanov over 3 years

    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?

    • Lieven Keersmaekers
      Lieven Keersmaekers almost 14 years
      Do you get an exception? What exception? Does you messagebox pops up? Does the key exists? Have you verified it?
    • Tofig Hasanov
      Tofig Hasanov almost 14 years
      It returns empty string, without any error.
    • Mark Robinson
      Mark Robinson almost 14 years
  • Tofig Hasanov
    Tofig Hasanov almost 14 years
    Thank you, this worked (although I only could test it on 64 bit machine). Do you know how to get Product ID of Windows if it is 64 bit also?
  • Blorgbeard
    Blorgbeard almost 14 years
    Not too sure.. Have you tried WMI? Eg. here: stackoverflow.com/questions/502735/…
  • Tofig Hasanov
    Tofig Hasanov almost 14 years
    The_Fox's answer worked for me, seems problem was in the way I opened the Key.