5,666,979 members and growing! (16,735 online)
Email Password   helpLost your password?
General Reading » Hardware & System » Registry     Intermediate

A Registry Class

By Carlos Antollini

A class that makes it easy to work with the registry
VC6, C++Windows, NT4, Win2K, Visual Studio, MFC, Dev

Posted: 6 Aug 2001
Updated: 6 Aug 2001
Views: 103,650
Bookmarked: 33 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
47 votes for this Article.
Popularity: 7.35 Rating: 4.40 out of 5
2 votes, 7.7%
1
1 vote, 3.8%
2
1 vote, 3.8%
3
8 votes, 30.8%
4
14 votes, 53.8%
5

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

"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();

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

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

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

pReg.CloseKey();

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

"Sample3">  CRegistry pReg;

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

  pReg.DeleteValue("SZVAL");	
  pReg.CloseKey();

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


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 Citibank.
Occupation: Architect
Company: Citigroup
Location: Argentina Argentina

Other popular Hardware & System articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 43 (Total in Forum: 43) (Refresh)FirstPrevNext
Questionerror C2872: 'ULONG_PTR' : ambiguous symbolmemberPooh Agarwal21:50 17 Oct '06  
AnswerRe: error C2872: 'ULONG_PTR' : ambiguous symbolmemberVider4:33 22 Nov '07  
Generalbug fix for VC7/8 [modified]memberwaldermort9:16 4 Oct '06  
GeneralThank youmembermsanchezv2:25 30 Jan '06  
GeneralMemory leaksussjmaccelari21:45 19 Jul '04  
Generalcan't get it to work (noob!)memberE-Male1:13 19 May '04  
GeneralAlmost perfect!memberTerry O'Nolley16:23 15 May '04  
GeneralRe: Almost perfect!memberCarlos Antollini16:42 15 May '04  
Generalbug with 2000pro ?memberalex-pcom3:17 22 Jan '04  
GeneralRe: bug with 2000pro ?memberCarlos Antollini4:36 22 Jan '04  
GeneralRe: bug with 2000pro ?memberShikamaru22:03 27 Apr '04  
GeneralSource updated for VS7memberralfoide12:42 30 Sep '03  
GeneralShould RegFlushKey be used?memberBaz5:53 18 Jul '03  
GeneralVC7 compiler errors/warningsmemberC++ Hacker4:17 23 Jan '03  
GeneralEnumKeymemberMichael Ehehalt20:41 8 Dec '02  
GeneralRe: EnumKeymemberBruno Scopinho13:31 4 Jan '07  
Generalbug in GetValuememberMichael Ehehalt20:31 8 Dec '02  
Generalbug in the readstringmemberThomas George15:08 1 Oct '02  
GeneralProblem with Hkey local machinesussZvika Vered7:11 18 Aug '02  
GeneralRe: Problem with Hkey local machinememberRaimon813:34 4 Nov '05  
GeneralProblem with .Net!memberGeorge Clarence0:12 7 Aug '02  
GeneralRe: Problem with .Net!memberCarlos Antollini4:47 7 Aug '02  
GeneralRe: Problem with .Net!memberGeorge Clarence5:32 7 Aug '02  
GeneralRename keysussZhangFei7:19 3 Aug '02  
GeneralSub-key deletesussAnonymous23:36 16 Jul '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Aug 2001
Editor: Brian Delahunty
Copyright 2001 by Carlos Antollini
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project