Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

285 lines
7.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: GetReg.cxx
  7. //
  8. // Contents: Routines to get the registry, and dump the contents into
  9. // a text file.
  10. //
  11. //
  12. // Objects:
  13. //
  14. // Coupling:
  15. //
  16. // Notes:
  17. //
  18. // History: 9/21/00 SHeffner Created
  19. //
  20. // 03-May-2001 WeiyouC Rewritten
  21. //
  22. //----------------------------------------------------------------------------
  23. //+---------------------------------------------------------------------------
  24. //
  25. // Common Includes
  26. //
  27. //----------------------------------------------------------------------------
  28. #include "SrHeader.hxx"
  29. //+---------------------------------------------------------------------------
  30. //
  31. // Function Prototypes
  32. //
  33. //----------------------------------------------------------------------------
  34. BOOL GetSRRegistry(LPTSTR ptszLogFile, LPTSTR ptszRegPath, BOOL fRecurse);
  35. //+---------------------------------------------------------------------------
  36. //
  37. // Global variables
  38. //
  39. //----------------------------------------------------------------------------
  40. //
  41. // A list of readable string names for the registery keys
  42. //
  43. LPTSTR tszRegStrings[] = {TEXT("REG_NONE"),
  44. TEXT("REG_SZ"),
  45. TEXT("REG_EXPAND_SZ"),
  46. TEXT("REG_BINARY"),
  47. TEXT("REG_DWORD or REG_DWORD_LITTLE_ENDIAN"),
  48. TEXT("REG_DWORD_BIG_ENDIAN"),
  49. TEXT("REG_LINK"),
  50. TEXT("REG_MULTI_SZ"),
  51. TEXT("REG_RESOURCE_LIST"),
  52. TEXT("REG_FULL_RESOURCE_DESCRIPTOR"),
  53. TEXT("REG_RESOURCE_REQUIREMENTS_LIST"),
  54. TEXT("REG_QWORD or REG_QWORD_LITTLE_ENDIAN")
  55. };
  56. //
  57. // List of the Registry keys that we are grabbing.
  58. // The first param is the Path from HKLM, the Second Param
  59. // is either FALSE for not recursing, or TRUE if you want to
  60. // recurse all of the sub keys.
  61. //
  62. struct _SR_REG_STRUCTURE_
  63. {
  64. LPTSTR ptszRegPath;
  65. BOOL fRecurse;
  66. } SRRegKeys [] =
  67. {
  68. {TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion"), FALSE},
  69. {TEXT("Software\\Microsoft\\Windows NT\\CurrentVersion\\SystemRestore"), TRUE},
  70. {TEXT("System\\CurrentControlSet\\Services\\SR"), TRUE},
  71. {TEXT("System\\CurrentControlSet\\Services\\SRService"), TRUE},
  72. {TEXT("Software\\Policies\\Microsoft\\Windows NT\\SystemRestore"), TRUE},
  73. {TEXT(""), FALSE}
  74. };
  75. //+---------------------------------------------------------------------------
  76. //
  77. // Function: GetSRRegistry
  78. //
  79. // Synopsis: Routine will recursivly call this routine to enumerate the keys
  80. // and values for the registry
  81. //
  82. // Arguments: [ptszLogFile] -- log file name
  83. // [ptszRegPath] -- registery path
  84. // [fRecurse] -- flag indicates if I should recurse into sub paths
  85. //
  86. // Returns: TRUE if successful
  87. //
  88. // History: 9/21/00 SHeffner Created
  89. //
  90. // 03-May-2001 WeiyouC Rewitten
  91. //
  92. //----------------------------------------------------------------------------
  93. BOOL GetSRRegistry(LPTSTR ptszLogFile,
  94. LPTSTR ptszRegPath,
  95. BOOL fRecurse)
  96. {
  97. DWORD dwIndex = 0;
  98. DWORD dwValueSize = MAX_PATH +1;
  99. DWORD dwDataSize = MAX_PATH +1;
  100. DWORD dwType = 0;
  101. long lResult = 0;;
  102. FILE* fpLog = NULL;
  103. LPTSTR ptszString = NULL;
  104. HKEY hKey;
  105. TCHAR tszKey[MAX_PATH +1];
  106. TCHAR tszValue[MAX_PATH +1];
  107. TCHAR tszNewRegPath[MAX_PATH +1];
  108. //
  109. // Open the Log file for append
  110. //
  111. fpLog = _tfopen(ptszLogFile, TEXT("a"));
  112. if (NULL == fpLog)
  113. {
  114. goto ErrReturn;
  115. }
  116. //
  117. // Log current path, and then open the registry hive,
  118. // and start enumerating the Values.
  119. //
  120. fprintf(fpLog, "\n[%S]\n", ptszRegPath);
  121. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  122. ptszRegPath,
  123. 0,
  124. KEY_READ,
  125. &hKey);
  126. if (ERROR_SUCCESS != lResult)
  127. {
  128. goto ErrReturn;
  129. }
  130. lResult = RegEnumValue(hKey,
  131. dwIndex,
  132. tszKey,
  133. &dwDataSize,
  134. 0,
  135. &dwType,
  136. (unsigned char *) tszValue,
  137. &dwValueSize);
  138. while (ERROR_SUCCESS == lResult )
  139. {
  140. ptszString = tszRegStrings[dwType];
  141. //
  142. // If it is type REG_DWORD or REG_DWORD_LITTLE_ENDIAN, then we
  143. // do the special type casting. If not then we just pass it through
  144. // as a string
  145. //
  146. if ((REG_DWORD == dwType) ||
  147. (REG_DWORD_LITTLE_ENDIAN == dwType))
  148. {
  149. fprintf(fpLog,
  150. "\"%S\"=%S:%lu\n",
  151. tszKey,
  152. ptszString,
  153. (DWORD &) tszValue);
  154. }
  155. else
  156. {
  157. fprintf(fpLog,
  158. "\"%S\"=%S:%S\n",
  159. tszKey,
  160. ptszString,
  161. tszValue);
  162. }
  163. //
  164. // Update local variables for next iteration.
  165. //
  166. dwDataSize = dwValueSize = MAX_PATH +1;
  167. dwIndex ++;
  168. lResult = RegEnumValue(hKey,
  169. dwIndex,
  170. tszKey,
  171. &dwDataSize,
  172. 0,
  173. &dwType,
  174. (unsigned char *) tszValue,
  175. &dwValueSize);
  176. }
  177. //
  178. // Close out the file, for next recursion loop.
  179. //
  180. fclose(fpLog);
  181. //
  182. // Now lets find all of the Key's under this key,
  183. // and start a another enumeration for each one found
  184. //
  185. if (fRecurse)
  186. {
  187. dwIndex = 0;
  188. dwDataSize = MAX_PATH +1;
  189. lResult = RegEnumKey(hKey, dwIndex, tszKey, dwDataSize);
  190. while (ERROR_SUCCESS == lResult)
  191. {
  192. //
  193. // Build the path, and then call this function again.
  194. //
  195. _tcscpy(tszNewRegPath, ptszRegPath);
  196. _tcscat(tszNewRegPath, TEXT("\\"));
  197. _tcscat(tszNewRegPath, tszKey);
  198. GetSRRegistry(ptszLogFile, tszNewRegPath, fRecurse);
  199. //
  200. // Now do next run through.
  201. //
  202. dwDataSize = MAX_PATH + 1;
  203. dwIndex ++;
  204. lResult = RegEnumKey(hKey, dwIndex, tszKey, dwDataSize);
  205. }
  206. }
  207. ErrReturn:
  208. if (NULL != fpLog)
  209. {
  210. fclose(fpLog);
  211. }
  212. RegCloseKey(hKey);
  213. return TRUE;
  214. }
  215. //+---------------------------------------------------------------------------
  216. //
  217. // Function: GetSRRegInfo
  218. //
  219. // Synopsis: Get SR registery settings and dump it to the log file
  220. //
  221. // Arguments: [ptszLogFile] -- log file name
  222. //
  223. // Returns: HRESULT
  224. //
  225. // History: 9/21/00 SHeffner Created
  226. //
  227. // 03-May-2001 WeiyouC Rewitten
  228. //
  229. //----------------------------------------------------------------------------
  230. HRESULT GetSRRegInfo(LPTSTR ptszLogFile)
  231. {
  232. HRESULT hr = S_OK;
  233. int i = 0;
  234. BOOL fOK = FALSE;
  235. DH_VDATEPTRIN(ptszLogFile, TCHAR);
  236. while (NULL != *(SRRegKeys[i].ptszRegPath))
  237. {
  238. fOK = GetSRRegistry(ptszLogFile,
  239. SRRegKeys[i].ptszRegPath,
  240. SRRegKeys[i].fRecurse);
  241. DH_ABORTIF(!fOK,
  242. E_FAIL,
  243. TEXT("GetSRRegistry"));
  244. i++;
  245. }
  246. ErrReturn:
  247. return hr;
  248. }