Read/Write Registry Keys

Submitted by date of submission user level 

Mahesh Chand July 01, 2000 Beginner 

This article shows how to read/write/delete a registry entries from windows registry. In this example, I have used two registry keys. 

HKEY_LOCAL_MACHINE\SOFTWARE\MIS Inc.\Settings\Data  

HKEY_LOCAL_MACHINE\SOFTWARE\MIS Inc.\Settings\Server 

Here is how to read HKEY_LOCAL_MACHINE\SOFTWARE\MIS Inc.\Settings\Server key and fill contents of this key in a CString array. The left and right values of key are separated by '#'.

CString default_server;

CString default_root_dir;

CStringArray servers;

CStringArray rootdirs;

default_server = "";

default_root_dir = "";

servers.RemoveAll() ;

rootdirs.RemoveAll() ;

/* Read FTP server name and root directories from registry. GetRegistryList reads all the key values and fill in the CString array */

HKEY hKey = GetRegistryKey( "Server" );

GetRegistryList( hKey, default_server, servers); 

/* Now read it from array and add to a combo box */

for(int i=0; i

{

CString str = servers[i].Left( servers[i].Find("#") ) ; 

combo->AddString( str ) ;

}

Now, here is how to add one registry entry to the registry.

AddRegValue("Server", m_szftpServerName , m_szWebRootDir );

Now, here is how to delete a key and its corresponding value from the registry:

AddRegValue("Server", m_szftpServerName , m_szWebRootDir );

/* ************************************************************ 

Registry functions STARTS

************************************************************/

HKEY GetRegistryKey( CString str )

{

HKEY hViewerKey = NULL;

HKEY hSoftDirKey = NULL;

HKEY hCompanyKey = NULL;

HKEY hCompanyDirKey = NULL;

CString m_pszRegistryKey = "MIS Inc.";

CString m_pszProfileName;

m_pszProfileName = "Settings";

CString section = str ; 

//read from the registry...for default viewer

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, szSoftware, 0, KEY_WRITE|KEY_READ,

&hSoftDirKey) == ERROR_SUCCESS)

{

DWORD dw;

if (RegCreateKeyEx(hSoftDirKey, (LPCTSTR)m_pszRegistryKey, 0, REG_NONE,

REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,

&hCompanyKey, &dw) == ERROR_SUCCESS)

{

if(RegCreateKeyEx(hCompanyKey, (LPCTSTR)m_pszProfileName, 0, REG_NONE,

REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,

&hCompanyDirKey, &dw) == ERROR_SUCCESS)

{

RegCreateKeyEx(hCompanyDirKey, (LPCTSTR)section, 0, REG_NONE,

REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,

&hViewerKey, &dw);

}

}

}

if (hCompanyKey != NULL)

RegCloseKey(hCompanyKey);

if (hSoftDirKey != NULL)

RegCloseKey(hSoftDirKey);

if (hCompanyDirKey != NULL)

RegCloseKey(hCompanyDirKey);

return hViewerKey;

}

BOOL GetRegistryList(HKEY hKey, CString &strValue, CStringArray &keyValue)

{

// HKEY hKey = GetConversionKey( "Data" );

DWORD dwType, dwCount;

long index = 0;

LONG lResult = 0;

CHAR ValueName[max_value];

DWORD dwcValueName = max_value;

CString data;

ValueName[0] = '\0';

dwCount = max_value;

lResult = RegEnumValue(hKey,index,ValueName,//regenumvalue return the key value names.

&dwcValueName,NULL,&dwType,NULL,

&dwCount);

while(lResult != ERROR_NO_MORE_ITEMS)

{

RegQueryValueEx(hKey, ValueName, NULL, &dwType,

(LPBYTE)data.GetBuffer(dwCount/sizeof(TCHAR)), &dwCount);

data.ReleaseBuffer();

CString str = (CString)ValueName;

if(!(str.IsEmpty())) //neglecting the default value. 

{

str = (CString)ValueName + "#" + data;//concatenating the key value and its data 

//separating with #

keyValue.Add((LPCTSTR)str);

}

++index;

dwcValueName = max_value;

lResult = RegEnumValue(hKey,index,ValueName,

&dwcValueName,NULL,&dwType,NULL,

&dwCount);

}

//this call is for default viewer.

lResult = RegQueryValueEx(hKey, NULL, NULL, &dwType,

NULL, &dwCount);

if (lResult == ERROR_SUCCESS)

{

ASSERT(dwType == REG_SZ);

lResult = RegQueryValueEx(hKey, NULL, NULL, &dwType,

(LPBYTE)strValue.GetBuffer(dwCount/sizeof(TCHAR)), &dwCount);

strValue.ReleaseBuffer();

}

RegCloseKey(hKey);

if (lResult == ERROR_SUCCESS)

{

ASSERT(dwType == REG_SZ);

return TRUE; 

}

return FALSE;

}

