Click here to Skip to main content
11,488,877 members (77,716 online)
Click here to Skip to main content

A Registry Class

, 6 Aug 2001 169.4K 2K 52
Rate this:
Please Sign up or sign in to vote.
A class that makes it easy to work with the registry
<!-- Download Links --> <!-- Add the rest of your HTML here --> <!-- contents -->

Overview

I created this class to make it easy to work with the Registry. For this I created the CRegistry class.

The CRegistry Class

The CRegistry class has a set of functions that make using the registry easy.

CRegistry::OpenKey

The OpenKey opens the specified registry key.

BOOL OpenKey(enum Keys hKey, LPCTSTR szKey);

Parameters

enum Keys hKey - Handle to any of the following predefined values:

CRegistry::classesRoot

CRegistry::currentUser

CRegistry::localMachine

CRegistry::currentConfig

CRegistry::users

Windows NT/2000: CRegistry::performanceData

Windows 95/98: CRegistry::dynData

LPCTSTR szKey - Pointer to a null-terminated string containing the name of the key to open.

If the function succeeds, this returns TRUE.

See sample

<a name="Sample1">
  CRegistry pReg;
  
  pReg.OpenKey(CRegistry::currentUser, "Entry1\\carlos1");
  CString str = _T("");

  if(pReg.GetValue("SZVAL1", str))
    AfxMessageBox("The Value SZVAL1 don't exists", MB_ICONWARNING);
    
  DWORD dwVal = 0;
  pReg.GetValue("DWVAL", dwVal);

  pReg.GetValue(NULL, str);
  pReg.CloseKey();</a>

CRegistry::CreateKey

The CreateKey function creates the specified registry key. If the key already exists in the registry, the function opens it.

BOOL CreateKey(enum Keys hKey, LPCTSTR szKey);

Parameters

enum Keys hKey - Handle to any of the following predefined values:

CRegistry::classesRoot

CRegistry::currentUser

CRegistry::localMachine

CRegistry::currentConfig

CRegistry::users

Windows NT/2000: CRegistry::performanceData

Windows 95/98: CRegistry::dynData

LPCTSTR szKey - Pointer to a null-terminated string containing the name of the key to open or create.

If the function succeeds, this returns TRUE.

CRegistry::DeleteKey

The DeleteKey function deletes a subkey.

BOOL DeleteKey(enum Keys hKey, LPCTSTR szKey);

Parameters

enum Keys hKey - Handle to any of the following predefined values:

CRegistry::classesRoot

CRegistry::currentUser

CRegistry::localMachine

CRegistry::currentConfig

CRegistry::users

Windows NT/2000: CRegistry::performanceData

Windows 95/98: CRegistry::dynData

LPCTSTR szKey - Pointer to a null-terminated string containing the name of the key to delete.

If the function succeeds, this returns TRUE.

CRegistry::GetValue

The GetValue function retrieves the data for a specified value name from the open registry key.

BOOL GetValue(LPCTSTR lpValueName, CString& strValue);
BOOL GetValue(LPCTSTR lpValueName, DWORD& dwValue);

Parameters

LPCTSTR lpValueName - Pointer to a null-terminated string containing the name of the value to get

CString& strValue - Pointer to a buffer that receives the value's data

DWORD& dwValue - Pointer to a buffer that receives the value's data

If the function succeeds, this returns TRUE.

See sample

CRegistry::SetValue

The SetValue function sets the data and type of a specified value under a registry key.

BOOL SetValue(LPCTSTR lpValueName, LPCTSTR lpData);
BOOL SetValue(LPCTSTR lpValueName, DWORD dwValue);

Parameters

LPCTSTR lpValueName - Pointer to a null-terminated string containing the name of the value to set

CString& strValue - Pointer to a buffer containing the data to be stored with the specified value name

DWORD& dwValue - Pointer to a buffer containing the data to be stored with the specified value name

If the function succeeds, this returns TRUE.

See sample

<a name="Sample2">pReg.OpenKey(CRegistry::currentUser, "Entry1\\carlos1");

pReg.SetValue("SZVAL", "STRVAL");
pReg.SetValue(NULL, "default");
pReg.SetValue("DWVAL", 34); 

pReg.CloseKey();</a>

CRegistry::DeleteValue

The DeleteValue function removes a named value from the specified registry key

