Click here to Skip to main content
Licence 
First Posted 6 Aug 2001
Views 139,368
Downloads 1,536
Bookmarked 44 times

A Registry Class

By | 6 Aug 2001 | Article
A class that makes it easy to work with the registry

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

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
BugOpenKey does not work on Windows 7 PinmemberThomas Haase23:32 27 Sep '11  
GeneralCannot read key from class Registry PinmemberLarry Mills Sr15:39 3 Jun '09  
Questionvery useful class Pinmemberkryptun10:50 16 Dec '08  
Questionerror C2872: 'ULONG_PTR' : ambiguous symbol PinmemberPooh Agarwal20:50 17 Oct '06  
Hi,
 
While using HKEY_LOCAL_MACHINE in RegOpenKey method, I get following error message -
 
error C2872: 'ULONG_PTR' : ambiguous symbol
could be 'd:\microsoft visual studio 8\vc\platformsdk\include\basetsd.h(123) : __w64 unsigned long ULONG_PTR'or 'd:\usbproject\debug\dte80a.tlh(463) : EnvDTE::ULONG_PTR'
 

I have included EnvDTE namespace in the file.
 
Could someone help me in detecting what is wrong?
 
Thanks in advance.
 
Poonam
AnswerRe: error C2872: 'ULONG_PTR' : ambiguous symbol PinmemberVider3:33 22 Nov '07  
Generalbug fix for VC7/8 [modified] Pinmemberwaldermort8:16 4 Oct '06  
GeneralThank you Pinmembermsanchezv1:25 30 Jan '06  
GeneralMemory leak Pinsussjmaccelari20:45 19 Jul '04  
Generalcan't get it to work (noob!) PinmemberE-Male0:13 19 May '04  
GeneralAlmost perfect! PinmemberTerry O'Nolley15:23 15 May '04  
GeneralRe: Almost perfect! PinmemberCarlos Antollini15:42 15 May '04  
Questionbug with 2000pro ? Pinmemberalex-pcom2:17 22 Jan '04  
AnswerRe: bug with 2000pro ? PinmemberCarlos Antollini3:36 22 Jan '04  
GeneralRe: bug with 2000pro ? PinmemberShikamaru21:03 27 Apr '04  
GeneralSource updated for VS7 Pinmemberralfoide11:42 30 Sep '03  
QuestionShould RegFlushKey be used? PinmemberBaz4:53 18 Jul '03  
GeneralVC7 compiler errors/warnings PinmemberC++ Hacker3:17 23 Jan '03  
GeneralEnumKey PinmemberMichael Ehehalt19:41 8 Dec '02  
GeneralRe: EnumKey PinmemberBruno Scopinho12:31 4 Jan '07  
Generalbug in GetValue PinmemberMichael Ehehalt19:31 8 Dec '02  
Generalbug in the readstring PinmemberThomas George14:08 1 Oct '02  
GeneralProblem with Hkey local machine PinsussZvika Vered6:11 18 Aug '02  
GeneralRe: Problem with Hkey local machine PinmemberRaimon812:34 4 Nov '05  
GeneralProblem with .Net! PinmemberGeorge Clarence23:12 6 Aug '02  
GeneralRe: Problem with .Net! PinmemberCarlos Antollini3:47 7 Aug '02  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 7 Aug 2001
Article Copyright 2001 by Carlos Antollini
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid