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.

252 lines
6.0 KiB

  1. /*++
  2. Copyright (c) 1985 - 1999, Microsoft Corporation
  3. Module Name:
  4. registry.cpp
  5. Abstract:
  6. This file implements the Registry Class.
  7. Author:
  8. Revision History:
  9. Notes:
  10. --*/
  11. #include "private.h"
  12. #include "registry.h"
  13. DWORD
  14. CRegistry::CreateKey(
  15. IN HKEY hKey,
  16. IN LPCTSTR lpSubKey,
  17. IN REGSAM access,
  18. IN LPSECURITY_ATTRIBUTES lpSecAttr,
  19. OUT LPDWORD pDisposition
  20. )
  21. /*++
  22. Routine Description:
  23. Create the registry key specified.
  24. Arguments:
  25. Return Value:
  26. --*/
  27. {
  28. if (_hKey != NULL) {
  29. RegCloseKey(_hKey);
  30. _hKey = NULL;
  31. }
  32. DWORD dwDisposition;
  33. LONG lResult = RegCreateKeyEx(hKey, // handle of an open key.
  34. lpSubKey, // address of subkey name.
  35. 0, // reserved.
  36. NULL, // address of class string.
  37. REG_OPTION_NON_VOLATILE, // special options flag.
  38. access, // desired security access.
  39. lpSecAttr, // address of key security structure.
  40. &_hKey, // address of buffer for opened handle.
  41. &dwDisposition); // address of disposition value buffer
  42. if (lResult != ERROR_SUCCESS) {
  43. _hKey = NULL;
  44. }
  45. if (pDisposition) {
  46. *pDisposition = dwDisposition;
  47. }
  48. return lResult;
  49. }
  50. DWORD
  51. CRegistry::OpenKey(
  52. IN HKEY hKey,
  53. IN LPCTSTR lpSubKey,
  54. IN REGSAM access
  55. )
  56. /*++
  57. Routine Description:
  58. Open the registry key specified.
  59. Arguments:
  60. Return Value:
  61. --*/
  62. {
  63. if (_hKey != NULL) {
  64. RegCloseKey(_hKey);
  65. _hKey = NULL;
  66. }
  67. LONG lResult = RegOpenKeyEx(hKey, // handle of open key.
  68. lpSubKey, // address of name of subkey to open
  69. 0, // reserved
  70. access, // security access mask
  71. &_hKey); // address of handle of open key
  72. if (lResult != ERROR_SUCCESS) {
  73. _hKey = NULL;
  74. }
  75. return lResult;
  76. }
  77. DWORD
  78. CRegistry::QueryInfoKey(
  79. IN REG_QUERY iType,
  80. OUT LPBYTE lpData
  81. )
  82. /*++
  83. Routine Description:
  84. Retrieves information about a specified registry key.
  85. Arguments:
  86. Return Value:
  87. --*/
  88. {
  89. DWORD cSubKeys, cbMaxSubKeyLen;
  90. LONG lResult = RegQueryInfoKey(_hKey, // handle to key to query
  91. NULL, // address of buffer for class string
  92. NULL, // address of size of class string buffer
  93. NULL, // reserved
  94. &cSubKeys, // address of buffer for number of subkeys
  95. &cbMaxSubKeyLen, // address of buffer for longest subkey name length
  96. NULL, // address of buffer for longest class string length
  97. NULL, // address of buffer for number of value entries
  98. NULL, // address of buffer for longest value name length
  99. NULL, // address of buffer for longest value data length
  100. NULL, // address of buffer for security descriptor length.
  101. NULL); // address of buffer for last write time
  102. switch (iType) {
  103. case REG_QUERY_NUMBER_OF_SUBKEYS:
  104. *((LPDWORD)lpData) = cSubKeys; break;
  105. case REG_QUERY_MAX_SUBKEY_LEN:
  106. *((LPDWORD)lpData) = cbMaxSubKeyLen; break;
  107. }
  108. return lResult;
  109. }
  110. DWORD
  111. CRegistry::GetFirstSubKey(
  112. OUT LPTSTR* lppStr,
  113. OUT LPDWORD lpdwSize
  114. )
  115. /*++
  116. Routine Description:
  117. Reads a first subkey for the key.
  118. Arguments:
  119. Return Value:
  120. --*/
  121. {
  122. _iEnumKeyIndex = 0;
  123. DWORD dwRet = QueryInfoKey(REG_QUERY_MAX_SUBKEY_LEN, (LPBYTE)&_dwMaxSubKeyLen);
  124. if (dwRet != ERROR_SUCCESS) {
  125. return dwRet;
  126. }
  127. return GetNextSubKey(lppStr, lpdwSize);
  128. }
  129. DWORD
  130. CRegistry::GetNextSubKey(
  131. OUT LPTSTR* lppStr,
  132. OUT LPDWORD lpdwSize
  133. )
  134. /*++
  135. Routine Description:
  136. Reads the next subkey for the key.
  137. Arguments:
  138. Return Value:
  139. --*/
  140. {
  141. *lpdwSize = 0;
  142. if (Allocate(*lpdwSize = (_dwMaxSubKeyLen+sizeof(TCHAR)) * sizeof(TCHAR)) == NULL) {
  143. return ERROR_NOT_ENOUGH_MEMORY;
  144. }
  145. DWORD lResult = RegEnumKeyEx(_hKey, // handle of key to enumrate
  146. _iEnumKeyIndex, // index of subkey to enumerate
  147. (LPTSTR)_pMemBlock, // address of buffer for subkey name
  148. lpdwSize, // address for size of subkey buffer
  149. 0, // reserved
  150. NULL, // address of buffer for class string
  151. NULL, // address for sieze of class buffer
  152. NULL); // address for time key last written to
  153. *lpdwSize += sizeof(TCHAR); // since null terminate is not included in the size.
  154. if (lResult == ERROR_SUCCESS) {
  155. *lppStr = (LPTSTR)_pMemBlock;
  156. _iEnumKeyIndex++;
  157. }
  158. return lResult;
  159. }
  160. void*
  161. CRegistry::Allocate(
  162. IN DWORD dwSize
  163. )
  164. {
  165. ASSERT(dwSize != 0);
  166. if (_pMemBlock) {
  167. Release();
  168. }
  169. _pMemBlock = new BYTE[dwSize];
  170. return _pMemBlock;
  171. }
  172. void
  173. CRegistry::Release(
  174. )
  175. {
  176. if (_pMemBlock) {
  177. delete [] _pMemBlock;
  178. }
  179. _pMemBlock = NULL;
  180. }