Creating and Deleting keys and values in REGEDIT using C program..

2 replies [Last post]
mckchenthil
Offline
Last seen: 9 years 49 weeks ago
Joined: 2009-12-04
bOt Points: 58

HI..

I'm searching for a C program...

How to create and delete registry keys using C program ...
if any one has any ideas please write code...

shashwat
Offline
Last seen: 1 year 31 weeks ago
Joined: 2009-02-18
bOt Points: 1160
Hello mckchenthil, Check out

Hello mckchenthil,

Check out the following C++ code. It prints out the registry value of the home page of Internet Explorer. It is self explanatory and well commented. See if it works..

 

#include <windows.h>

#include <iostream.h>


int main () {


HKEY hKey; // Declare a key to store the result

DWORD buffersize = 1024; // Declare the size of the data buffer

char* lpData = new char[buffersize];// Declare the buffer


/* Open the Registry Key at the location 

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main

with read only access

*/


RegOpenKeyEx (HKEY_CURRENT_USER, 

"Software\\Microsoft\\Internet Explorer\\Main",NULL,KEY_READ,&hKey);

// Query the registry value

RegQueryValueEx(hKey,"Start Page",NULL,NULL,(LPBYTE) lpData,&buffersize);

// Print out the registry value

std::cout << "Registry Key Open: memory location=" << hKey << "\n";

std::cout << "Your Internet Start Page is " << lpData << "\n\n";


// Close the Registry Key

RegCloseKey (hKey);

// Pause the system so there is time to read what is going on

system("Pause");


delete lpData;

}

 

Founder and Admin

shashwat
Offline
Last seen: 1 year 31 weeks ago
Joined: 2009-02-18
bOt Points: 1160
Here is a complete set of

Here is a complete set of functions to read/write/delete registry entries in Visual C++.

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

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

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

Founder and Admin