BOOL GetLeftRegistryList(HKEY hKey, CString &strValue, CStringArray &keyValue)

{

// HKEY hKey = GetConversionKey( "Data" );

DWORD dwType, dwCount;

long index = 0;

LONG lResult = 0;

CHAR ValueName[max_value];

DWORD dwcValueName = max_value;

CString data;

ValueName[0] = '\0';

dwCount = max_value;

lResult = RegEnumValue(hKey,index,ValueName,//regenumvalue return the key value names.

&dwcValueName,NULL,&dwType,NULL,

&dwCount);

while(lResult != ERROR_NO_MORE_ITEMS)

{

RegQueryValueEx(hKey, ValueName, NULL, &dwType,

(LPBYTE)data.GetBuffer(dwCount/sizeof(TCHAR)), &dwCount);

data.ReleaseBuffer();

CString str = (CString)ValueName;

if(!(str.IsEmpty())) //neglecting the default value. 

{

str = (CString)ValueName ;//concatenating the key value and its data 

//separating with #

keyValue.Add((LPCTSTR)str);

}

++index;

dwcValueName = max_value;

lResult = RegEnumValue(hKey,index,ValueName,

&dwcValueName,NULL,&dwType,NULL,

&dwCount);

}

//this call is for default viewer.

lResult = RegQueryValueEx(hKey, NULL, NULL, &dwType,

NULL, &dwCount);

if (lResult == ERROR_SUCCESS)

{

ASSERT(dwType == REG_SZ);

lResult = RegQueryValueEx(hKey, NULL, NULL, &dwType,

(LPBYTE)strValue.GetBuffer(dwCount/sizeof(TCHAR)), &dwCount);

strValue.ReleaseBuffer();

}

RegCloseKey(hKey);

if (lResult == ERROR_SUCCESS)

{

ASSERT(dwType == REG_SZ);

return TRUE; 

}

return FALSE;

}

BOOL AddRegValue(const CString& key, const CString& value, const CString& data)

{

HKEY hKey = GetRegistryKey(key);

LPCTSTR lpszEntry = value;

LPCTSTR lpszValue = data ; 

if (hKey == NULL)

return FALSE;

LONG lResult = RegSetValueEx(hKey, lpszEntry, NULL, REG_SZ,

(LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));

RegCloseKey(hKey);

//adding in the registry so that number of extensions can be retrieved

//for which the viewers are added.

SetKruseExtension(value, data);

if(lResult == ERROR_SUCCESS)

return TRUE;

else

return FALSE;

}

BOOL DeleteRegValue(const CString& key, const CString& value, const CString& data)

{

HKEY hKey = GetRegistryKey(key);

LPCTSTR lpszEntry = value;

LPCTSTR lpszValue = data ; 

if (hKey == NULL)

return FALSE;

LONG lResult = RegSetValueEx(hKey, lpszEntry, NULL, REG_SZ,

(LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));

RegCloseKey(hKey);

//adding in the registry so that number of extensions can be retrieved

//for which the viewers are added.

SetKruseExtension(value , data);

if(lResult == ERROR_SUCCESS)

return TRUE;

else

return FALSE;

}

/* ************************************************************ 

Registry functions ENDS

************************************************************/