Click here to Skip to main content
13,259,771 members (53,911 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 1 Jul 2003

Storing an array of properties in the Windows Registry

, 1 Jul 2003
Rate this:
Please Sign up or sign in to vote.
MFC and Win API don't have functions that store arrays in the Windows Registry. This article describes the class based on MFC CArray class that does this task.

Sample Image - RegistryArray.gif


MFC and Win API don’t have functions that store arrays in the Windows registry. This article describes the class based on MFC CArray class that solves this task. I used this class in the PC Atomic Sync project for storing the NTP (Network Time Protocol) servers list.

MFC registry functions

We use the registry to store program parameters.

If you use MFC, you must set the registry key for your program. MFC SDI and MDI wizard generates correspondent string in the App::InitInstance() function. In dialog based projects you can insert it manually.



After calling this function, all your parameters will be stored in the registry path: HKEY_CURRENT_USER/Software/RegistryArray. As a result, in such multi-user systems as Win NT/XP/2K, every user will have his/her own set of parameters.

There are four MFC functions for writing/reading registry parameters: GetProfileInt, GetProfileString, WriteProfileInt, and WriteProfileString. They are members of the CWinApp class. The "Get" functions have a nice parameter for setting the default property. If the registry has not been yet initialized by your program, this parameter is used instead of the registry value.


// Writing integer value nSomeValue to the
// section SomeSection under name SomeValue.
                         _T("SomeValue"), nSomeValue );
// Reading nSomeValue from the key SomeValue of the section SomeSection.
// If the key is absent the variable will be assigned by 10.
int nSomeValue = AfxGetApp()->GetProfileInt(_T("SomeSection"), 
                                           _T("SomeValue"), 10);

Storing arrays in the registry

Usually we store data in files and program parameters in the registry. But it is possible that some parameters can look like an array. For example, our application has some windows and we want to store their placements. Every user would like a private placement. One solution is to create some file with parameters, the other is to store an array in the registry. There is no such standard function but the solution is simple. We can store the array size as an integer value and the array elements using special key names that include numbers of elements. For example, array_0, array_1, array_2 ...

CRegArray template class

It is good to have a common solution for any array that we want to store in the registry. So our class must be a template that receives any type. Mostly we work with classes and need to store records with data of different types. I created the CRegArray class template that is based on the CArray class template. There is only one demand to the class that is used as an array element. It must include two functions: CString GetAsString() const and void Parse(CString& sStr). I.e. the data converts to string before storing and restores from the string after reading from the registry.

Class source

#include <afxtempl.h>
template <class T, class F>
class CRegArray: public CArray<T, F>
     CRegArray(const CString sSection, const CString sArrayName);
     void FetchRegistry();
     void PersistRegistry();
     const CString m_sSection;
     const CString m_sArrayName;
     const CString m_sCounterName;
template <class T, class F>
inline CRegArray<T,T&>::CRegArray(const CString sSection, 
          const CString sArrayName):CArray<T,F>(),
template <class T, class F>
inline void CRegArray<T,T&>::FetchRegistry()
     int nCounter = 
       AfxGetApp()->GetProfileInt(m_sSection, m_sCounterName, 0);
     for(int i = 0; i < nCounter; i++){
          CString sElmName;
          sElmName.Format("%s %d",m_sArrayName, i);
          CString sTmp = 
             sElmName, "");
          CSomeClass clTmp;
template <class T, class F>
inline void CRegArray<T,T&>::PersistRegistry()
                     m_sCounterName, GetSize() );
     for(int i=0;i<GetSize();i++){
          CString sElmName;
          sElmName.Format("%s %d",m_sArrayName, i);
                          sElmName, (*this)[i].GetAsString() ); 

The class is simple. I added only two functions to the CArray class. They are FetchRegistry() and PersistRegistry(). The constructor has two parameters: the section name and the array name. So, it is possible to store some arrays in one section.

Demo project

The demo project is a dialog based project. To demonstrate the use of CRegArray I created CSomeClass. It includes string, double and integer fields. The object is entered in the dialog form and is added to the array by pressing the "Add Object" button. All objects are shown in the list control. "Delete Object" button removes the last object from the list. "Store into the registry" button writes the array into registry. This array is loaded in the OnInitDialog() function.

String converting of CSomeClass fields is realized in the following way:

const int nBuffSize(1024);
CString CSomeClass::GetAsString() const
     CString sStr(' ', nBuffSize);

     sStr.Format("%s\n%d\n%e", m_sStrValue, m_nIntValue,m_nDoubleValue);
     return sStr;
void CSomeClass::Parse(CString& sStr)
     strstream sStream((char*)(LPCTSTR)sStr, nBuffSize);
     char buff[nBuffSize];
     m_sStrValue = buff;
     sStream >> m_nIntValue;
     sStream >> m_nDoubleValue;



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

Ukraine Ukraine
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralHidden Regitry Data Value Pin
sbuonocore19-Jul-05 4:33
membersbuonocore19-Jul-05 4:33 
QuestionREG_MULTI_SZ? Pin
Peter Ritchie9-Jul-03 4:20
memberPeter Ritchie9-Jul-03 4:20 
There's no mention of why REG_MULTI_SZ does not fit the bill. It allows the storage of a standard Windows null-deliminiated array of strings. Although limited to strings, any value may be represented as a string.

While REG_MULIT_SZ is not supported entirely buy RegEdit.exe, REG_MULTI_SZ does an excellent job of storing arrays in the registry, and it's merits and downfalls should be addressed in the article.

-- Peter
Questionhow about using CArchive Pin
Liorsh7-Jul-03 21:24
memberLiorsh7-Jul-03 21:24 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 2 Jul 2003
Article Copyright 2003 by Brigsoft
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid