87 lines
2.9 KiB
C++
87 lines
2.9 KiB
C++
/**
|
|
* @brief The RegistryCRUD.cpp program demonstrates how to perform basic operations on the Windows Registry using the native API.
|
|
* The program performs the following CRUD (Create, Read, Update, Delete) operations on a registry key:
|
|
* Open Key: It opens a registry key for the current user's application settings.
|
|
* Create Value: It creates a new registry value with the name "MySetting" and sets its data to "MyValue".
|
|
* Read Value: It reads the value of "MySetting" from the registry and prints it.
|
|
* Modify Value: It modifies the value of "MySetting" to "MyNewValue".
|
|
* Delete Value: It deletes the value of "MySetting" from the registry.
|
|
*
|
|
* @author Yavuz Sava (woose)
|
|
* @date 13/12/2024
|
|
*/
|
|
|
|
#include <windows.h>
|
|
#include <iostream>
|
|
using namespace std;
|
|
// Open a registry key using the native API.
|
|
HKEY OpenKey(HKEY hKey, LPCWSTR subKey, DWORD dwOptions)
|
|
{
|
|
HKEY hOpenKey = NULL;
|
|
if (RegOpenKeyEx(hKey, subKey, 0, KEY_READ | KEY_WRITE, &hOpenKey) != ERROR_SUCCESS)
|
|
{
|
|
cerr << "Error opening registry key." << endl;
|
|
return NULL;
|
|
}
|
|
return hOpenKey;
|
|
}
|
|
// Create a new registry value.
|
|
void CreateValue(HKEY hKey, LPCWSTR valueName, LPCWSTR valueData)
|
|
{
|
|
if (RegSetValueEx(hKey, valueName, 0, REG_SZ, (const BYTE *)valueData, (DWORD)(wcslen(valueData) * sizeof(WCHAR))) != ERROR_SUCCESS)
|
|
{
|
|
cerr << "Error creating registry value." << endl;
|
|
}
|
|
}
|
|
// Read a registry value.
|
|
void ReadValue(HKEY hKey, LPCWSTR valueName)
|
|
{
|
|
DWORD dwType = REG_SZ;
|
|
WCHAR szBuffer[256];
|
|
DWORD dwSize = sizeof(szBuffer);
|
|
if (RegQueryValueEx(hKey, valueName, NULL, &dwType, (LPBYTE)szBuffer, &dwSize) != ERROR_SUCCESS)
|
|
{
|
|
cerr << "Error reading registry value." << endl;
|
|
}
|
|
else
|
|
{
|
|
wcout << valueName << L": " << szBuffer << endl;
|
|
}
|
|
}
|
|
// Modify a registry value.
|
|
void ModifyValue(HKEY hKey, LPCWSTR valueName, LPCWSTR valueData)
|
|
{
|
|
if (RegSetValueEx(hKey, valueName, 0, REG_SZ, (const BYTE *)valueData, (DWORD)(wcslen(valueData) * sizeof(WCHAR))) != ERROR_SUCCESS)
|
|
{
|
|
cerr << "Error modifying registry value." << endl;
|
|
}
|
|
}
|
|
// Delete a registry value.
|
|
void DeleteValue(HKEY hKey, LPCWSTR valueName)
|
|
{
|
|
if (RegDeleteValue(hKey, valueName) != ERROR_SUCCESS)
|
|
{
|
|
cerr << "Error deleting registry value." << endl;
|
|
}
|
|
}
|
|
int main()
|
|
{
|
|
// Open the registry key for the current user's application settings.
|
|
HKEY hAppSettings = OpenKey(HKEY_CURRENT_USER, L"Software\\MyCompany\\MyApplication", 0);
|
|
if (hAppSettings == NULL)
|
|
{
|
|
return 1;
|
|
}
|
|
// Create a new registry value.
|
|
CreateValue(hAppSettings, L"MySetting", L"MyValue");
|
|
// Read the registry value.
|
|
ReadValue(hAppSettings, L"MySetting");
|
|
// Modify the registry value.
|
|
ModifyValue(hAppSettings, L"MySetting", L"MyNewValue");
|
|
// Delete the registry value.
|
|
DeleteValue(hAppSettings, L"MySetting");
|
|
// Close the registry key.
|
|
RegCloseKey(hAppSettings);
|
|
return 0;
|
|
}
|