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

A Registry Class

By , 6 Aug 2001
 

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
BugOpenKey does not work on Windows 7memberThomas Haase27 Sep '11 - 23:32 
OpenKey may fail on Windows 7, e.g for HKLM. This is probably related to the settings of the Windows 7 user access control.
 
For my context, replacing KEY_ALL_ACCESS in RegOpenKeyEx with KEY_READ fixes this issue.
 
BOOL CRegistry::OpenKey(enum Keys hKey, LPCTSTR szKey)
{	
	if(RegOpenKeyEx((HKEY)hKey,(LPCSTR)szKey, 0, KEY_READ, &m_hKey) == ERROR_SUCCESS)
	{
		return TRUE;
	}
	else
	{
		m_hKey = NULL;
		return FALSE;
	}
}
Thomas Haase

GeneralCannot read key from class RegistrymemberLarry Mills Sr3 Jun '09 - 15:39 
I used the class to obtain values that an installer package saved to the registry. I can see the values in Regedit. but when the CRegistry class readline() reads them and tries to put them in a CString I get "Bad Pointer> errors in VS2008 MFC
 
I really need a solution to this: here's my code:
// App command to run the dialog
void CShopApp::FindRegData()
{
CRegistry Reg();
CRegData m_cRegData;// class of 5 CStrings
m_cRegData.CleanUp();
//CRegistry Reg;
CString csSubRoot = "SOFTWARE\\Mills Software Solutions Inc\\Shop";
m_cRegData.m_csRootMainKey = "HKEY_LOCAL_MACHINE";
m_cRegData.m_csSubKey = csSubRoot;
Reg.SetRootKey(HKEY_LOCAL_MACHINE);
if (Reg.SetKey(csSubRoot, FALSE))
{
m_cRegData.m_csCompany = Reg.ReadString("Company", "");// all CStrings in class m_cRegData are
"bad pointer" errors
m_cRegData.m_csUser = Reg.ReadString("Name", "");
m_cRegData.m_csSerialCode = Reg.ReadString("Serial", "");
m_cRegData.m_csRegCode = Reg.ReadString("RegCode", "");
//Reg
}
else
{
TRACE("Failed to open key\n");// never goes here!
}
 
m_csSaveData = InsertDelimiterReg(m_cRegData);// all CStrings Bad pointer
FileSaveRegData();
}
 
A C++ programming language novice, but striving to learn

Questionvery useful classmemberkryptun16 Dec '08 - 10:50 
thanks for very useful class.
is there any chance for Unicode support??
Questionerror C2872: 'ULONG_PTR' : ambiguous symbolmemberPooh Agarwal17 Oct '06 - 20:50 
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 symbolmemberVider22 Nov '07 - 3:33 
replace the symbol on you import directive:
 
#import "libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2" version("8.0") lcid("0") raw_interfaces_only named_guids rename("ULONG_PTR","ULONG_PTRDTE")
Generalbug fix for VC7/8 [modified]memberwaldermort4 Oct '06 - 8:16 
Change the keys enum to the following:
enum Keys
{
    classesRoot     = (LONG)(ULONG_PTR)HKEY_CLASSES_ROOT,
    currentUser     = (LONG)(ULONG_PTR)HKEY_CURRENT_USER,
    localMachine    = (LONG)(ULONG_PTR)HKEY_LOCAL_MACHINE,
    users           = (LONG)(ULONG_PTR)HKEY_USERS,
    performanceData = (LONG)(ULONG_PTR)HKEY_PERFORMANCE_DATA, //Windows NT/2000
    currentConfig   = (LONG)(ULONG_PTR)HKEY_CURRENT_CONFIG,
    dynData         = (LONG)(ULONG_PTR)HKEY_DYN_DATA          //Windows 95/98
};
 

-- modified at 14:35 Wednesday 4th October, 2006
GeneralThank youmembermsanchezv30 Jan '06 - 1:25 
For me it works just fine, and it's very useful
GeneralMemory leaksussjmaccelari19 Jul '04 - 20: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!)memberE-Male19 May '04 - 0:13 
i tried to add this clase to a program (that for does nothing)
i use ms visual studio .net 2003 c++, but i'm very new to it
 
so my question is:
can i just add this class or do i have to include other stuff or add a namespace or...?
 
if there is nothing more to add:
is there anything to be carefull about when adding classes to a visual studio project? (i only learned command line g++)
 
thx in advance
E-Male
GeneralAlmost perfect!memberTerry O'Nolley15 May '04 - 15:23 
First off - thanks for simplifying the registry. I am working on my first project where I need to add registry entries and I headed straight for Code Project.
 
I voted this a 5.

 







General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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