Reputation: 73
I am trying to write new key i.e Mode and assign a a value to it, my system is 32 bit and code is as follows. I am getting an error with the following code as failed to set data for Mode I have tried with all rectification by setting different parameters to reg.Access but no success. I always have to keep reg.Access to KEY_READ as with KEY_WRITE it will not open subkey at reg.OpenKey method.
reg := TRegistry.Create(KEY_READ);
reg.RootKey := HKEY_LOCAL_MACHINE;
if (not reg.KeyExists('\Software\MyApp\appname\')) then
begin
MessageDlg('Key not found! Created now.', mtInformation, mbOKCancel, 0);
end;
reg.Access:= KEY_READ;
openResult := reg.OpenKey('\Software\MyApp\appname\',True);
if not openResult = True then
begin
MessageDlg('Unable to create key! Exiting.', mtError, mbOKCancel, 0);
Exit();
end;
if not reg.KeyExists('Mode') then
begin
reg.WriteString('Mode','trial');
end;
Upvotes: 1
Views: 560
Reputation: 612794
KEY_READ
gives you read access. But that's not enough for you. You pass True
to the CanCreate
argument of OpenKey
and so need the KEY_CREATE_SUB_KEY
access flag. And then you attempt to write a value, which requires the KEY_WRITE
access flag.
Your problem is presumably that your process executes as a user without sufficient rights. You will need to make sure that your program runs with sufficient rights. Usually this means running it as administrator. Use the application manifest to enforce that, or the runas
verb, whichever method is most appropriate for your application.
Upvotes: 4