8

I have a C++ MFC app that stores all of its system wide configuration settings to the registry. Previously, we used .INI files, and changed over to using the registry some years back using

SetRegistryKey("MyCompanyName");

We now get regular support calls from users having difficulty migrating from PC and Windows version to another, and I've come to the conclusion that using the registry causes many more problems than it solves. I don't particularly want to go back to .INI files either as I'd like to store settings per user, so the plan is to write my own versions of the GetProfile... and SetProfile... functions to work with a database. Has anybody done this already, and does anyone know of an existing drop in replacement library for registry usage that wouldn't require too much code modification? Ideally, I'd also like it to have options to read initial values from the registry to support existing users.

2
  • 1
    You are trying to reduce support calls by switching to a database? Did you ever support one? Feb 19, 2010 at 15:43
  • 1
    Fair enough, probably a simple XML file will suffice, rather than a database
    – SmacL
    Feb 19, 2010 at 16:52

7 Answers 7

5

Boost has a library called Boost.PropertyTree that abstracts settings into a hierarchical structure. It can be serialized to a number of formats, such as XML, JSON, and INI.

Store these files in %APPDATA%. Storing them in the same directory as the executable is not a good idea, as %PROGRAMFILES% is not writable by regular users from XP on.

1
  • This would be the solution for me, except Boost.PropertyTree uses RapidXML, which does not handle UTF-8 XML files. If it instead used pugixml, then all would be good! Apr 6, 2011 at 23:44
4

I suggest moving over to an XML file in the same location as the executable. One benefit is that XML is portable across non-Windows machines (and even between Windows versions).

Edit: The idea behind an XML configuration file in the same location as the executable is that the configuration file is for program configurations (not user) and for encapsulation / data hiding (not to pollute global file system directories). User configuration data should be in another file in a directory of the user or user's choice.

7
  • That's very much in line with what I'm thinking at the moment, both XML and exe folder. I'm surprised there isn't something out there already, although google didn't show much of interest.
    – SmacL
    Feb 19, 2010 at 15:47
  • 9
    -1 Unless your application is not going to be installed to the Program Files directory, you are going to run into permissions issues with writing the XML file. Windows non-admin accounts, and installations with UAC enabled block writing to Program Files. Config files should be located in the user's application data directory. Feb 19, 2010 at 15:59
  • If it's in the same location as the executable, it exists in a single instance only. Of course you can store all user settings in that single file (and it's definitely easier to handle this in XML than in INI)... still it's not ideal IMHO. Feb 19, 2010 at 16:08
  • In our in-house apps, there's a hierarchy that starts with a standard .ini file in the same location as the executable, and nobody changes this. There's a few other possible locations that will override that, and some of them the user can alter as he or she desires. So, if a user just starts using the app, there's reasonable configuration, but the user can customize. Feb 19, 2010 at 16:38
  • @Jon, thanks for that. It's exactly this type of UAC nonsense that I'm trying to avoid. I'll use a different folder, although will probably stick with a single XML file for all users and avoid the Windows API entirely.
    – SmacL
    Feb 19, 2010 at 16:49
3

You could store a separate INI file for each user though - although I don't know if this is directly supported by WinAPI. At least a lot of applications do this (e.g. TotalCommander).

Update: apparently it is supported by the API.

3
  • 7
    He could do it by using the user specific settings directories. Like in XP: C:\Docs and Settings\<user>\Application Data\<app name>\...
    – NG.
    Feb 19, 2010 at 15:28
  • Especially as there is already an API call to get the user's AppData folder path, too... Feb 19, 2010 at 15:33
  • Yes, that's what I had in mind. Feb 19, 2010 at 16:03
2

You can build a system similar to .Net's Isolated Storage, where a configuration file is located in a safe directory, then you can use GetPrivateProfileString to access it.

1

You could switch to a XML/INI configuration file that's stored in the user's application data folder, or possibly even the My Documents folder. This way the user can copy the file over to preserve their settings.

1

I stopped using the registry with my last desktop app project and use an INI file stored in the user's %APPDATA% folder instead. If you're only storing strings and integers then an INI file does the job and is easy to edit in Notepad.

0

Apple uses plist files which basically are XML.

Not the answer you're looking for? Browse other questions tagged or ask your own question.