Error when trying to save value in registry
===

Using the code below I try to set a value in the HKEY_LOCAL_MACHINE section of registry but I get an error 'Failed to set data for.....'
If I use HKEY_CURRENT_USER there is no problem.

What might I be missing here.

(The code is not complete, but I think it is the important parts of it)


    type
      TTypWinBits = (Bit32, Bit64);

    function WinBits: TTypWinBits;
    type
      TIsWow64Process = function(Handle:THandle; var IsWow64 : BOOL) : BOOL; stdcall;
    var
      hKernel32 : Integer;
      IsWow64Process : TIsWow64Process;
      IsWow64 : BOOL;
    begin
      Result := Bit32;
      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
            Result := Bit64
          else
            RaiseLastOSError;
        end;
      FreeLibrary(hKernel32);
    end;

    function TFastRegistry.CreateConnection: TRegistry;
    begin
      Result := TRegistry.Create;
      try
        case WinBits of
          Bit32: Result := TRegistry.Create;
          Bit64: Result := TRegistry.Create(KEY_WRITE OR KEY_WOW64_64KEY);
        end;
      except
        on E: exception do
          Result := nil;
      end;
    end;

    procedure TFastRegistry.RunAdd(aDesc, aName: string);
    var
      Reg: TRegistry;
      sRegKey: String;
    begin
      sRegKey := 'Software\Microsoft\Windows\CurrentVersion\Run';
      Reg := CreateConnection;
      with Reg do
        begin
          try
            RootKey := HKEY_LOCAL_MACHINE;
            if not KeyExists(sRegKey) then
              OpenKey(sRegKey, True)
            else
              OpenKey(sRegKey, False);
            WriteString(aDesc, aName);
          finally
            CloseKey;
            Free;
          end;
        end;
    end;

