tags:

views:

20

answers:

1

Is there any standard way to work with application settings in WinAPI? What I'm doing currently is this:

if(!ReadKey(some_setting))
    WriteKey(some_setting, some_setting_setting_default_value)

when the settings dialog is initialized. Then I set the widget states to the corresponding values read from the registry. The problem is that if the application is run for the first time, the default settings can't be read following the above code pattern. One more ReadKey() is necessary to read the just written default settings into the settings variable in my program. This looks a bit clumsy to me. So the question basically is:

  • is there any standard way to work with settings in Win32?
  • and, most importantly, is there any way to set up default application settings during installation, so that there would be code to set the default settings at all? (which I guess is the preferred method, as then you could modify the default application settings without rebuilding it)

Again, this should be pure Win32, no MFC allowed.

Why is this homework? This is question about whether there is an established practice of doing things, not a request to do my job for me. Now I better remove the "university project" phrase from there.

+1  A: 

You could avoid writing hard-coded default values to the registry, and leave the registry empty except when it contains a non-default value:

string ReadRegistry(
  const string& some_setting,
  const string& some_setting_default_value
  )
{
  //try to read user-specified setting from registry
  string rc;
  if (ReadKey(some_setting, rc))
  {
    return rc;
  }
  //else return hard-coded default value, not from registry
  return some_setting_default_value;
}

Alternatively you can write all default values to the registry when the program is installed (before the program is run and before you try to read fro the registry).

Is there any standard way to work with settings in Win32?

No.

Is there any way to set up default application settings during installation

Yes, an installation program can write to the registry.

ChrisW
So I guess the most common way of working with application settings is writing the default values during installation?
Semen Semenych
@Semen Semenych - I guess that's probably a common way; and that two minor disadvantages are: a) Less robust (than using hard-coded default values) if the values are missing from the registry (e.g. deleted by the end-user); b) Less easy to know whether the value in the registry, if it's the default value, was created by the installation program or whether it was explicitly chosen/written by the end-user.
ChrisW