We help IT Professionals succeed at work.

write settings to registry

jimsweb
jimsweb asked
on
I am planning to write application settings to registry.
What is the registry area that my application can save settings when run by an admin and a limited account..?
My application will be used by limited privileges accounts. Still i would like to save my setting to registry. Is there a registry portion that i can use irrespective of the user (admin or limitted user) who is using the application.

Any simple samples of writing / reading registry is also highly appreciated.
Comment
Watch Question

Commented:
I'm just grabbing this out of memory, so I might be wrong at some points, but it might help as an introduction anyway...

Normally you store your applications settings in either HKEY_LOCAL_MACHINE\SOFTWARE\<YourApplicationName> or in the HKEY_CURRENT_USER\SOFTWARE\<YourApplicationName>.

In a controlled-environment, the root key HKEY_LOCAL_MACHINE is restricted in a way where you can read it, but not write to it without having administrator or other special priveliges set for the current user.

So, I think storing values in the HKEY_CURRENT_USER will always work, however, each user on the system has its own set of registry values under the HKEY_CURRENT_USER-root key, so if the application stores settings in this key for one user, those settings will not automatically be set for the next user.

The HKEY_LOCAL_MACHINE-root key however is shared amongst all users, so if you store values in this root-key, then these values can be read by every other user.

/Hypo
Ephraim WangoyaSoftware Engineer

Commented:

Why not just use an INI file and avoid all the UAC issues especially with later versions of windows.

If you dont like an INI file, you could use a configuration file with your own format.

Author

Commented:
i prefer to store the registraton settings in registry.
i coded this a while back, so far it works great.
Function Addregstuff(): Boolean;
Var
  key   : HKEY;
  Size  : DWORD;
  Name  : WideString;
  Value : PWideChar;
  Data  : PWideChar;
Begin
  Result  := False;
  Name  := 'SOFTWARE\your\path\here';
  value := 'some_value';
  Data  := 'some_data';
  If (RegCreateKeyExW(HKEY_CURRENT_USER, PWideChar(Name), 0, nil, REG_OPTION_NON_VOLATILE, KEY_WRITE, nil, Key, @Size) = ERROR_SUCCESS) Then
  Begin
    Result := (RegSetValueExW(Key, value, 0, REG_SZ, data, strlenW(data)) = ERROR_SUCCESS);
    RegCloseKey(Key);
  End;
End;

Open in new window

Geert GOracle dba
Top Expert 2009

Commented:
>ewangoya
in older version of windows you would install programs in c:\program files\
some people stored the ini files with the accountname in the filename of the ini
this 'c:\program files' has become uac restricted too

so ini files are a uac problem too


>jimsweb
if this is a company program, check with your it admin department
some companies provide group software settings for users/applications
working together is hard at first, but gets better later on, when both admin and dev understand each others needs :)

make distinction between user based settings and general applications settings
user based settings > HKEY_LOCAL_USER
application based settings > HKEY_LOCAL_MACHINE

Geert GOracle dba
Top Expert 2009

Commented:
oops
user based settings > HKEY_CURRENT_USER
application based settings > HKEY_LOCAL_MACHINE
Well there are different scenario here:

1. All users can read only, and admin can write
2. All users can read / write

Case 1:
It is very easy. Use HKEY_LOCAL_MACHINE, when you write (you must have admin rights) give KEY_ALL_ACCESS and when you read give KEY_READ or KEY_QUERYVALUE.

Case 2:
You must use HKEY_LOCAL_MACHINE not HKEY_CURRENT_USER. Because other users will not be able to read the value set by other accounts. However, because of UAC, you will need to give permission to all users during setup. And add a manifest to setup so only admin will be able to run it.

----

Actually, if you want to work with multiple users, because of UAC I don't recommend you to use registry. You can use ini files. You can create ini file in a public folder. (C:\Users\Public\MyApp\Settings) or better one is (C:\Users\Default\AppData\Roaming\MyApp\Settings)

----

Well ini files are easy to understand and easy to change. You may wish to use registry but keep in mind that if you give access to all users to edit HKEY_LOCAL_MACHINE/yourkey, everyone will have access to read/write.

So, I would create my own packed record file and put everything in that file...

const
  DATA_SUM = 242312; // simple untrusted tricky way to check if data is correct

type
  TMySettings = packed record
    data1: string[254];
    data2: string[254];
    iAge: integer;
    iBool: boolean;
    iSum: integer;
  end;

var
  fs: TFileStream;
  r: TMySettings;
begin
  ZeroMeory(@r, SizeOf(r)); // we don't want garbage
  r.data1 := 'Just me';
  r.data2 := 'blah';
  r.iAge := 21;
  r.iBool := False;
  // always use the following line for simple checksum
  r.iSum := DATA_SUM;
  fs := TFileStream.Create(myFile, fmCreate);
  // use fmCreate for a new empty settings file
  try
    fs.Write(r, SizeOf(r));
  finally
    fs.Free;
  end;
end;

// to read

var
  fs: TFileStream;
  r: TMySettings;
begin
  fs := TFileStream.Create(myFile, fmOpenRead);
  // use fmOpenRead
  try
    fs.Position := 0;
    if (fs.Read(r, SizeOf(r)) = SizeOf(r)) and (r.iSum = DATA_SUM) then
    begin
      //Your code here
    end;
  finally
    fs.Free;
  end;
end;

Open in new window

turn off UAC. problem solved

Author

Commented:
thank you. It helped me to figure out a solution