Source code of Windows XP (NT5)
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.

351 lines
7.7 KiB

  1. /******************************************************************************\
  2. * *
  3. * RegistryAPi.C - Adapter control related code. *
  4. * *
  5. * Copyright (c) C-Cube Microsystems 1996 - 1999 *
  6. * All Rights Reserved. *
  7. * *
  8. * Use of C-Cube Microsystems code is governed by terms and conditions *
  9. * stated in the accompanying licensing statement. *
  10. * *
  11. \******************************************************************************/
  12. // C related include files.
  13. #include <strmini.h>
  14. // typedef HANDLE HKEY;
  15. #include "Registry.h"
  16. static BOOL
  17. GetRegistryKey( // Get registry key
  18. PTSTR pszSection, // Pointer to section.
  19. PTSTR pszEntry, // Pointer to entry.
  20. HANDLE pszPath, // Registry Key Handle. If NULL default path is used.
  21. HKEY * phKey); // Pointer to receive the key.
  22. int // Return value read from registry.
  23. REG_GetPrivateProfileInt( // Read int value from registry.
  24. PTSTR pszSection, // Pointer to section.
  25. PTSTR pszEntry, // Pointer to entry.
  26. int nDefault, // Default value.
  27. HANDLE pszPath) // Registry Key Handle If NULL default path is used.
  28. {
  29. HKEY hKey = NULL;
  30. DWORD dwType = REG_DWORD;
  31. DWORD dwSize = sizeof(DWORD);
  32. DWORD dwData = 0;
  33. LPBYTE lpData = (LPBYTE)&dwData;
  34. RTL_QUERY_REGISTRY_TABLE query[] =
  35. {
  36. {
  37. NULL,
  38. RTL_QUERY_REGISTRY_DIRECT,
  39. pszEntry,
  40. (PVOID)lpData,
  41. dwType,
  42. NULL,
  43. 0
  44. },
  45. {
  46. NULL,
  47. 0,
  48. NULL,
  49. NULL,
  50. 0,
  51. NULL,
  52. 0
  53. }
  54. };
  55. if (pszEntry == NULL)
  56. return 0;
  57. if (!GetRegistryKey(pszSection, pszEntry, pszPath, &hKey))
  58. return nDefault;
  59. #ifdef UNICODE
  60. if (!NT_SUCCESS(RtlQueryRegistryValues(RTL_REGISTRY_HANDLE,
  61. (PCWSTR)hKey, // key handle
  62. query,
  63. NULL,
  64. NULL)))
  65. #endif
  66. return nDefault;
  67. ZwClose( hKey );
  68. return (int)dwData;
  69. }
  70. BOOL // Return TRUE on success, else FALSE.
  71. REG_WritePrivateProfileInt( // Write int value to registry.
  72. PTSTR pszSection, // Pointer to section.
  73. PTSTR pszEntry, // Pointer to entry.
  74. int nValue, // Value to be written.
  75. HANDLE pszPath) // Registry Key Handle. If NULL default path is used.
  76. {
  77. HKEY hKey = NULL;
  78. DWORD dwType = REG_DWORD;
  79. DWORD dwSize = sizeof(DWORD);
  80. DWORD dwData = (DWORD)nValue;
  81. if (pszEntry == NULL)
  82. return FALSE;
  83. if (!GetRegistryKey(pszSection, pszEntry, pszPath, &hKey))
  84. return FALSE;
  85. #ifdef UNICODE
  86. if (!NT_SUCCESS(RtlWriteRegistryValue(RTL_REGISTRY_HANDLE,
  87. (PCWSTR)hKey,
  88. pszEntry,
  89. dwType,
  90. (PVOID)&dwData,
  91. dwSize)))
  92. #endif
  93. return FALSE;
  94. ZwClose( hKey );
  95. return TRUE;
  96. }
  97. long // Return value read from registry.
  98. REG_GetPrivateProfileLong( // Read int value from registry.
  99. PTSTR pszSection, // Pointer to section.
  100. PTSTR pszEntry, // Pointer to entry.
  101. long lDefault, // Default value.
  102. HANDLE pszPath) // Registry path. If NULL default path is used.
  103. {
  104. HKEY hKey = NULL;
  105. DWORD dwType = REG_DWORD;
  106. DWORD dwSize = sizeof(DWORD);
  107. DWORD dwData = 0;
  108. LPBYTE lpData = (LPBYTE)&dwData;
  109. RTL_QUERY_REGISTRY_TABLE query[] =
  110. {
  111. {
  112. NULL,
  113. RTL_QUERY_REGISTRY_DIRECT,
  114. pszEntry,
  115. (PVOID)lpData,
  116. dwType,
  117. &lDefault,
  118. sizeof(long)
  119. },
  120. {
  121. NULL,
  122. 0,
  123. NULL,
  124. NULL,
  125. 0,
  126. NULL,
  127. 0
  128. }
  129. };
  130. if (pszEntry == NULL)
  131. return 0;
  132. if (!GetRegistryKey(pszSection, pszEntry, pszPath, &hKey))
  133. return lDefault;
  134. #ifdef UNICODE
  135. if (!NT_SUCCESS(RtlQueryRegistryValues(RTL_REGISTRY_HANDLE,
  136. (PCWSTR)hKey, // key handle
  137. query,
  138. NULL,
  139. NULL)))
  140. #endif
  141. return lDefault;
  142. ZwClose( hKey );
  143. return (long)dwData;
  144. }
  145. BOOL // Return TRUE on success, else FALSE.
  146. REG_WritePrivateProfileLong( // Write long value to registry.
  147. PTSTR pszSection, // Pointer to section.
  148. PTSTR pszEntry, // Pointer to entry.
  149. long lValue, // Value to be written.
  150. HANDLE pszPath) // Registry path. If NULL default path is used.
  151. {
  152. HKEY hKey = NULL;
  153. DWORD dwType = REG_DWORD;
  154. DWORD dwSize = sizeof(DWORD);
  155. DWORD dwData = (DWORD)lValue;
  156. if (pszEntry == NULL)
  157. return FALSE;
  158. if (!GetRegistryKey(pszSection, pszEntry, pszPath, &hKey))
  159. return FALSE;
  160. #ifdef UNICODE
  161. if (!NT_SUCCESS(RtlWriteRegistryValue(RTL_REGISTRY_HANDLE,
  162. (PCWSTR)hKey,
  163. pszEntry,
  164. dwType,
  165. (PVOID)&dwData,
  166. dwSize)))
  167. #endif
  168. return FALSE;
  169. ZwClose( hKey );
  170. return TRUE;
  171. }
  172. BOOL // Return # of chars read.
  173. REG_GetPrivateProfileString( // Read string from registry.
  174. PTSTR pszSection, // Pointer to section.
  175. PTSTR pszEntry, // Pointer to entry.
  176. PTSTR pszDefault, // Pointer to default string.
  177. PTSTR pString, // Pointer to get the string.
  178. int nStringSize, // string size in bytes.
  179. HANDLE pszPath) // Registry path. If NULL default path is used.
  180. {
  181. HKEY hKey = NULL;
  182. DWORD dwType = REG_SZ;
  183. DWORD dwSize = (DWORD)nStringSize;
  184. DWORD dwData = (DWORD)pString;
  185. LPBYTE lpData = (LPBYTE)&dwData;
  186. UNICODE_STRING string = {
  187. (USHORT)0,
  188. (USHORT)dwSize,
  189. pString
  190. };
  191. RTL_QUERY_REGISTRY_TABLE query[] =
  192. {
  193. {
  194. NULL,
  195. RTL_QUERY_REGISTRY_DIRECT,
  196. pszEntry,
  197. (PVOID)&string,
  198. dwType,
  199. pszDefault,
  200. STRLEN(pszDefault) * sizeof(WCHAR)
  201. },
  202. {
  203. NULL,
  204. 0,
  205. NULL,
  206. NULL,
  207. 0,
  208. NULL,
  209. 0
  210. }
  211. };
  212. if (pszEntry == NULL)
  213. return 0;
  214. if (!GetRegistryKey(pszSection, pszEntry, pszPath, &hKey))
  215. return 0;
  216. if (!NT_SUCCESS(RtlQueryRegistryValues(RTL_REGISTRY_HANDLE,
  217. (PCWSTR)hKey,
  218. query,
  219. NULL,
  220. NULL)))
  221. return 0;
  222. ZwClose( hKey );
  223. return (int)(STRLEN(pString));
  224. }
  225. int // Return # of chars written.
  226. REG_WritePrivateProfileString( // Write the string to registry.
  227. PTSTR pszSection, // Pointer to section.
  228. PTSTR pszEntry, // Pointer to entry.
  229. PTSTR pString, // Pointer to get the string.
  230. HANDLE pszPath) // Registry path. If NULL default path is used.
  231. {
  232. HKEY hKey = NULL;
  233. DWORD dwType = REG_SZ;
  234. DWORD dwSize = (DWORD)(STRLEN(pString));
  235. DWORD dwData = (DWORD)pString;
  236. if (pszEntry == NULL)
  237. return 0;
  238. //if (!GetRegistryKey(pszSection, pszEntry, pszPath, &hKey))
  239. // return 0;
  240. if (!NT_SUCCESS(RtlWriteRegistryValue(RTL_REGISTRY_HANDLE,
  241. (PCWSTR)hKey,
  242. pszEntry,
  243. dwType,
  244. (PVOID)dwData, // note string IS address
  245. dwSize)))
  246. return 0;
  247. ZwClose( hKey );
  248. return (int)(STRLEN(pString));
  249. }
  250. static BOOL // Return TRUE on success, else FALSE
  251. GetRegistryKey( // Get registry key
  252. PTSTR pszSection, // Pointer to section.
  253. PTSTR pszEntry, // Pointer to entry.
  254. HANDLE pszPath, // Registry path. If NULL default path is used.
  255. HKEY * phKey) // Pointer to receive the key.
  256. {
  257. NTSTATUS ntStatus;
  258. BOOL fRet=TRUE;
  259. UNICODE_STRING ustr;
  260. OBJECT_ATTRIBUTES objectAttributes;
  261. RtlInitUnicodeString( &ustr, pszSection);
  262. InitializeObjectAttributes(
  263. &objectAttributes,
  264. &ustr,
  265. OBJ_CASE_INSENSITIVE,
  266. pszPath,
  267. NULL);
  268. //ntStatus = ZwCreateKey( phKey,
  269. // KEY_ALL_ACCESS,
  270. // &objectAttributes,
  271. // 0,
  272. // NULL,
  273. // REG_OPTION_NON_VOLATILE,
  274. // NULL);
  275. //
  276. // Only open existing key
  277. //
  278. ntStatus = ZwOpenKey( phKey,
  279. KEY_ALL_ACCESS,
  280. &objectAttributes);
  281. if ( !NT_SUCCESS( ntStatus )) {
  282. *phKey = NULL;
  283. fRet = FALSE;
  284. }
  285. return fRet;
  286. }