整理下注册表读取子键的操作,方便以后查阅。随便找个注册表位置,如下图:
我们要读取HKEY_LOCAL_MACHINE\System\Setup下的子键,上图红框中的。
#include <windows.h> #include <stdio.h> #define MAX_KEY_LENGTH 255 #define MAX_VALUE_NAME 16383 void QueryKey(HKEY hKey) { TCHAR achKey[MAX_KEY_LENGTH]; //装载子键内容 DWORD cbName; TCHAR achClass[MAX_PATH] = TEXT(""); //装载类名 DWORD cchClassName = MAX_PATH; DWORD cSubKeys=0; //子键数量 DWORD cbMaxSubKey; DWORD cchMaxClass; DWORD cValues; //键值 DWORD cchMaxValue; DWORD cbMaxValueData; DWORD cbSecurityDescriptor; FILETIME ftLastWriteTime; char subkeystr[MAX_KEY_LENGTH]; DWORD i, retCode; DWORD cchValue = MAX_VALUE_NAME; retCode = RegQueryInfoKey(hKey,achClass,&cchClassName,NULL,&cSubKeys, &cbMaxSubKey,&cchMaxClass,&cValues,&cchMaxValue,&cbMaxValueData,&cbSecurityDescriptor,&ftLastWriteTime); if (cSubKeys) { printf( "\nNumber 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) { ZeroMemory(subkeystr,MAX_KEY_LENGTH); printf("%s\n",achKey); } } } } void main() { HKEY hKey; char *defkey = "System\\Setup"; if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,defkey,0, KEY_READ,&hKey) == ERROR_SUCCESS) { QueryKey(hKey); } }
这样就可以读出来啦!