1、主要函数
1.1、打开一个指定的注册表键
LONG RegOpenKeyEx(
HKEY hKey, //需要打开的主键的名称
LPCTSTR lpSubKey, //需要打开的子键的名称
DWORD ulOptions, //保留,设为0
REGSAM samDesired, //安全访问标记,也就是权限
PHKEY phkResult //得到的将要打开键的句柄
)
返回值:如果调用成功,返回0(
ERROR_SUCCESS)
1.2、创建指定的注册键
LONG WINAPI RegCreateKeyEx(
HKEY hKey, //主键的名称
LPCTSTR lpSubKey, //子键的名称
DWORD Reserved, //保留,为0
LPTSTR lpClass, //指定此键的类(对象类型), 如果键已经存在,则忽略此参数
DWORD dwOptions, //指定密钥的特殊选项
REGSAM samDesired, //指定对密钥的访问权限
LPSECURITY_ATTRIBUTES lpSecurityAttributes, //可设为NULL
PHKEY phkResult, //句柄
LPDWORD lpdwDisposition //可设为NULL
);
返回值:如果调用成功,返回0(
ERROR_SUCCESS)
1.3、在注册表项下设置指定值的数据和类型
LONG RegSetValueEx(
HKEY hKey, // 句柄
LPCTSTR lpValueName, // 指向一个字符串的指针,该字符串包含要设置的值的名称
DWORD Reserved, // 保留,设为0
DWORD dwType, // 指定数据类型(字符串:REG_SZ, 数字:REG_DWORD)
CONST BYTE *lpData, // 指向一个缓冲区,该缓冲区包含了欲为指定值名称存储的数据
DWORD cbData // 指定由lpData参数所指向的数据的大小,单位是字节。
);
返回值:如果调用成功,返回0(
ERROR_SUCCESS)
1.4、读取指定注册表键的类型和设置值
LONG RegQueryValueEx(
HKEY hKey, // 句柄
LPCTSTR lpValueName, // 要查询注册表键值的名字字符串
LPDWORD lpReserved, // 设为0
LPDWORD lpType, // 用于装载取回数据类型的一个变量
LPBYTE lpData, // 用于装载指定值的一个缓冲区
LPDWORD lpcbData // 用于装载lpData缓冲区长度的一个变量
);
返回值:如果调用成功,返回0(
ERROR_SUCCESS)
1.5、释放指定注册键的句柄
LONG RegCloseKey(
HKEY hKey // 释放键的句柄
);
返回值:如果调用成功,返回0(
ERROR_SUCCESS)
2、测试代码
2.1、写数据到注册表
#include <windows.h>
#include <stdio.h>
#include <iostream>
int main()
{
HKEY hKey = NULL;
//打开注册表
long result = RegOpenKeyEx(HKEY_CURRENT_USER, (LPCTSTR)("CSDN\\20211118"), 0, KEY_ALL_ACCESS | KEY_WOW64_32KEY, &hKey);
if (result != ERROR_SUCCESS) {
printf("call RegOpenKeyEx failure, result=%d\n", result);
return result;
}
//创建注册项
result = RegCreateKeyEx(HKEY_CURRENT_USER, (LPCTSTR)("CSDN\\20211118"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS | KEY_WOW64_32KEY, NULL, &hKey, NULL);
if (result != ERROR_SUCCESS) {
printf("call RegOpenKeyEx failure, result=%d\n", result);
if (hKey != 0) {
RegCloseKey(hKey);
}
return result;
}
//写入数据
//写入字符串
std::string ip = "127.0.0.1";
LPCSTR lpIp = ip.c_str();
DWORD IpLength = ip.length();
result = RegSetValueEx(hKey, LPCTSTR("ip"), 0, REG_SZ, (LPBYTE)lpIp, IpLength);
if (ERROR_SUCCESS != result) {
printf("RegSetValueEx failure, result=%d\n", result);
if (hKey != 0) {
RegCloseKey(hKey);
}
return result;
}
DWORD port = 18;
DWORD portLength = sizeof(port);
//写入数字
result = RegSetValueEx(hKey, LPCTSTR("port"), 0, REG_DWORD, (LPBYTE)&port, portLength);
if (ERROR_SUCCESS != result) {
printf("RegSetValueEx failure, result=%d\n", result);
if (hKey != 0) {
RegCloseKey(hKey);
}
return result;
}
//关闭注册表
if (hKey != 0) {
RegCloseKey(hKey);
}
system("pause");
return 0;
}
执行程序后就可以在注册表看到写入的数据
2.2、从注册表读数据
#include <windows.h>
#include <stdio.h>
#include <iostream>
int main()
{
HKEY hKey = NULL;
//打开注册表
long result = RegOpenKeyEx(HKEY_CURRENT_USER, (LPCTSTR)("CSDN\\20211118"), 0, KEY_ALL_ACCESS | KEY_WOW64_32KEY, &hKey);
if (result != ERROR_SUCCESS) {
printf("call RegOpenKeyEx failure, result=%d\n", result);
return result;
}
//读取数据
//读字符串
std::string ip;
DWORD dataTypeIp = REG_SZ;
BYTE addressValue[20] = { 0 };
DWORD ipAddressLength = sizeof(addressValue);
result = RegQueryValueEx(hKey, "ip", NULL, &dataTypeIp, addressValue, &ipAddressLength);
if (result != ERROR_SUCCESS) {
if (hKey != 0) {
RegCloseKey(hKey);
}
}
ip.assign((char*)addressValue);
printf("ip = %s\n", ip.c_str());
//读数字
DWORD port;
DWORD dataTypePort = REG_DWORD;
DWORD portLength = sizeof(dataTypePort);
result = RegQueryValueEx(hKey, "port", NULL, &dataTypePort, (LPBYTE)&port, &portLength);
if (result != ERROR_SUCCESS) {
if (hKey != 0) {
RegCloseKey(hKey);
}
}
int iPort = (int)port;
printf("port = %d\n", iPort);
//关闭注册表
if (hKey != 0) {
RegCloseKey(hKey);
}
system("pause");
return 0;
}
3、其他功能
3.1、从注册表读取计算机已安装的软件
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
void QueryKey(HKEY hKey, std::vector<std::string>& v)
{
TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name
DWORD cbName; // size of name string
TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys = 0; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cchMaxClass; // longest class string
DWORD cValues; // number of values for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time
DWORD i, retCode;
TCHAR achValue[MAX_VALUE_NAME];
DWORD cchValue = MAX_VALUE_NAME;
//检索有关指定注册表项的信息
retCode = RegQueryInfoKey(hKey, achClass, &cchClassName, NULL, &cSubKeys, &cbMaxSubKey, &cchMaxClass, &cValues, &cchMaxValue, &cbMaxValueData, &cbSecurityDescriptor, &ftLastWriteTime);
if (cSubKeys)
{
printf("Number of subkeys: %d\n", cSubKeys);
for (i = 0; i<cSubKeys; i++)
{
cbName = MAX_KEY_LENGTH;
//枚举指定项下方的子项
retCode = RegEnumKeyEx(hKey, i, achKey, &cbName, NULL, NULL, NULL, &ftLastWriteTime);
if (retCode == ERROR_SUCCESS)
{
v.push_back(achKey);
//printf("index = %d, achKey = %s\n", i + 1, achKey);
}
}
}
}
int main()
{
HKEY hKey = NULL;
HKEY hSubKey = NULL;
std::vector<std::string> v;
long result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", 0, KEY_READ, &hKey);
if (result != ERROR_SUCCESS)
{
printf("call RegOpenKeyEx failure, result=%d\n", result);
return result;
}
QueryKey(hKey, v);
for (int i = 0; i < v.size(); i++)
{
v.at(i) = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + v.at(i);
printf("%s\n", v.at(i).c_str());
}
for (int i = 0; i < v.size(); i++)
{
long subResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, v.at(i).c_str(), 0, KEY_READ, &hSubKey);
if (subResult != ERROR_SUCCESS)
{
printf("sub call RegOpenKeyEx failure, result=%d\n", subResult);
return subResult;
}
DWORD dataType = REG_SZ;
BYTE displayName[200] = { 0 };
DWORD displayNameLength = sizeof(displayName);
long resultName = RegQueryValueEx(hSubKey, "DisplayName", NULL, &dataType, displayName, &displayNameLength);
if (resultName != ERROR_SUCCESS) {
if (hSubKey != NULL) {
RegCloseKey(hSubKey);
hSubKey = NULL;
}
}
if (strcmp((char*)displayName, "") != 0) {
printf("displayName = %s\n", (char*)displayName);
}
}
RegCloseKey(hKey);
}
windows注册表相关资料
注册表 - Win32 apps | Microsoft Docshttps://docs.microsoft.com/zh-cn/windows/win32/sysinfo/registry