BOOL DeleteValue(LPCTSTR lpValueName);

Parameters

LPCTSTR lpValueName - Pointer to a null-terminated string that names the value to remove.

If the function succeeds, this returns TRUE.

See sample

<a name="Sample3">  CRegistry pReg;

  pReg.OpenKey(CRegistry::currentUser, "Entry1\\carlos1");

  pReg.DeleteValue("SZVAL");	
  pReg.CloseKey();</a>

CRegistry::CloseKey

The CloseKey function closes the registry key.

void CloseKey();

See sample

Updates

04 August 2001: Version 1.0 Released.

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

Share

About the Author

Carlos Antollini
Architect Citigroup
Argentina Argentina
Carlos Antollini is a software engineer working on Object Oriented, Visual C++, MFC, COM, ATL, ADO, Internet technologies and Business Intelligence.
Carlos is originally from Argentina, he was living for several years in Fort Lauderdale, Florida, working for Citibank. Then he started his own business.
Carlos is the creator of piFive[^], a family of BI Analytic Platform software, that it deals next to, latinsys[^], his partner in businesses...
Currently he is sharing his passion for project management and BI at Citigroup.

Comments and Discussions

 
BugIn function CRegistry::GetValue(LPCTSTR lpValueName, DWORD& dwValue) 64-bit pointer truncation Pin
Andreas Schniertshauer8-Apr-15 4:05
memberAndreas Schniertshauer8-Apr-15 4:05 
BugErrors compiling UNICODE version with VS2010 Pin
C++ Hacker30-Oct-13 2:51
memberC++ Hacker30-Oct-13 2:51 
BugOpenKey does not work on Windows 7 Pin
Thomas Haase28-Sep-11 0:32
memberThomas Haase28-Sep-11 0:32 
GeneralCannot read key from class Registry Pin
Larry Mills Sr3-Jun-09 16:39
memberLarry Mills Sr3-Jun-09 16:39 
Questionvery useful class Pin
kryptun16-Dec-08 11:50
memberkryptun16-Dec-08 11:50 
Questionerror C2872: 'ULONG_PTR' : ambiguous symbol Pin
Pooh Agarwal17-Oct-06 21:50
memberPooh Agarwal17-Oct-06 21:50 
AnswerRe: error C2872: 'ULONG_PTR' : ambiguous symbol Pin
Vider22-Nov-07 4:33
memberVider22-Nov-07 4:33 
Generalbug fix for VC7/8 [modified] Pin
waldermort4-Oct-06 9:16
memberwaldermort4-Oct-06 9:16 
GeneralThank you Pin
msanchezv30-Jan-06 2:25
membermsanchezv30-Jan-06 2:25 
GeneralMemory leak Pin
jmaccelari19-Jul-04 21:45
sussjmaccelari19-Jul-04 21:45 
In the method BOOL CRegistry::GetValue(LPCTSTR lpValueName, CString& strValue), the variable lpstrValue is malloc()'ed but not free()'ed. This results in a memory leak each time it is called.

Solution is to add:

free(lpstrValue);

just before the return.

