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.

221 lines
6.2 KiB

  1. /* File: D:\WACKER\tdll\registry.c (Created: 26-Nov-1996 by cab)
  2. *
  3. * Copyright 1996 by Hilgraeve Inc. -- Monroe, MI
  4. * All rights reserved
  5. *
  6. * Description:
  7. * Declares the functions used for manipulating the Windows 95
  8. * system registry.
  9. *
  10. * An explanation of registry terms:
  11. *
  12. * The Windows 95 registry uses the terms "keys", "values",
  13. * and "data". The way the registry stores information can
  14. * best be described as a folder analogy.
  15. *
  16. * Keys are the equivalent of folders. The can contain other
  17. * keys (subkeys) or values.
  18. *
  19. * Values are the equivalent of documents. They contain data.
  20. *
  21. * The data is the actual contents of the document, i.e. it
  22. * is the information we are interested in.
  23. *
  24. * An example:
  25. *
  26. * HyperTerminal uses the registry to store the value of the
  27. * "Don't ask me this question" check box of the "Default Telnet
  28. * App" dialog.
  29. *
  30. * The key for this is "HKEY_LOCAL_MACHINE\SOFTWARE\Hilgraeve\
  31. * HyperTerminal PE\3.0".
  32. *
  33. * The value for this is "Telnet Check".
  34. *
  35. * The data for this will be either 0 or 1 depending on if the
  36. * user wants HT to check if it is the default telnet app.
  37. *
  38. * $Revision: 3 $
  39. * $Date: 3/26/02 8:59a $
  40. */
  41. #include <windows.h>
  42. #pragma hdrstop
  43. #include "assert.h"
  44. #include "stdtyp.h"
  45. #include "htchar.h"
  46. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  47. * FUNCTION:
  48. * htRegOpenKey
  49. *
  50. * DESCRIPTION:
  51. * Opens the specified key. If the key does not exist, it will be
  52. * created.
  53. *
  54. * PARAMETERS:
  55. * hKey - A pointer to an opened key.
  56. * pszSubKey - The name of the subkey to open.
  57. * samAccess - The type of access desired for the key.
  58. * phOpenKey - Pointer to the opened key.
  59. *
  60. * RETURNS:
  61. * ERROR_SUCCESS if successful, otherwise an error value.
  62. *
  63. * AUTHOR: C. Baumgartner, 12/06/96
  64. */
  65. long htRegOpenKey(HKEY hKey, LPCTSTR pszSubKey, REGSAM samAccess, HKEY* phOpenKey)
  66. {
  67. DWORD dwDisposition = 0;
  68. // Instead of calling RegOpenKeyEx, call RegCreateKeyEx which will return
  69. // an open in key, but will also create a key that does not exist.
  70. //
  71. return RegCreateKeyEx(hKey, pszSubKey, 0, 0, REG_OPTION_NON_VOLATILE,
  72. samAccess, NULL, phOpenKey, &dwDisposition);
  73. }
  74. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  75. * FUNCTION:
  76. * htRegQueryValue
  77. *
  78. * DESCRIPTION:
  79. * A generic function to get a value from the registry.
  80. *
  81. * PARAMETERS:
  82. * hKey - A pointer to an opened key.
  83. * pszSubKey - The name of the subkey to open.
  84. * pszValue - The name of the value to query.
  85. * pData - The value's data.
  86. * pdwDataSize - Upon input this must be the size of pData,
  87. * on exit this will be the size of the data
  88. * read.
  89. *
  90. * RETURNS:
  91. * 0 if successful, -1 if error.
  92. *
  93. * AUTHOR: C. Baumgartner, 11/26/96
  94. */
  95. INT_PTR htRegQueryValue(HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValue,
  96. LPBYTE pData, LPDWORD pdwDataSize)
  97. {
  98. long lResult = ERROR_SUCCESS;
  99. HKEY hSubKey = 0;
  100. // Open the sub key with the given name.
  101. //
  102. lResult = htRegOpenKey(hKey, pszSubKey, KEY_READ, &hSubKey);
  103. if ( lResult == ERROR_SUCCESS )
  104. {
  105. // Get value of that subkey.
  106. //
  107. lResult = RegQueryValueEx(hSubKey, pszValue, NULL, NULL,
  108. pData, pdwDataSize);
  109. }
  110. if (hSubKey != 0)
  111. {
  112. RegCloseKey(hSubKey);
  113. }
  114. return lResult == ERROR_SUCCESS ? 0 : -1;
  115. }
  116. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  117. * FUNCTION:
  118. * regSetStringValue
  119. *
  120. * DESCRIPTION:
  121. * A generic function to set the value of a registry entry. This value
  122. * is a null-terminated string.
  123. *
  124. * PARAMETERS:
  125. * hKey - A pointer to an opened key.
  126. * pszSubKey - The name of the subkey to open.
  127. * pszValue - The name of the value to query.
  128. * pszData - The value's *string* data.
  129. *
  130. * RETURNS:
  131. * 0 if successful, -1 if error.
  132. *
  133. * AUTHOR: C. Baumgartner, 11/27/96
  134. */
  135. INT_PTR regSetStringValue(HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValue,
  136. LPCTSTR pszData)
  137. {
  138. long lResult = ERROR_SUCCESS;
  139. DWORD dwSize = 0;
  140. HKEY hSubKey = 0;
  141. // Open the sub key with the given name.
  142. //
  143. lResult = htRegOpenKey(hKey, pszSubKey, KEY_WRITE, &hSubKey);
  144. if ( lResult == ERROR_SUCCESS )
  145. {
  146. // The size of the string must include the null terminator.
  147. //
  148. dwSize = StrCharGetByteCount(pszData) + sizeof(TCHAR);
  149. // Set value of that subkey.
  150. //
  151. lResult = RegSetValueEx(hSubKey, pszValue, 0, REG_SZ,
  152. pszData, dwSize);
  153. }
  154. if (hSubKey != 0)
  155. {
  156. RegCloseKey(hSubKey);
  157. }
  158. return lResult == ERROR_SUCCESS ? 0 : -1;
  159. }
  160. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  161. * FUNCTION:
  162. * regSetDwordValue
  163. *
  164. * DESCRIPTION:
  165. * A generic function to set the value of a registry entry. This value
  166. * is a doubleword (32 bits).
  167. *
  168. * PARAMETERS:
  169. * hKey - A pointer to an opened key.
  170. * pszSubKey - The name of the subkey to open.
  171. * pszValue - The name of the value to query.
  172. * dwData - The value's *doubleword* data.
  173. *
  174. * RETURNS:
  175. * 0 if successful, -1 if error.
  176. *
  177. * AUTHOR: C. Baumgartner, 11/27/96
  178. */
  179. INT_PTR regSetDwordValue(HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValue,
  180. DWORD dwData)
  181. {
  182. long lResult = ERROR_SUCCESS;
  183. HKEY hSubKey = 0;
  184. // Open the sub key with the given name.
  185. //
  186. lResult = htRegOpenKey(hKey, pszSubKey, KEY_WRITE, &hSubKey);
  187. if ( lResult == ERROR_SUCCESS )
  188. {
  189. // Set value of that subkey.
  190. //
  191. lResult = RegSetValueEx(hSubKey, pszValue, 0, REG_DWORD,
  192. (LPBYTE)&dwData, sizeof(dwData));
  193. }
  194. if (hSubKey != 0)
  195. {
  196. RegCloseKey(hSubKey);
  197. }
  198. return lResult == ERROR_SUCCESS ? 0 : -1;
  199. }