We're going to create/open a new key and write some values to it
// let's create a new pointer to our registry class
// Sadly the MSDN MFC documentation seems to be wrong
// on the constructor logic.
// This will put us under the HKEY_CURRENT_USER node
// with full read/write rights.
CSettingsStore* m_pRegistry = new CSettingsStore( FALSE, FALSE );
int test = 100;
bool testBool = false;
// First always check your pointer to make sure nothing
// jinky has happened to it.
// CreateKey opens the key or if it doesn't exist
// creates a new key and sets it as the default current node.
if( m_pRegistry &&
m_pRegistry->CreateKey( _T("\\Software\\Test Application") ) )
{
// all the methods on the CSettingsClass return BOOL to
// let you know if they succeeded
// now we're under the node we want so we're going
// to write some values, the Write methods looks for DWORD
// so we need to do some casting to ensure our values get
// written correctly
m_pRegistry->Write( _T("intValue"), (int)test );
m_pRegistry->Write( _T("boolValue"), (bool)testBool );
// always close our current connection when done
m_pRegistry->Close();
}
Reading is much the same. We create an instace of the CSettingsClass object and call the Read method.
// let's create a new pointer to our registry class
// Sadly the MSDN MFC documentation seems to be wrong
// on the constructor logic.
// This will put us under the HKEY_CURRENT_USER node
// with full read/write rights.
CSettingsStore* m_pRegistry = new CSettingsStore( FALSE, FALSE );
int test = ;
// First always check your pointer to make sure nothing
// jinky has happened to it.
// CreateKey opens the key or if it doesn't exist
// creates a new key and sets it as the default current node.
if( m_pRegistry &&
m_pRegistry->CreateKey( _T("\\Software\\Test Application") ) )
{
// read in the value from the node
// again this will bring back a bool result for success or failure
m_pRegistry->Read( _T("test"), (int)test );
// always close our current connection when done
m_pRegistry->Close();
}
So there is some basic usage on how to read/write to the registry. A very easy way to quickly get data in and out for your application.
