Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / MFC
Article

CXMLProfile - Easy XML Profiles for applications

Rate me:
Please Sign up or sign in to vote.
4.44/5 (6 votes)
26 Feb 20042 min read 131.7K   1.3K   42   34
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


Written By
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: Good idea.. Pin
Emilio Guijarro30-Aug-02 6:36
Emilio Guijarro30-Aug-02 6:36 
Generalc:\Documens and settings\ Pin
Hugo Hallman21-Aug-02 1:30
Hugo Hallman21-Aug-02 1:30 
GeneralRe: c:\Documens and settings\ Pin
Hugo Hallman21-Aug-02 1:34
Hugo Hallman21-Aug-02 1:34 
GeneralRe: c:\Documens and settings\ Pin
Emilio Guijarro21-Aug-02 6:24
Emilio Guijarro21-Aug-02 6:24 
QuestionUnmanaged code? Pin
Miguel Lopes19-Aug-02 9:00
Miguel Lopes19-Aug-02 9:00 
AnswerRe: Unmanaged code? Pin
Giles19-Aug-02 11:16
Giles19-Aug-02 11:16 
AnswerRe: Unmanaged code? Pin
Chris Maunder19-Aug-02 12:40
cofounderChris Maunder19-Aug-02 12:40 
AnswerRe: Unmanaged code? Pin
Emilio Guijarro20-Aug-02 7:37
Emilio Guijarro20-Aug-02 7:37 
My code retrieves, handles and writes the code using the Microsoft´s MSXML DOM parser... I think is fast and powerful, but SAX is still very cool Cool | :cool:

Regards.

"nobody knows it, but you´ve got a secret smile, and you use it only for me"

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

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