Codrut
Codrut

Reputation: 332

How to resolve "Failed to set data" error when writing a String value in the Windows Registry?

When trying to write a value in the windows registry I get a "Failed to set data for "Value1" " error. I tried running the program as a administrator, creating the key manually and setting the permisions of the key for "Everyone" to full acess. But the error persists

I have used the following code to write the value:

var
   Registry: TRegistry;
 begin
   Registry := TRegistry.Create(KEY_READ);
     Registry.RootKey := HKEY_LOCAL_MACHINE;

     // False because otherwise it would create a new key
     try
     if not Registry.OpenKey(KeyLocation, True) then RaiseLastOSError;
     Registry.WriteString('Value1', 'text123');
    finally
      Registry.Free;
     end;
end;

How do I resolve this issue?

Upvotes: 0

Views: 1741

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 598309

First off, you can't use the parameter-less overload of RaiseLastOSError() with TRegistry, because the Registry API doesn't report errors via the Win32 GetLastError() function. Registry API functions return error codes directly to the caller. TRegistry captures those error codes in its LastError property, which you can then pass to one of the other overloads of RaiseLastOSError(), eg:

if not Registry.OpenKey(KeyLocation, True) then
  RaiseLastOSError(Registry.LastError);

That being said, you are opening the key for read-only access, but are then trying to write to the key. That is likely why WriteString() is failing. You need to use KEY_WRITE instead, or better just KEY_SET_VALUE (since KEY_WRITE includes permissions that WriteString() doesn't need in this particular situation), eg:

var
  Registry: TRegistry;
begin
  Registry := TRegistry.Create(KEY_SET_VALUE{KEY_WRITE});
  try
    Registry.RootKey := HKEY_LOCAL_MACHINE;

    if not Registry.OpenKey(KeyLocation, True) then
      RaiseLastOSError(Registry.LastError);

    Registry.WriteString('Value1', 'text123');
  finally
    Registry.Free;
  end;
end;

That being said, do note that you are trying to write to HKEY_LOCAL_MACHINE, which by default only admins have write access to. So yes, you will have to either run your code with elevated rights, or else adjust the Registry permissions to grant write access to a non-admin user and then run your code as that user.

Also, if you are running on a 64bit machine, you might need to explicitly specify whether you want to access the 32bit (KEY_WOW64_32KEY) or 64bit (KEY_WOW64_32KEY) section of the Registry. See Accessing An Alternate Registry View.

Upvotes: 1

SilverWarior
SilverWarior

Reputation: 8396

You are trying to write registry to HKEY_LOCAL_MACHINE branch part of the registry. This is a system level part of the registry that requires elevated privileges to be accessible.

Or in other words you will have to run your application as administrator or request privileges elevation using manifest file.

EDIT: Also you are opening the registry with KEY_READ access level which is only used for reading the registry but not writing to the registry.

You can read more about Access levels here

Upvotes: 1

Related Questions