Even better - replace the malloc with new and the free with delete (this is supposedly C++ after all!).
Generalcan't get it to work (noob!) Pin
E-Male19-May-04 1:13
memberE-Male19-May-04 1:13 
GeneralAlmost perfect! Pin
Terry O'Nolley15-May-04 16:23
memberTerry O'Nolley15-May-04 16:23 
GeneralRe: Almost perfect! Pin
Carlos Antollini15-May-04 16:42
memberCarlos Antollini15-May-04 16:42 
Questionbug with 2000pro ? Pin
alex-pcom22-Jan-04 3:17
memberalex-pcom22-Jan-04 3:17 
AnswerRe: bug with 2000pro ? Pin
Carlos Antollini22-Jan-04 4:36
memberCarlos Antollini22-Jan-04 4:36 
GeneralRe: bug with 2000pro ? Pin
Shikamaru27-Apr-04 22:03
memberShikamaru27-Apr-04 22:03 
GeneralSource updated for VS7 Pin
ralfoide30-Sep-03 12:42
memberralfoide30-Sep-03 12:42 
QuestionShould RegFlushKey be used? Pin
Baz18-Jul-03 5:53
memberBaz18-Jul-03 5:53 
GeneralVC7 compiler errors/warnings Pin
C++ Hacker23-Jan-03 4:17
memberC++ Hacker23-Jan-03 4:17 
GeneralEnumKey Pin
Michael Ehehalt8-Dec-02 20:41
memberMichael Ehehalt8-Dec-02 20:41 
GeneralRe: EnumKey Pin
Bruno Scopinho4-Jan-07 13:31
memberBruno Scopinho4-Jan-07 13:31 
Generalbug in GetValue Pin
Michael Ehehalt8-Dec-02 20:31
memberMichael Ehehalt8-Dec-02 20:31 
Generalbug in the readstring Pin
Thomas George1-Oct-02 15:08
memberThomas George1-Oct-02 15:08 
GeneralProblem with Hkey local machine Pin
Zvika Vered18-Aug-02 7:11
sussZvika Vered18-Aug-02 7:11 
GeneralRe: Problem with Hkey local machine Pin
Raimon814-Nov-05 3:34
memberRaimon814-Nov-05 3:34 
GeneralProblem with .Net! Pin
George Clarence7-Aug-02 0:12
memberGeorge Clarence7-Aug-02 0:12 
GeneralRe: Problem with .Net! Pin
Carlos Antollini7-Aug-02 4:47
memberCarlos Antollini7-Aug-02 4:47 
GeneralRe: Problem with .Net! Pin
George Clarence7-Aug-02 5:32
memberGeorge Clarence7-Aug-02 5:32 
GeneralRename key Pin
ZhangFei3-Aug-02 7:19
sussZhangFei3-Aug-02 7:19 
GeneralSub-key delete Pin
Anonymous16-Jul-02 23:36
sussAnonymous16-Jul-02 23:36 
GeneralC1010 Error Pin
Matt Newman24-Nov-01 9:34
memberMatt Newman24-Nov-01 9:34 
GeneralRe: C1010 Error Pin
Carlos Antollini26-Nov-01 4:52
memberCarlos Antollini26-Nov-01 4:52 
GeneralRe: C1010 Error Pin
Matt Newman26-Nov-01 8:56
memberMatt Newman26-Nov-01 8:56 
GeneralRe: C1010 Error Pin
Carlos Antollini26-Nov-01 9:53
memberCarlos Antollini26-Nov-01 9:53 
GeneralRe: C1010 Error Pin
Matt Newman26-Nov-01 16:42
memberMatt Newman26-Nov-01 16:42 
GeneralRe: C1010 Error Pin
Matt Newman13-Dec-01 11:57
memberMatt Newman13-Dec-01 11:57 
GeneralRe: C1010 Error Pin
baur zeng30-Oct-02 15:38
memberbaur zeng30-Oct-02 15:38 
GeneralRe: C1010 Error Pin
Matt Newman30-Oct-02 17:36
memberMatt Newman30-Oct-02 17:36 
GeneralRe: C1010 Error Pin
Anonymous12-Apr-02 9:35
memberAnonymous12-Apr-02 9:35 
GeneralRe: C1010 Error Pin
Carlos Antollini12-Apr-02 9:52
memberCarlos Antollini12-Apr-02 9:52 
Generalmemory leak Pin
Samuel23-Nov-01 0:42
memberSamuel23-Nov-01 0:42 
GeneralRe: memory leak Pin
Carlos Antollini23-Nov-01 3:34
memberCarlos Antollini23-Nov-01 3:34 
GeneralRe: memory leak Pin
Samuel26-Nov-01 3:53
memberSamuel26-Nov-01 3:53 
GeneralRe: memory leak Pin
Anonymous30-Oct-02 14:58
sussAnonymous30-Oct-02 14:58 
GeneralCannot read ReadOnly keys Pin
Hans-Georg Ulrich8-Aug-01 7:43
memberHans-Georg Ulrich8-Aug-01 7:43 
GeneralSuggestions Pin
Jörgen Sigvardsson8-Aug-01 1:50
memberJörgen Sigvardsson8-Aug-01 1:50 
GeneralRe: Suggestions Pin
James Pullicino9-Aug-01 3:04
memberJames Pullicino9-Aug-01 3:04 
GeneralRe: Suggestions Pin
Jörgen Sigvardsson9-Aug-01 3:13
memberJörgen Sigvardsson9-Aug-01 3:13 

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 | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 7 Aug 2001
Article Copyright 2001 by Carlos Antollini
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid