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.

171 lines
5.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: GetReg.cpp
  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. //----------------------------------------------------------------------------
  21. //+---------------------------------------------------------------------------
  22. //
  23. // Common Includes
  24. //
  25. //----------------------------------------------------------------------------
  26. #include <stdio.h>
  27. #include <windows.h>
  28. #include <stdlib.h>
  29. //+---------------------------------------------------------------------------
  30. //
  31. // Function Proto types
  32. //
  33. //----------------------------------------------------------------------------
  34. void RegType(DWORD dwType, WCHAR *szString);
  35. //+---------------------------------------------------------------------------
  36. //
  37. // Function: GetSRRegistry
  38. //
  39. // Synopsis: Routine will recursivly call this routine to enumerate the keys and values for the registry
  40. //
  41. // Arguments: [szFileName] -- Simple ANSI string to log file
  42. // [szPath] -- Simple ANSI string to the path to start at the registry
  43. // [bRecurse] -- Bool to indicate if I should recurse into sub paths
  44. //
  45. // Returns: true if successful
  46. //
  47. // History: 9/21/00 SHeffner Created
  48. //
  49. //
  50. //----------------------------------------------------------------------------
  51. bool GetSRRegistry(char *szFileName, WCHAR *szPath, bool bRecurse)
  52. {
  53. WCHAR szKeyString[_MAX_PATH +1], szValueString[_MAX_PATH +1], szString[_MAX_PATH +1];
  54. DWORD dwIndex=0, dwValueSize, dwDataSize, dwType;
  55. long lResult;
  56. HKEY mHkey;
  57. FILE *fStream;
  58. // Open the Log file for append
  59. fStream = fopen(szFileName, "a");
  60. //Initialize local variables before processing path
  61. dwIndex =0;
  62. dwDataSize = dwValueSize = _MAX_PATH +1;
  63. //log current path, and then open the registry hive, and start enumerating the Values.
  64. fprintf(fStream, "\n[%S]\n", szPath);
  65. lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szPath, 0, KEY_READ, &mHkey);
  66. lResult = RegEnumValue(mHkey, dwIndex, szKeyString, &dwDataSize, 0, &dwType, (unsigned char *)szValueString, &dwValueSize);
  67. while( ERROR_SUCCESS == lResult ) {
  68. RegType(dwType, szString);
  69. //BUGBUG.. If it is type 4, then we do the special type casting,
  70. // if not then we just pass it through as a string
  71. if (4 == dwType)
  72. fprintf(fStream, "\"%S\"=%S:%lu\n", szKeyString, szString, (DWORD &) szValueString);
  73. else
  74. fprintf(fStream, "\"%S\"=%S:%S\n", szKeyString, szString, (unsigned char *) szValueString);
  75. //Update local variables for next iteration.
  76. dwDataSize = dwValueSize = _MAX_PATH +1;
  77. dwIndex ++;
  78. lResult = RegEnumValue(mHkey, dwIndex, szKeyString, &dwDataSize, 0, &dwType, (unsigned char *)szValueString, &dwValueSize);
  79. }
  80. //Close out the file, for next recursion loop.
  81. fclose(fStream);
  82. //Now lets find all of the Key's under this key, and kick off a another enumeration for each one found
  83. if ( true == bRecurse ) {
  84. dwIndex = 0;
  85. dwDataSize = _MAX_PATH +1;
  86. lResult = RegEnumKey(mHkey, dwIndex, szKeyString, dwDataSize);
  87. while( ERROR_SUCCESS == lResult) {
  88. //Build the path, and then call this function again.
  89. wcscpy(szString, szPath);
  90. wcscat(szString, TEXT("\\"));
  91. wcscat(szString, szKeyString);
  92. GetSRRegistry(szFileName, szString, bRecurse);
  93. //Now do next run through.
  94. dwDataSize = _MAX_PATH +1;
  95. dwIndex ++;
  96. lResult = RegEnumKey(mHkey, dwIndex, szKeyString, dwDataSize);
  97. }
  98. }
  99. //Close key, and return back.
  100. RegCloseKey(mHkey);
  101. return true;
  102. }
  103. //+---------------------------------------------------------------------------
  104. //
  105. // Function: RegType
  106. //
  107. // Synopsis: Routine returns in the string pass in, the description of registry key it is
  108. //
  109. // Arguments: [dwType] -- DWord Type
  110. // [szString] -- Simple ANSI string receive a string description
  111. //
  112. // Returns: void
  113. //
  114. // History: 9/21/00 SHeffner Created
  115. //
  116. //----------------------------------------------------------------------------
  117. void RegType(DWORD dwType, WCHAR *szString)
  118. {
  119. switch(dwType) {
  120. case REG_BINARY:
  121. wcscpy(szString, TEXT("REG_BINARY"));
  122. break;
  123. case REG_DWORD:
  124. wcscpy(szString, TEXT("REG_DWORD"));
  125. break;
  126. case REG_DWORD_BIG_ENDIAN:
  127. wcscpy(szString, TEXT("REG_DWORD_BIG_ENDIAN"));
  128. break;
  129. case REG_EXPAND_SZ:
  130. wcscpy(szString, TEXT("REG_EXPAND_SZ"));
  131. break;
  132. case REG_LINK:
  133. wcscpy(szString, TEXT("REG_LINK"));
  134. break;
  135. case REG_MULTI_SZ:
  136. wcscpy(szString, TEXT("REG_MULTI_SZ"));
  137. break;
  138. case REG_NONE:
  139. wcscpy(szString, TEXT("REG_NONE"));
  140. break;
  141. case REG_QWORD:
  142. wcscpy(szString, TEXT("REG_QWORD"));
  143. break;
  144. case REG_RESOURCE_LIST:
  145. wcscpy(szString, TEXT("REG_RESOURCE_LIST"));
  146. break;
  147. case REG_SZ:
  148. wcscpy(szString, TEXT("REG_SZ"));
  149. break;
  150. default:
  151. wcscpy(szString, TEXT("UnKnown"));
  152. break;
  153. }
  154. }