注册表遍历操作


  1. // EnumReg.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <stdio.h>
  5. #include <Windows.h>
  6. int main(int argc, char* argv[])
  7. {
  8. HKEY hMainKey = HKEY_CURRENT_USER;
  9. HKEY hSubKey;
  10. //打开HKEY_CURRENT_USER\Control Panel键句柄,并获得枚举权限,以枚举其下面的子键
  11. LONG ReturnValue = RegOpenKeyEx (hMainKey, "Control Panel",
  12. 0L, KEY_ENUMERATE_SUB_KEYS, &hSubKey);
  13. if (ReturnValue == ERROR_SUCCESS) //如果成功打开
  14. {
  15. FILETIME ftLastWriteTime; //用以保存最后一次写入时间
  16. SYSTEMTIME sysTime; //用以转化成系统时间
  17. TCHAR tcKeyName[128]; //用于保存每个子键的名字的内存空间
  18. DWORD dwKeyNameSize = 128;
  19. int i = 0, retCode = 0;
  20. printf("%-30s%-30s", "键名", "最后一次写入时间"); //打印标题行
  21. printf("\n");
  22. TCHAR tcBuffer[256] = {0};
  23. while (1)
  24. { //获取每个子键的名称
  25. retCode = RegEnumKeyEx(hSubKey, i, tcKeyName, &dwKeyNameSize,
  26. NULL, NULL, NULL, &ftLastWriteTime);
  27. if (retCode == ERROR_NO_MORE_ITEMS)
  28. { //如果获取结束则跳出循环
  29. break;
  30. }
  31. //将文件时间转化成系统时间
  32. FileTimeToSystemTime(&ftLastWriteTime, &sysTime);
  33. sprintf(tcBuffer, "%02d-%02d-%02d %02d:%02d:%02d", sysTime.wYear,
  34. sysTime.wMonth, sysTime.wDay,
  35. sysTime.wHour, sysTime.wMinute, sysTime.wSecond);
  36. printf("%-30s%-30s", tcKeyName, tcBuffer);
  37. printf("\n");
  38. dwKeyNameSize = 128;
  39. i++;
  40. }
  41. }
  42. return 0;
  43. }



  1. // EnumReg2.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <stdio.h>
  5. #include <tchar.h>
  6. #include <Windows.h>
  7. int main(int argc, char* argv[])
  8. {
  9. HKEY hMainKey = HKEY_CURRENT_USER;
  10. HKEY hSubKey;
  11. //打开HKEY_CURRENT_USER\Control Panel键句柄,并获得枚举和设置权限。
  12. LONG ReturnValue = ::RegOpenKeyEx (hMainKey, "Control Panel",
  13. 0L, KEY_ENUMERATE_SUB_KEYS|KEY_QUERY_VALUE, &hSubKey);
  14. DWORD dwSubKeyCount = 0;
  15. if (ReturnValue == ERROR_SUCCESS) //如果成功打开
  16. {
  17. //查询HKEY_CURRENT_USER\Control Panel键下子键个数,不需要其他信息,所以其他参数统统传入NULL值。
  18. RegQueryInfoKey(hSubKey, NULL, NULL, NULL, &dwSubKeyCount,
  19. NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  20. TCHAR tcKeyName[128] = {0}; //用于保存每个子键的名字的内存空间
  21. DWORD dwKeyNameSize = 128;
  22. for (int i=0; i<dwSubKeyCount; i++)
  23. { //循环获得每个子键的名字
  24. RegEnumKeyEx(hSubKey, i, tcKeyName, &dwKeyNameSize,
  25. NULL, NULL, NULL, NULL);
  26. printf("%s\n", tcKeyName);
  27. dwKeyNameSize=128;
  28. }
  29. }
  30. return 0;
  31. }




  1. /* ************************************
  2. *《精通Windows API》
  3. * 示例代码
  4. * reg.c
  5. * 10.3 注册表
  6. **************************************/
  7. /* 头文件 */
  8. #include <windows.h>
  9. #include <stdio.h>
  10. #include <tchar.h>
  11. /* 预定义 */
  12. #define MAX_KEY_LENGTH 255
  13. #define MAX_VALUE_NAME 16383
  14. /* ************************************
  15. * void QueryKey(HKEY hKey)
  16. * 功能 列举指定注册表项的子键
  17. **************************************/
  18. void QueryKey(HKEY hKey)
  19. {
  20. TCHAR achKey[MAX_KEY_LENGTH];
  21. DWORD cbName;
  22. TCHAR achClass[MAX_PATH] = TEXT("");
  23. DWORD cchClassName = MAX_PATH;
  24. DWORD cSubKeys=0;
  25. DWORD cbMaxSubKey;
  26. DWORD cchMaxClass;
  27. DWORD cValues;
  28. DWORD cchMaxValue;
  29. DWORD cbMaxValueData;
  30. DWORD cbSecurityDescriptor;
  31. FILETIME ftLastWriteTime;
  32. DWORD i, retCode;
  33. TCHAR achValue[MAX_VALUE_NAME];
  34. DWORD cchValue = MAX_VALUE_NAME;
  35. // 获取类名和数量
  36. retCode = RegQueryInfoKey(
  37. hKey, // 键的句柄
  38. achClass, // 类名
  39. &cchClassName, // 类名长度
  40. NULL, // 保留
  41. &cSubKeys, // 子键的数量
  42. &cbMaxSubKey, // 子键长度
  43. &cchMaxClass, // 类长度
  44. &cValues, // 子键键值数量
  45. &cchMaxValue, // 子键名长度
  46. &cbMaxValueData, // 键值长度
  47. &cbSecurityDescriptor, // 安全描述符
  48. &ftLastWriteTime); // 最后写时间
  49. // 列举子键
  50. if (cSubKeys)
  51. {
  52. printf( "\nNumber of subkeys: %d\n", cSubKeys);
  53. for (i=0; i<cSubKeys; i++)
  54. {
  55. cbName = MAX_KEY_LENGTH;
  56. retCode = RegEnumKeyEx(hKey, i,
  57. achKey,
  58. &cbName,
  59. NULL,
  60. NULL,
  61. NULL,
  62. &ftLastWriteTime);
  63. if (retCode == ERROR_SUCCESS)
  64. {
  65. printf(TEXT("(%d) %s\n"), i+1, achKey);
  66. }
  67. }
  68. }
  69. // 列举键值
  70. if (cValues)
  71. {
  72. printf( "\nNumber of values: %d\n", cValues);
  73. for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++)
  74. {
  75. cchValue = MAX_VALUE_NAME;
  76. achValue[0] = '\0';
  77. retCode = RegEnumValue(hKey, i,
  78. achValue,
  79. &cchValue,
  80. NULL,
  81. NULL,
  82. NULL,
  83. NULL);
  84. if (retCode == ERROR_SUCCESS )
  85. {
  86. printf(TEXT("(%d) %s\n"), i+1, achValue);
  87. }
  88. }
  89. }
  90. }
  91. /* ************************************
  92. * void AddKey(HKEY hKey)
  93. * 功能 增加一个子键,并设置键值
  94. **************************************/
  95. void AddKey(HKEY hKey)
  96. {
  97. HKEY hSubKey;
  98. DWORD dwKeyValue = 100;
  99. // 创建键
  100. RegCreateKey(hKey,"MySoftware",&hSubKey);
  101. // 设置键值
  102. if( ERROR_SUCCESS != RegSetValueEx(
  103. hSubKey,
  104. "TEST",
  105. 0,
  106. REG_DWORD,
  107. (LPBYTE)&dwKeyValue,
  108. sizeof(DWORD)))
  109. {
  110. printf("error\n");
  111. }
  112. }
  113. /* ************************************
  114. * void main(void)
  115. * 功能 打开键,获得键句柄
  116. **************************************/
  117. void main(void)
  118. {
  119. HKEY hTestKey;
  120. if( RegOpenKeyEx( HKEY_CURRENT_USER,
  121. TEXT("SOFTWARE"),
  122. 0,
  123. KEY_READ | KEY_WRITE,
  124. &hTestKey) == ERROR_SUCCESS
  125. )
  126. {
  127. // 增加键
  128. AddKey(hTestKey);
  129. // 列举子键
  130. QueryKey(hTestKey);
  131. }
  132. }
AddKey()函数:










posted @ 2016-03-20 19:16  hungryvampire  阅读(1717)  评论(0编辑  收藏  举报