Click here to Skip to main content
Click here to Skip to main content

CXMLProfile - Easy XML Profiles for applications

, 26 Feb 2004
Rate this:
Please Sign up or sign in to vote.
A MFC class to store an application's profile in an XML file.

Introduction

Storing and loading configuration data for application is a very common task for programmers, and there are many ways to do it: store information in a file, in the registry, in the Windows common initialization files (system.ini, win.ini, etc...). But information saving and sharing has a new standard called XML.

Windows APIs and MFC have a set of functions which simplify these procedures a lot, storing and retrieving information from win.ini, the registry or any file based on the Windows-INI structure. Currently, handling XML files under Visual C++ is a bit complex, due to the COM nature of the Microsoft XML services.

CXMLProfile is a class which simplifies this work, encapsulating the standard configuration input/output operations in a really simple set of functions.

Using CXMLProfile

When you instantiate a CXMLProfile class and call the loadProfile function, it tries to open the XML file containing the profile specified by the first param of the constructor; if it is not found, a blank profile is created. The data is stored in the file every time you call the saveProfile function.

Now, let's have a look on the class public declarations:

CXMLProfile(LPCTSTR lpszProfileName);

bool writeProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue);
bool writeProfileString(LPCTSTR lpszSection, 
    LPCTSTR lpszEntry, LPCTSTR lpszData);

int getProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault);
LPSTR getProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, 
    LPCSTR lpszDefault, LPSTR lpBuffer, UINT nBufferSize);

bool saveProfile();
bool loadProfile();

~CXMLProfile();

The function prototypes are self-explaining, but I will give a short description:

  • CXMLProfile(LPCTSTR lpszProfileName)

    The only and one constructor, lpszProfileName contains the profile name (your application's name, for example).

  • virtual ~CXMLProfile();

    The only and one destructor; performs a class cleanup.

  • bool writeProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue);

    Writes an integer value into the specified section and entry.

  • bool writeProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszData);

    Writes a string value into the specified section and entry.

  • int getProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault);

    Retrieves an integer value from the specified section and entry.

  • LPSTR getProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCSTR lpszDefault, LPSTR lpBuffer, UINT nBufferSize);

    Retrieves a string value from the specified section and entry.

  • bool saveProfile();

    Saves the current profile to disk.

  • bool loadProfile();

    Loads the current profile from disk. It must be called before reading or writing from the profile.

Comments

To use this code, just include the xmlprofile.h and xmlprofile.cpp into your project and add comsupp.lib to the linker dependencies.

Example code:

#include "xmlprofile.h"

int main()
{
    CXMLProfile xmlProfile("MyApplication");

    xmlProfile.loadProfile();

    // write some stuff in the profile
    xmlProfile.writeProfileString("Owner", "Name", "John");
    xmlProfile.writeProfileInt("Owner", "Age", 20);

    // now let´s retrieve it
    CHAR szName[255];

    xmlProfile.getProfileString("Owner", "Name", "None", szName, 255);
    int nAge = xmlProfile.getProfileInt("Owner", "Age", 0);

    // Warning! the destructor will not save to file any data... 
    // we have to use saveProfile()
    xmlProfile.saveProfile();
}

Updates

  • 21/August/2002 - Now uses the "Documents and Settings\Username" directory to store the profile if the current OS allows it (NT/2000/XP).
  • 18/February/2004 - Now this class is not MFC-dependent and some functions have been added/improved.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Emilio Guijarro
Instructor/Trainer
Netherlands Netherlands
Emilio is a Computer Engineer currently working as software engineer in embedded systems.
 
Main interests are C/C++ programming, algorithmics, compilers, embedded systems, cryptography, and operating systems.

Comments and Discussions

 
GeneralRe: Which header files Pinmemberalerma8-Dec-03 21:35 
GeneralAwesome. :D PinmemberDjof23-May-03 19:35 
GeneralRe: Awesome. :D PinmemberEmilio Guijarro25-May-03 5:14 
Generalexception error PinmemberJazzy Jeff13-Feb-03 9:25 
GeneralRe: exception error PinmemberEmilio Guijarro15-Feb-03 4:02 
GeneralRe: exception error PinsussLJ Whitmire30-Jun-03 5:56 
GeneralGood idea.. PinmemberIan Leslie29-Aug-02 8:44 
GeneralRe: Good idea.. PinmemberEmilio Guijarro30-Aug-02 6:36 
Yes, I forgot those things, I will try to fix them in my free time.
 
Thanks.
 
"nobody knows it, but you´ve got a secret smile, and you use it only for me"
Generalc:\Documens and settings\ Pinmemberhuggeh21-Aug-02 1:30 
GeneralRe: c:\Documens and settings\ Pinmemberhuggeh21-Aug-02 1:34 
GeneralRe: c:\Documens and settings\ PinmemberEmilio Guijarro21-Aug-02 6:24 
QuestionUnmanaged code? PinmemberMiguel Lopes19-Aug-02 9:00 
AnswerRe: Unmanaged code? PinmemberGiles19-Aug-02 11:16 
AnswerRe: Unmanaged code? PinadminChris Maunder19-Aug-02 12:40 
AnswerRe: Unmanaged code? PinmemberEmilio Guijarro20-Aug-02 7:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 27 Feb 2004
Article Copyright 2002 by Emilio Guijarro
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid