I am trying to write a value to the HKLM registry using a component TRegistryin Delphi.
Since I work in Windows 2000 as a standard user (or XP as a standard user or Windows Vista as a standard user or Windows 7 with a standard user), I fully expect that I will not be able to write to the HKEY_LOCAL_MACHINEregistry part :
reg := TRegistry.Create(KEY_WRITE);
try
reg.Access := KEY_WRITE; //sure, set it again, why not
reg.RootKey := HKEY_LOCAL_MACHINE;
if not reg.OpenKey('\Software\Microsoft\SQMClient', True) then
Exit;
reg.WriteString('MachineId', s);
finally
reg.Free;
end;
Unfortunately, WriteStringthrows ERegistryException:
Failed to set data for 'MachineId`
This is to be expected, so I am trying to avoid an exception. I do not see any CanWriteStringor TryWriteStringin TRegistry.
How can I not throw an exception when I try to write to HKLM?
Self-explanatory notes:
- ,
WriteString try-except:
reg := TRegistry.Create(KEY_WRITE);
try
reg.RootKey := HKEY_LOCAL_MACHINE;
if not reg.OpenKey('\Software\Microsoft\SQMClient', True) then
Exit;
try
reg.WriteString('MachineId', s);
except
on E:ERegistryException do
{nothing};
end;
finally
reg.Free;
end;
.
: RTL:
KEY_WRITE = (STANDARD_RIGHTS_WRITE or
KEY_SET_VALUE or
KEY_CREATE_SUB_KEY) and not
SYNCHRONIZE;
MSDN:
KEY_WRITE (0x20006)
STANDARD_RIGHTS_WRITE, KEY_SET_VALUE KEY_CREATE_SUB_KEY.