views:

989

answers:

3

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?

+4  A: 

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

Blorgbeard
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?
Tofig Hasanov
Not too sure.. Have you tried WMI? Eg. here: http://stackoverflow.com/questions/502735/pinvoke-call-for-getting-the-windows-serial-number
Blorgbeard
The_Fox's answer worked for me, seems problem was in the way I opened the Key.
Tofig Hasanov
+6  A: 

You already asked this question see http://stackoverflow.com/questions/2666807/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.

The_Fox
Yes, I totally forgot that :) Thank you, this worked!
Tofig Hasanov
+1  A: 

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.

PhiS