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.

395 lines
7.0 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. registry.c
  5. Abstract:
  6. This module provides a generic table driven access
  7. to the registry.
  8. Author:
  9. Wesley Witt (wesw) 9-June-1996
  10. Revision History:
  11. --*/
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <tchar.h>
  16. #define StringSize(_s) (( _s ) ? (_tcslen( _s ) + 1) * sizeof(TCHAR) : 0)
  17. LPTSTR
  18. StringDup(
  19. LPTSTR String
  20. )
  21. {
  22. LPTSTR NewString;
  23. if (!String) {
  24. return NULL;
  25. }
  26. NewString = (LPTSTR) LocalAlloc( LPTR, (_tcslen( String ) + 1) * sizeof(TCHAR) );
  27. if (!NewString) {
  28. return NULL;
  29. }
  30. _tcscpy( NewString, String );
  31. return NewString;
  32. }
  33. HKEY
  34. OpenRegistryKey(
  35. HKEY hKey,
  36. LPTSTR KeyName,
  37. BOOL CreateNewKey,
  38. REGSAM SamDesired
  39. )
  40. {
  41. LONG Rslt;
  42. HKEY hKeyNew;
  43. DWORD Disposition;
  44. if (CreateNewKey) {
  45. Rslt = RegCreateKeyEx(
  46. hKey,
  47. KeyName,
  48. 0,
  49. NULL,
  50. REG_OPTION_NON_VOLATILE,
  51. SamDesired == 0 ? KEY_ALL_ACCESS : SamDesired,
  52. NULL,
  53. &hKeyNew,
  54. &Disposition
  55. );
  56. if (Rslt != ERROR_SUCCESS) {
  57. //
  58. // could not open the registry key
  59. //
  60. return NULL;
  61. }
  62. } else {
  63. Rslt = RegOpenKeyEx(
  64. hKey,
  65. KeyName,
  66. 0,
  67. SamDesired == 0 ? KEY_ALL_ACCESS : SamDesired,
  68. &hKeyNew
  69. );
  70. if (Rslt != ERROR_SUCCESS) {
  71. //
  72. // could not open the registry key
  73. //
  74. return NULL;
  75. }
  76. }
  77. return hKeyNew;
  78. }
  79. LPTSTR
  80. GetRegistryStringValue(
  81. HKEY hKey,
  82. DWORD RegType,
  83. LPTSTR ValueName,
  84. LPTSTR DefaultValue
  85. )
  86. {
  87. BOOL Success = FALSE;
  88. DWORD Size;
  89. LONG Rslt;
  90. DWORD Type;
  91. LPBYTE Buffer = NULL;
  92. LPBYTE ExpandBuffer = NULL;
  93. Rslt = RegQueryValueEx(
  94. hKey,
  95. ValueName,
  96. NULL,
  97. &Type,
  98. NULL,
  99. &Size
  100. );
  101. if (Rslt != ERROR_SUCCESS) {
  102. if (Rslt == ERROR_FILE_NOT_FOUND) {
  103. Size = StringSize( DefaultValue );
  104. } else {
  105. goto exit;
  106. }
  107. }
  108. Buffer = (LPBYTE) LocalAlloc( LPTR, Size );
  109. if (!Buffer) {
  110. goto exit;
  111. }
  112. Rslt = RegQueryValueEx(
  113. hKey,
  114. ValueName,
  115. NULL,
  116. &Type,
  117. Buffer,
  118. &Size
  119. );
  120. if (Rslt != ERROR_SUCCESS) {
  121. if (Rslt != ERROR_FILE_NOT_FOUND) {
  122. goto exit;
  123. }
  124. //
  125. // create the value since it doesn't exist
  126. //
  127. _tcscpy( (LPTSTR) Buffer, DefaultValue );
  128. Rslt = RegSetValueEx(
  129. hKey,
  130. ValueName,
  131. 0,
  132. RegType,
  133. Buffer,
  134. Size
  135. );
  136. if (Rslt != ERROR_SUCCESS) {
  137. //
  138. // could not set the registry value
  139. //
  140. goto exit;
  141. }
  142. }
  143. if (RegType == REG_EXPAND_SZ) {
  144. Rslt = ExpandEnvironmentStrings( (LPTSTR) Buffer, NULL, 0 );
  145. if (!Rslt) {
  146. goto exit;
  147. }
  148. ExpandBuffer = (LPBYTE) LocalAlloc( LPTR, (Rslt + 1) * sizeof(WCHAR) );
  149. if (!ExpandBuffer) {
  150. goto exit;
  151. }
  152. Rslt = ExpandEnvironmentStrings( (LPTSTR) Buffer, (LPTSTR) ExpandBuffer, Rslt );
  153. if (Rslt == 0) {
  154. LocalFree( ExpandBuffer );
  155. goto exit;
  156. }
  157. LocalFree( Buffer );
  158. Buffer = ExpandBuffer;
  159. }
  160. Success = TRUE;
  161. exit:
  162. if (!Success) {
  163. LocalFree( Buffer );
  164. return StringDup( DefaultValue );
  165. }
  166. return (LPTSTR) Buffer;
  167. }
  168. LPTSTR
  169. GetRegistryString(
  170. HKEY hKey,
  171. LPTSTR ValueName,
  172. LPTSTR DefaultValue
  173. )
  174. {
  175. return GetRegistryStringValue( hKey, REG_SZ, ValueName, DefaultValue );
  176. }
  177. LPTSTR
  178. GetRegistryStringExpand(
  179. HKEY hKey,
  180. LPTSTR ValueName,
  181. LPTSTR DefaultValue
  182. )
  183. {
  184. return GetRegistryStringValue( hKey, REG_EXPAND_SZ, ValueName, DefaultValue );
  185. }
  186. DWORD
  187. GetRegistryDword(
  188. HKEY hKey,
  189. LPTSTR ValueName
  190. )
  191. {
  192. DWORD Size = sizeof(DWORD);
  193. LONG Rslt;
  194. DWORD Type;
  195. DWORD Value = 0;
  196. Rslt = RegQueryValueEx(
  197. hKey,
  198. ValueName,
  199. NULL,
  200. &Type,
  201. (LPBYTE) &Value,
  202. &Size
  203. );
  204. if (Rslt != ERROR_SUCCESS) {
  205. //
  206. // create the value since it doesn't exist
  207. //
  208. Value = 0;
  209. Rslt = RegSetValueEx(
  210. hKey,
  211. ValueName,
  212. 0,
  213. REG_DWORD,
  214. (LPBYTE) &Value,
  215. Size
  216. );
  217. if (Rslt != ERROR_SUCCESS) {
  218. //
  219. // could not set the registry value
  220. //
  221. Value = 0;
  222. }
  223. }
  224. return Value;
  225. }
  226. DWORD
  227. GetSubKeyCount(
  228. HKEY hKey
  229. )
  230. {
  231. DWORD KeyCount = 0;
  232. LONG Rval;
  233. Rval = RegQueryInfoKey( hKey, NULL, NULL, NULL, &KeyCount, NULL, NULL, NULL, NULL, NULL, NULL, NULL );
  234. if (Rval != ERROR_SUCCESS) {
  235. return 0;
  236. }
  237. return KeyCount;
  238. }
  239. DWORD
  240. GetMaxSubKeyLen(
  241. HKEY hKey
  242. )
  243. {
  244. DWORD MaxSubKeyLen = 0;
  245. LONG Rval;
  246. Rval = RegQueryInfoKey( hKey, NULL, NULL, NULL, NULL, &MaxSubKeyLen, NULL, NULL, NULL, NULL, NULL, NULL );
  247. if (Rval != ERROR_SUCCESS) {
  248. return 0;
  249. }
  250. return MaxSubKeyLen;
  251. }
  252. BOOL
  253. SetRegistryDword(
  254. HKEY hKey,
  255. LPTSTR ValueName,
  256. DWORD Value
  257. )
  258. {
  259. LONG Rslt;
  260. Rslt = RegSetValueEx(
  261. hKey,
  262. ValueName,
  263. 0,
  264. REG_DWORD,
  265. (LPBYTE) &Value,
  266. sizeof(DWORD)
  267. );
  268. if (Rslt != ERROR_SUCCESS) {
  269. return FALSE;
  270. }
  271. return TRUE;
  272. }
  273. BOOL
  274. SetRegistryStringValue(
  275. HKEY hKey,
  276. DWORD RegType,
  277. LPTSTR ValueName,
  278. LPTSTR Value,
  279. LONG Length
  280. )
  281. {
  282. LONG Rslt;
  283. Rslt = RegSetValueEx(
  284. hKey,
  285. ValueName,
  286. 0,
  287. RegType,
  288. (LPBYTE) Value,
  289. Length == -1 ? StringSize( Value ) : Length
  290. );
  291. if (Rslt != ERROR_SUCCESS) {
  292. return FALSE;
  293. }
  294. return TRUE;
  295. }
  296. BOOL
  297. SetRegistryString(
  298. HKEY hKey,
  299. LPTSTR ValueName,
  300. LPTSTR Value
  301. )
  302. {
  303. return SetRegistryStringValue( hKey, REG_SZ, ValueName, Value, -1 );
  304. }
  305. BOOL
  306. SetRegistryStringExpand(
  307. HKEY hKey,
  308. LPTSTR ValueName,
  309. LPTSTR Value
  310. )
  311. {
  312. return SetRegistryStringValue( hKey, REG_EXPAND_SZ, ValueName, Value, -1 );
  313. }
  314. BOOL
  315. SetRegistryStringMultiSz(
  316. HKEY hKey,
  317. LPTSTR ValueName,
  318. LPTSTR Value,
  319. DWORD Length
  320. )
  321. {
  322. return SetRegistryStringValue( hKey, REG_MULTI_SZ, ValueName, Value, Length );
  323. }