registry.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. // ERROR.C
  39. //
  40. // This file contains the functions needed to install the httpd server.
  41. // They are as follows.
  42. //
  43. // getreg.c
  44. //
  45. // This file has the function needed to get a particular value of a key from the registry...
  46. // 1/16/95 aruna
  47. //
  48. #include <windows.h>
  49. #include "nt/ntos.h"
  50. BOOL NS_WINAPI
  51. REG_CheckIfKeyExists( HKEY hKey, LPCTSTR key )
  52. {
  53. HKEY hQueryKey;
  54. if (RegOpenKeyEx(hKey, key, 0, KEY_ALL_ACCESS,
  55. &hQueryKey) != ERROR_SUCCESS) {
  56. return FALSE;
  57. }
  58. RegCloseKey(hQueryKey);
  59. return TRUE;
  60. }
  61. BOOL NS_WINAPI
  62. REG_GetRegistryParameter(
  63. HKEY hKey,
  64. LPCTSTR registryKey,
  65. LPTSTR QueryValueName,
  66. LPDWORD ValueType,
  67. LPBYTE ValueBuffer,
  68. LPDWORD ValueBufferSize
  69. )
  70. {
  71. HKEY hQueryKey;
  72. if (RegOpenKeyEx(hKey, registryKey, 0, KEY_ALL_ACCESS,
  73. &hQueryKey) != ERROR_SUCCESS) {
  74. return FALSE;
  75. }
  76. if (RegQueryValueEx(hQueryKey, QueryValueName, 0,
  77. ValueType, ValueBuffer, ValueBufferSize) != ERROR_SUCCESS) {
  78. RegCloseKey(hQueryKey);
  79. return FALSE;
  80. }
  81. RegCloseKey(hQueryKey);
  82. return TRUE;
  83. }
  84. BOOL NS_WINAPI
  85. REG_CreateKey( HKEY hKey, LPCTSTR registryKey )
  86. {
  87. HKEY hNewKey;
  88. if ( RegCreateKey (hKey, registryKey, &hNewKey) != ERROR_SUCCESS) {
  89. return FALSE;
  90. }
  91. RegCloseKey(hNewKey);
  92. return TRUE;
  93. }
  94. BOOL NS_WINAPI
  95. REG_DeleteKey( HKEY hKey, LPCTSTR registryKey )
  96. {
  97. HKEY hQueryKey;
  98. DWORD dwNumberOfSubKeys;
  99. char registrySubKey[256];
  100. DWORD i;
  101. /* if key does not exist, then consider it deleted */
  102. if ( !REG_CheckIfKeyExists( hKey, registryKey ) )
  103. return TRUE;
  104. if ( !REG_GetSubKeysInfo( hKey, registryKey, &dwNumberOfSubKeys, NULL ) )
  105. return FALSE;
  106. if ( dwNumberOfSubKeys ) {
  107. if (RegOpenKeyEx(hKey, registryKey, 0, KEY_ALL_ACCESS,
  108. &hQueryKey) != ERROR_SUCCESS) {
  109. return FALSE;
  110. }
  111. // loop through all sub keys and delete the subkeys (recursion)
  112. for ( i=0; i<dwNumberOfSubKeys; i++ ) {
  113. if ( RegEnumKey( hQueryKey, 0, registrySubKey, sizeof(registrySubKey) ) != ERROR_SUCCESS) {
  114. RegCloseKey(hQueryKey);
  115. return FALSE;
  116. }
  117. if ( !REG_DeleteKey( hQueryKey, registrySubKey ) ) {
  118. RegCloseKey(hQueryKey);
  119. return FALSE;
  120. }
  121. }
  122. RegCloseKey(hQueryKey);
  123. }
  124. if ( RegDeleteKey (hKey, registryKey) != ERROR_SUCCESS) {
  125. return FALSE;
  126. }
  127. return TRUE;
  128. }
  129. BOOL NS_WINAPI
  130. REG_GetSubKey( HKEY hKey, LPCTSTR registryKey, DWORD nSubKeyIndex, LPTSTR registrySubKeyBuffer, DWORD subKeyBufferSize )
  131. {
  132. HKEY hQueryKey;
  133. if (RegOpenKeyEx(hKey, registryKey, 0, KEY_ALL_ACCESS,
  134. &hQueryKey) != ERROR_SUCCESS) {
  135. return FALSE;
  136. }
  137. if ( RegEnumKey( hQueryKey, nSubKeyIndex, registrySubKeyBuffer, subKeyBufferSize ) != ERROR_SUCCESS) {
  138. RegCloseKey(hQueryKey);
  139. return FALSE;
  140. }
  141. RegCloseKey(hQueryKey);
  142. return TRUE;
  143. }
  144. BOOL NS_WINAPI
  145. REG_GetSubKeysInfo( HKEY hKey, LPCTSTR registryKey, LPDWORD lpdwNumberOfSubKeys, LPDWORD lpdwMaxSubKeyLength )
  146. {
  147. HKEY hQueryKey;
  148. char szClass[256]; // address of buffer for class string
  149. DWORD cchClass; // address of size of class string buffer
  150. DWORD cSubKeys; // address of buffer for number of subkeys
  151. DWORD cchMaxSubkey; // address of buffer for longest subkey name length
  152. DWORD cchMaxClass; // address of buffer for longest class string length
  153. DWORD cValues; // address of buffer for number of value entries
  154. DWORD cchMaxValueName; // address of buffer for longest value name length
  155. DWORD cbMaxValueData; // address of buffer for longest value data length
  156. DWORD cbSecurityDescriptor; // address of buffer for security descriptor length
  157. FILETIME ftLastWriteTime; // address of buffer for last write time
  158. if (RegOpenKeyEx(hKey, registryKey, 0, KEY_ALL_ACCESS,
  159. &hQueryKey) != ERROR_SUCCESS) {
  160. return FALSE;
  161. }
  162. if ( RegQueryInfoKey( hQueryKey, // handle of key to query
  163. (char*)&szClass, // address of buffer for class string
  164. &cchClass, // address of size of class string buffer
  165. NULL, // reserved
  166. &cSubKeys, // address of buffer for number of subkeys
  167. &cchMaxSubkey, // address of buffer for longest subkey name length
  168. &cchMaxClass, // address of buffer for longest class string length
  169. &cValues, // address of buffer for number of value entries
  170. &cchMaxValueName, // address of buffer for longest value name length
  171. &cbMaxValueData, // address of buffer for longest value data length
  172. &cbSecurityDescriptor, // address of buffer for security descriptor length
  173. &ftLastWriteTime // address of buffer for last write time
  174. ) != ERROR_SUCCESS) {
  175. RegCloseKey(hQueryKey);
  176. return FALSE;
  177. }
  178. // return desired information
  179. if ( lpdwNumberOfSubKeys )
  180. *lpdwNumberOfSubKeys = cSubKeys;
  181. if ( lpdwMaxSubKeyLength )
  182. *lpdwMaxSubKeyLength = cchMaxSubkey;
  183. RegCloseKey(hQueryKey);
  184. return TRUE;
  185. }
  186. BOOL NS_WINAPI
  187. REG_SetRegistryParameter(
  188. HKEY hKey,
  189. LPCTSTR registryKey,
  190. LPTSTR valueName,
  191. DWORD valueType,
  192. LPCTSTR ValueString,
  193. DWORD valueStringLength
  194. )
  195. {
  196. HKEY hQueryKey;
  197. if (RegOpenKeyEx(hKey, registryKey, 0, KEY_ALL_ACCESS,
  198. &hQueryKey) != ERROR_SUCCESS) {
  199. return FALSE;
  200. }
  201. if ( RegSetValueEx( hQueryKey, valueName, 0, valueType, (CONST BYTE *)ValueString,
  202. valueStringLength ) != ERROR_SUCCESS) {
  203. RegCloseKey(hQueryKey);
  204. return FALSE;
  205. }
  206. RegCloseKey(hQueryKey);
  207. return TRUE;
  208. }
  209. BOOL NS_WINAPI
  210. REG_DeleteValue( HKEY hKey, LPCTSTR registryKey, LPCSTR valueName )
  211. {
  212. HKEY hQueryKey;
  213. DWORD ValueBufferSize = 256;
  214. char ValueBuffer[256];
  215. DWORD i, ValueType;
  216. /* if key does not exist, then consider it deleted */
  217. if (RegOpenKeyEx(hKey, registryKey, 0, KEY_ALL_ACCESS,
  218. &hQueryKey) != ERROR_SUCCESS) {
  219. return FALSE;
  220. }
  221. /* if valuename does not exist, then consider it deleted */
  222. if (RegQueryValueEx(hQueryKey, valueName, 0,
  223. &ValueType, ValueBuffer, &ValueBufferSize) != ERROR_SUCCESS) {
  224. RegCloseKey(hQueryKey);
  225. return TRUE;
  226. }
  227. if (RegDeleteValue(hQueryKey, valueName) != ERROR_SUCCESS) {
  228. RegCloseKey(hQueryKey);
  229. return FALSE;
  230. }
  231. RegCloseKey(hQueryKey);
  232. return TRUE;
  233. }