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

A Registry Class

, 6 Aug 2001
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

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

 
BugErrors compiling UNICODE version with VS2010 PinmemberC++ Hacker30-Oct-13 1:51 
BugOpenKey does not work on Windows 7 PinmemberThomas Haase27-Sep-11 23:32 
GeneralCannot read key from class Registry PinmemberLarry Mills Sr3-Jun-09 15:39 
Questionvery useful class Pinmemberkryptun16-Dec-08 10:50 
Questionerror C2872: 'ULONG_PTR' : ambiguous symbol PinmemberPooh Agarwal17-Oct-06 20:50 
AnswerRe: error C2872: 'ULONG_PTR' : ambiguous symbol PinmemberVider22-Nov-07 3:33 
Generalbug fix for VC7/8 [modified] Pinmemberwaldermort4-Oct-06 8:16 
GeneralThank you Pinmembermsanchezv30-Jan-06 1:25 
GeneralMemory leak Pinsussjmaccelari19-Jul-04 20:45 
Generalcan't get it to work (noob!) PinmemberE-Male19-May-04 0:13 
GeneralAlmost perfect! PinmemberTerry O'Nolley15-May-04 15:23 
GeneralRe: Almost perfect! PinmemberCarlos Antollini15-May-04 15:42 
Questionbug with 2000pro ? Pinmemberalex-pcom22-Jan-04 2:17 
AnswerRe: bug with 2000pro ? PinmemberCarlos Antollini22-Jan-04 3:36 
GeneralRe: bug with 2000pro ? PinmemberShikamaru27-Apr-04 21:03 
GeneralSource updated for VS7 Pinmemberralfoide30-Sep-03 11:42 
QuestionShould RegFlushKey be used? PinmemberBaz18-Jul-03 4:53 
GeneralVC7 compiler errors/warnings PinmemberC++ Hacker23-Jan-03 3:17 
GeneralEnumKey PinmemberMichael Ehehalt8-Dec-02 19:41 
GeneralRe: EnumKey PinmemberBruno Scopinho4-Jan-07 12:31 
Generalbug in GetValue PinmemberMichael Ehehalt8-Dec-02 19:31 
Generalbug in the readstring PinmemberThomas George1-Oct-02 14:08 
GeneralProblem with Hkey local machine PinsussZvika Vered18-Aug-02 6:11 
GeneralRe: Problem with Hkey local machine PinmemberRaimon814-Nov-05 2:34 
GeneralProblem with .Net! PinmemberGeorge Clarence6-Aug-02 23:12 
GeneralRe: Problem with .Net! PinmemberCarlos Antollini7-Aug-02 3:47 
GeneralRe: Problem with .Net! PinmemberGeorge Clarence7-Aug-02 4:32 
GeneralRename key PinsussZhangFei3-Aug-02 6:19 
GeneralSub-key delete PinsussAnonymous16-Jul-02 22:36 
GeneralC1010 Error PinmemberMatt Newman24-Nov-01 8:34 
GeneralRe: C1010 Error PinmemberCarlos Antollini26-Nov-01 3:52 
GeneralRe: C1010 Error PinmemberMatt Newman26-Nov-01 7:56 
GeneralRe: C1010 Error PinmemberCarlos Antollini26-Nov-01 8:53 
GeneralRe: C1010 Error PinmemberMatt Newman26-Nov-01 15:42 
GeneralRe: C1010 Error PinmemberMatt Newman13-Dec-01 10:57 
GeneralRe: C1010 Error Pinmemberbaur zeng30-Oct-02 14:38 
GeneralRe: C1010 Error PinmemberMatt Newman30-Oct-02 16:36 
GeneralRe: C1010 Error PinmemberAnonymous12-Apr-02 8:35 
GeneralRe: C1010 Error PinmemberCarlos Antollini12-Apr-02 8:52 
Generalmemory leak PinmemberSamuel22-Nov-01 23:42 
GeneralRe: memory leak PinmemberCarlos Antollini23-Nov-01 2:34 
GeneralRe: memory leak PinmemberSamuel26-Nov-01 2:53 
GeneralRe: memory leak PinsussAnonymous30-Oct-02 13:58 
GeneralCannot read ReadOnly keys PinmemberHans-Georg Ulrich8-Aug-01 6:43 
GeneralSuggestions PinmemberJörgen Sigvardsson8-Aug-01 0:50 
GeneralRe: Suggestions PinmemberJames Pullicino9-Aug-01 2:04 
GeneralRe: Suggestions PinmemberJörgen Sigvardsson9-Aug-01 2: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 | Mobile
Web02 | 2.8.140721.1 | Last Updated 7 Aug 2001
Article Copyright 2001 by Carlos Antollini
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid