tags:

views:

255

answers:

7

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.

+3  A: 

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.

Péter Török
He could do it by using the user specific settings directories. Like in XP: C:\Docs and Settings\<user>\Application Data\<app name>\...
SB
Especially as there is already an API call to get the user's AppData folder path, too...
Rowland Shaw
Yes, that's what I had in mind.
Péter Török
+1  A: 

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.

C. Ross
+3  A: 

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.

Thomas Matthews
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.
Shane MacLaughlin
-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.
Jon Benedicto
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.
Péter Török
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.
David Thornley
@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.
Shane MacLaughlin
@Shane, if you want one config file for all users, then use the SHGetSpecialFolderPath Win32 API function to get the CSIDL_COMMON_APPDATA directory.
Jon Benedicto
Thanks again, Jon. Will look this up.
Shane MacLaughlin
A: 

Apple uses plist files which basically are XML.

mouviciel
+1  A: 

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.

Jon Benedicto
+3  A: 

boost has a library called Boost.PropertyTree (http://www.boost.org/doc/libs/1_42_0/doc/html/property_tree.html) which is basically an abstraction for settings in a hierarchical structure. It can be serialized to a number of formats, such as XML, JSON and INI. Store these files in %APPDATA% and you're good to go. Storing configuration in the same directory as the executable is not a good idea, as Program Files is not writable by regular users from XP on.

Roel
+1  A: 

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.

Rob