CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2004
    Posts
    119

    Overload WriteProfileXXX/GetProfileXXX to store settings in XML instead of registry?

    Does anyone know of an MFC class that overloads the CWinApp WriteProfileXXX/GetProfileXXX functions so that settings are stored in XML rather then the registry? I know there is a way to use an INI instead, but that seems like it's depreciated. From what I've read .NET has a way to write settings to XML, but I don't see anything similar for MFC.

    If there isn't a ready made class out there I'll just do it myself, but I was hoping to save myself the effort.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Overload WriteProfileXXX/GetProfileXXX to store settings in XML instead of regist

    What is the reason to NOT use the registry?
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2004
    Posts
    119

    Re: Overload WriteProfileXXX/GetProfileXXX to store settings in XML instead of regist

    Our program is a complex video editing/processing app with a TON of settings. When diagnosing an issue it's hard to tell if it's a bad setting or an actual bug causing the issue. Usually our first recommendation to a user when diagnosing an issue is to uninstall/reinstall, but a lot of them are reluctant to do that because it deletes all their settings. We also get a lot of requests to make the settings portable, so they can be copied to multiple machines. Storing the settings in an XML seems like it would solve both of those issues. We could have users "reset" their settings by simply renaming the XML file for testing and the users that want to copy settings could simply copy the XML between machines.

    I actually recently added an import/export option for the second case, and it sort of helps the first case too as it allows users to back up their settings before uninstalling, but it's a bit more cumbersome.

    Is there a reason why not to use XML instead of the registry? Is XML going to be significantly slower or more prone to error?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Overload WriteProfileXXX/GetProfileXXX to store settings in XML instead of regist

    Quote Originally Posted by Dan203 View Post
    ...
    Is there a reason why not to use XML instead of the registry? Is XML going to be significantly slower or more prone to error?
    I may be wrong, but in my opinion, it's like reinventing the wheel...
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2004
    Posts
    119

    Re: Overload WriteProfileXXX/GetProfileXXX to store settings in XML instead of regist

    Quote Originally Posted by VictorN View Post
    I may be wrong, but in my opinion, it's like reinventing the wheel...
    Perhaps, but obviously I'm not the only one who wants this functionality as it appears to be pretty common for .NET applications to use XML files for settings rather then the registry.

    If anyone knows of a class that already does this I'd love to get a link. If not I guess I'll roll my own and see how it goes.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Overload WriteProfileXXX/GetProfileXXX to store settings in XML instead of regist

    I know there is a way to use an INI instead, but that seems like it's depreciated
    What's wrong with .ini files? The Write/GetPrivatexxx() API's haven't been depreciated and aren't going away any time soon - and if at some distant time in the future Microsoft decides to withdraw them, it's fairly easy to replicate them.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Mar 2004
    Posts
    119

    Re: Overload WriteProfileXXX/GetProfileXXX to store settings in XML instead of regist

    Quote Originally Posted by 2kaud View Post
    What's wrong with .ini files? The Write/GetPrivatexxx() API's haven't been depreciated and aren't going away any time soon - and if at some distant time in the future Microsoft decides to withdraw them, it's fairly easy to replicate them.
    INI files are harder to edit by hand and less structured. If I'm going to write a text file anyway XML just seems more modern and easier to read/edit if needed.

    I'm a little concerned about the performance/memory hit though. I need to do some testing and see if it has a significant impact. If it does then I'll just have to guide customers to the import/export feature I already have.

  8. #8
    Join Date
    May 2007
    Posts
    811

    Re: Overload WriteProfileXXX/GetProfileXXX to store settings in XML instead of regist

    I have used TinyXML as my xml reader and writer. It does not have validation built in, but it's small, fast, fairly efficient and easy to add to your project.

    I want to make couple comments regarding other bits of your post. You mention to prefer xml over ini. Over many years in my career, where editing xml files is fairly common, like in notepad type of the program is not that fun. xml files are very noisy and it's not obvious of data structure. This sentiment is not only mine, but many other colleges would agree with it.

    ini files are easy to read and mostly to the point. And mostly user settings are key value pair, so ini lends well to it. If user ever wants to edit that file manually (like in notepad), then looking at the ini file would be easier over xml. Try to load fairly complex xml file into your windows notepad (no color syntax, 8 spaces per tab) and you will see that it's not easy to read. Now, try to add or change settings in it. xml is much less forgiving for typos, then ini for example.

    Of course, it is your call, I just wanted to convey my and many other people experience with xml.

    For the performance, I don't think it matters. It is not like you are going to be saving or loading hundreds of times per second setting file. You read it at startup once, cache the values and move on, and then save it when user changes the setting, or on exit the app.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Overload WriteProfileXXX/GetProfileXXX to store settings in XML instead of regist

    If I'm going to write a text file anyway XML just seems more modern and easier to read/edit if needed
    I agree with STLDude in post #8. We've experimented with various methods and where use of the registry (which should be the first choice) is not considered appropriate then we think .ini files are the next best.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Overload WriteProfileXXX/GetProfileXXX to store settings in XML instead of regist

    Quote Originally Posted by Dan203 View Post
    I actually recently added an import/export option for the second case, and it sort of helps the first case too as it allows users to back up their settings before uninstalling, but it's a bit more cumbersome.
    To make it less cumbersome, you may backup the settings on any settings change, as well as program start and shutdown.

    Is there a reason why not to use XML instead of the registry? Is XML going to be significantly slower or more prone to error?
    It's absolutely up to you. Storing settings in the registry is standard and recommended approach. The registry key is exportable/importable, and thus portable, yet again, absolutely (hint: reg.exe). So ultimately it's a matter of your personal preferences and skills.
    Best regards,
    Igor

  11. #11
    Join Date
    Mar 2004
    Posts
    119

    Re: Overload WriteProfileXXX/GetProfileXXX to store settings in XML instead of regist

    Yeah after thinking about this more I'm not sure the minor benefit this provides over the import/export we already have is really worth the effort. I added this to my ToDo list before we had the import/export, and back then it seemed much more important. But now the benefits over what we already have seem minimal. I was just tackling some stuff on my ToDo list yesterday and came across this which is what prompted this thread. I think I'm going to skip it for now.

    As for XML vs INI... I've been writing HTML by hand since the mid 90s, so to me XML is second nature and seems completely intuitive. INI files seem unstructured to me. With XML you can structure the tags to match the structure of the settings screen (which has multiple tabs) so it seems more intuitive to edit by hand. That being said I seriously doubt many (any?) of our users would be editing this file by hand anyway. It's mainly going to be used for backup and support.

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Overload WriteProfileXXX/GetProfileXXX to store settings in XML instead of regist

    Quote Originally Posted by Dan203 View Post
    Yeah after thinking about this more I'm not sure the minor benefit this provides over the import/export we already have is really worth the effort. I added this to my ToDo list before we had the import/export, and back then it seemed much more important. But now the benefits over what we already have seem minimal. I was just tackling some stuff on my ToDo list yesterday and came across this which is what prompted this thread. I think I'm going to skip it for now.

    As for XML vs INI... I've been writing HTML by hand since the mid 90s, so to me XML is second nature and seems completely intuitive. INI files seem unstructured to me. With XML you can structure the tags to match the structure of the settings screen (which has multiple tabs) so it seems more intuitive to edit by hand. That being said I seriously doubt many (any?) of our users would be editing this file by hand anyway. It's mainly going to be used for backup and support.
    Used to be that storing settings in the registry was the way to go. Now-a-days, not so much (even if MFC supports this approach).

    Xml is perfectly valid (even JSON). Not that it is available, but in .Net there is an XmlSerializer class that allows you to read from xml and it turns it into a class instance (where xml elements and/or attributes become class properties). This is called deserialization and you can serialize also where a class instance is turned into xml. It's too bad this isn't available for native C++ because it's really slick.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured