标签:time pts ems 内存 dia files config software msn
注册表和文件的操作差不多,也存在打开,关闭,写入,查询等操作,使用的所有API函数皆以Reg开头。
对注册表操作前,需通过API函数打开注册表,并返回用于操作注册表的句柄,通过此句柄,来对注册表进行读写操作。
LONG RegOpenKeyEx(
HKEY hKey, // handle to open key
LPCTSTR lpSubKey, // address of name of subkey to open
DWORD ulOptions, // reserved
REGSAM samDesired, // security access mask
PHKEY phkResult // address of handle to open key
);
在Win16下,还有一个RegOpenKey()函数,如今虽仍可使用,但RegOpenKeyEx()兼容性更好。
打开注册表,实质是打开注册表的某一个子键,然后进行操作。
操作完注册表后,也需要关闭句柄以释放资源。函数如下:
LONG RegCloseKey(
HKEY hKey //handle to key to close
);
LONG RegCreateKeyEx(
HKEY hKey, // handle to an open key
LPCTSTR lpSubKey, // address of subkey name
DWORD Reserved, // reserved
LPTSTR lpClass, // address of class string
DWORD dwOptions, // special options flag
REGSAM samDesired, // desired security access
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
// address of key security structure
PHKEY phkResult, // address of buffer for opened handle
LPDWORD lpdwDisposition // address of disposition value buffer
);
当需要创建的子键以存在时,该函数与RegOpenKeyEx()函数的作用相同。
LONG RegDeleteKey(
HKEY hKey, //父键句柄
LPCTSTR lpSubKey //指向要删除的子键名称字符串
);
该函数的值能用来删除键值项,即函数只能删除最下一层的子键。
读取键名称中的数据或查询键名称的属性,使用此函数:
LONG RegQueryValueEx(
HKEY hKey, // handle to key to query
LPTSTR lpValueName, // address of name of value to query
LPDWORD lpReserved, // reserved
LPDWORD lpType, // address of buffer for value type
LPBYTE lpData, // address of data buffer
LPDWORD lpcbData // address of data buffer size
);
LONG RegSetValueEx(
HKEY hKey, // handle to key to set value for
LPCTSTR lpValueName, // name of the value to set
DWORD Reserved, // reserved
DWORD dwType, // flag for value type
CONST BYTE *lpData, // address of value data
DWORD cbData // size of value data
);
LONG RegDeleteValue(
HEKY hKey, //指定删除的句柄
LPCTSTR lpValueName //被删除键值项的名称
);
枚举即逐一获取。子键的枚举对指定键下的子键进行逐一获取。键值的枚举是对指定子键下的键值进行逐一获取。
LONG RegEnumKeyEx(
HKEY hKey, // handle to key to enumerate
DWORD dwIndex, // index of subkey to enumerate
LPTSTR lpName, // address of buffer for subkey name
LPDWORD lpcbName, // address for size of subkey buffer
LPDWORD lpReserved, // reserved
LPTSTR lpClass, // address of buffer for class string
LPDWORD lpcbClass, // address for size of class buffer
PFILETIME lpftLastWriteTime
// address for time key last written to
);
LONG RegEnumValue(
HKEY hKey, // handle to key to query
DWORD dwIndex, // index of value to query
LPTSTR lpValueName, // address of buffer for value string
LPDWORD lpcbValueName, // address for size of value buffer
LPDWORD lpReserved, // reserved
LPDWORD lpType, // address of buffer for type code
LPBYTE lpData, // address of buffer for value data
LPDWORD lpcbData // address for size of data buffer
);
#include <Windows.h>
#include <iostream>
#include <iomanip>
int main()
{
char szValueName[MAXBYTE] = { 0 };
char szValueKey[MAXBYTE] = { 0 };
DWORD dwBufferSize = MAXBYTE;
DWORD dwKeySize = MAXBYTE;
DWORD dwType = 0;
HKEY hKey = NULL;
char subKeyName[] = "Software\\Microsoft\\Windows\\CurrentVersion\\";
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, subKeyName, 0, KEY_ALL_ACCESS, &hKey);
if (lRet != ERROR_SUCCESS)
{
std::cout << "ERROR:打开注册表失败,可能是因为没有提供管理员权限。";
return -1;
}
int i = 0;
while (TRUE)
{
lRet = RegEnumValue(hKey, i, szValueName, &dwBufferSize, NULL, &dwType, reinterpret_cast<LPBYTE>(szValueKey), &dwKeySize);
if (lRet == ERROR_NO_MORE_ITEMS)
{
break;
}
std::cout << i << " " << std::setw(30) <<szValueName << " " << std::setw(50) << szValueKey << std::endl;
ZeroMemory(szValueKey, MAXBYTE);
ZeroMemory(szValueName, MAXBYTE);
dwBufferSize = MAXBYTE;
dwKeySize = MAXBYTE;
i++;
}
RegCloseKey(hKey);
return 0;
}
0 CommonFilesDir C:\Program Files (x86)\Common Files
1 CommonFilesDir (x86) C:\Program Files (x86)\Common Files
2 CommonW6432Dir C:\Program Files\Common Files
3 DevicePath %SystemRoot%\inf
4 MediaPathUnexpanded %SystemRoot%\Media
5 ProgramFilesDir C:\Program Files (x86)
6 ProgramFilesDir (x86) C:\Program Files (x86)
7 ProgramFilesPath %ProgramFiles%
8 ProgramW6432Dir C:\Program Files
9 SM_ConfigureProgramsName Set Program Access and Defaults
10 SM_GamesName Games
标签:time pts ems 内存 dia files config software msn
原文地址:https://www.cnblogs.com/coolcpp/p/regoperator.html