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.

307 lines
7.6 KiB

  1. #include "stock.h"
  2. /////////////////////////////////////////////////////////////////////////////
  3. //
  4. //
  5. // SHRegSubKeyExists
  6. //
  7. //
  8. STDAPI_(BOOL)
  9. SHRegSubKeyExistsA(
  10. IN HKEY hkey,
  11. IN LPCSTR pszSubKey)
  12. {
  13. LONG lr;
  14. HKEY hkSubKey;
  15. lr = RegOpenKeyExA(hkey, pszSubKey, 0, KEY_QUERY_VALUE, &hkSubKey);
  16. if (lr == ERROR_SUCCESS)
  17. {
  18. RegCloseKey(hkSubKey);
  19. }
  20. return lr == ERROR_SUCCESS;
  21. }
  22. STDAPI_(BOOL)
  23. SHRegSubKeyExistsW(
  24. IN HKEY hkey,
  25. IN LPCWSTR pwszSubKey)
  26. {
  27. BOOL br = FALSE;
  28. if (IsOS(OS_NT))
  29. {
  30. HKEY hkSubKey;
  31. if (ERROR_SUCCESS == RegOpenKeyExW(hkey, pwszSubKey, 0, KEY_QUERY_VALUE, &hkSubKey))
  32. {
  33. RegCloseKey(hkSubKey);
  34. br = TRUE;
  35. }
  36. }
  37. else
  38. {
  39. // Thunk...
  40. CHAR szSubKey[MAX_PATH];
  41. if (0 != WideCharToMultiByte(CP_ACP, 0, pwszSubKey, -1, szSubKey, sizeof(szSubKey), NULL, NULL))
  42. {
  43. br = SHRegSubKeyExistsA(hkey, szSubKey);
  44. }
  45. }
  46. return br;
  47. }
  48. /////////////////////////////////////////////////////////////////////////////
  49. //
  50. //
  51. // SHRegGetDWORD
  52. //
  53. //
  54. STDAPI
  55. SHRegGetDWORDA(
  56. IN HKEY hkey,
  57. IN PCSTR pszSubKey, // OPTIONAL: NULL or "" ok
  58. IN PCSTR pszValue, // OPTIONAL: NULL or "" ok
  59. OUT DWORD * pdwData)
  60. {
  61. LONG lr;
  62. if (hkey && pdwData)
  63. {
  64. DWORD cbData = sizeof(DWORD);
  65. lr = SHRegGetValueA(hkey, pszSubKey, pszValue, SRRF_RT_REG_DWORD, NULL, pdwData, &cbData);
  66. }
  67. else
  68. {
  69. RIPMSG(!!hkey, "SHRegGetDWORDA: caller passed null hkey!");
  70. RIPMSG(!!pdwData, "SHRegGetDWORDA: caller passed null pdwData!");
  71. lr = ERROR_INVALID_PARAMETER;
  72. }
  73. ASSERT(lr != ERROR_MORE_DATA); // Sanity check.
  74. return HRESULT_FROM_WIN32(lr);
  75. }
  76. STDAPI
  77. SHRegGetDWORDW(
  78. IN HKEY hkey,
  79. IN PCWSTR pwszSubKey, // OPTIONAL: NULL or "" ok
  80. IN PCWSTR pwszValue, // OPTIONAL: NULL or "" ok
  81. OUT DWORD * pdwData)
  82. {
  83. LONG lr;
  84. if (hkey && pdwData)
  85. {
  86. DWORD cbData = sizeof(DWORD);
  87. lr = SHRegGetValueW(hkey, pwszSubKey, pwszValue, SRRF_RT_REG_DWORD, NULL, pdwData, &cbData);
  88. }
  89. else
  90. {
  91. RIPMSG(!!hkey, "SHRegGetDWORDW: caller passed null hkey!");
  92. RIPMSG(!!pdwData, "SHRegGetDWORDW: caller passed null pdwData!");
  93. lr = ERROR_INVALID_PARAMETER;
  94. }
  95. ASSERT(lr != ERROR_MORE_DATA); // Sanity check.
  96. return HRESULT_FROM_WIN32(lr);
  97. }
  98. /////////////////////////////////////////////////////////////////////////////
  99. //
  100. //
  101. // SHRegGetString
  102. //
  103. //
  104. STDAPI
  105. SHRegGetStringA(
  106. IN HKEY hkey,
  107. IN PCSTR pszSubKey, // OPTIONAL: NULL or "" ok
  108. IN PCSTR pszValue, // OPTIONAL: NULL or "" ok
  109. OUT PSTR pszData,
  110. IN DWORD cchData)
  111. {
  112. LONG lr;
  113. if (hkey && pszData && cchData > 0)
  114. {
  115. DWORD cbData = cchData * sizeof(CHAR);
  116. lr = SHRegGetValueA(hkey, pszSubKey, pszValue, SRRF_RT_REG_SZ, NULL, pszData, &cbData);
  117. }
  118. else
  119. {
  120. RIPMSG(!!hkey, "SHRegGetStringA: caller passed null hkey!");
  121. RIPMSG(!!pszData, "SHRegGetStringA: caller passed null pszData!");
  122. RIPMSG(cchData > 0, "SHRegGetStringA: caller passed pszData of 0 size!");
  123. lr = ERROR_INVALID_PARAMETER;
  124. }
  125. if (lr != ERROR_SUCCESS && pszData && cchData > 0)
  126. {
  127. pszData[0] = '\0';
  128. }
  129. return HRESULT_FROM_WIN32(lr);
  130. }
  131. STDAPI
  132. SHRegGetStringW(
  133. IN HKEY hkey,
  134. IN PCWSTR pwszSubKey, // OPTIONAL: NULL or "" ok
  135. IN PCWSTR pwszValue, // OPTIONAL: NULL or "" ok
  136. OUT PWSTR pwszData,
  137. IN DWORD cchData)
  138. {
  139. LONG lr;
  140. if (hkey && pwszData && cchData > 0)
  141. {
  142. DWORD cbData = cchData * sizeof(WCHAR);
  143. lr = SHRegGetValueW(hkey, pwszSubKey, pwszValue, SRRF_RT_REG_SZ, NULL, pwszData, &cbData);
  144. }
  145. else
  146. {
  147. RIPMSG(!!hkey, "SHRegGetStringW: caller passed null hkey!");
  148. RIPMSG(!!pwszData, "SHRegGetStringW: caller passed null pwszData!");
  149. RIPMSG(cchData > 0, "SHRegGetStringW: caller passed pszData of 0 size!");
  150. lr = ERROR_INVALID_PARAMETER;
  151. }
  152. if (lr != ERROR_SUCCESS && pwszData && cchData > 0)
  153. {
  154. pwszData[0] = L'\0';
  155. }
  156. return HRESULT_FROM_WIN32(lr);
  157. }
  158. /////////////////////////////////////////////////////////////////////////////
  159. //
  160. //
  161. // SHRegAllocString (use CoTaskMemFree() to free returned string on S_OK)
  162. //
  163. //
  164. STDAPI
  165. SHRegAllocStringA(
  166. IN HKEY hkey,
  167. IN PCSTR pszSubKey, // OPTIONAL: NULL or "" ok
  168. IN PCSTR pszValue, // OPTIONAL: NULL or "" ok
  169. OUT PSTR * ppszData)
  170. {
  171. LONG lr;
  172. if (hkey && ppszData)
  173. {
  174. CHAR szData[128]; // perf: best guess for average case
  175. DWORD cbData = sizeof(szData);
  176. lr = SHRegGetValueA(hkey, pszSubKey, pszValue, SRRF_RT_REG_SZ, NULL, szData, &cbData);
  177. if (lr == ERROR_SUCCESS || lr == ERROR_MORE_DATA)
  178. {
  179. *ppszData = (PSTR)CoTaskMemAlloc(cbData);
  180. if (*ppszData)
  181. {
  182. if (lr == ERROR_SUCCESS)
  183. {
  184. memcpy(ppszData, szData, cbData);
  185. }
  186. else if (SHRegGetValueA(hkey, pszSubKey, pszValue, SRRF_RT_REG_SZ, NULL, szData, &cbData) != ERROR_SUCCESS)
  187. {
  188. lr = ERROR_CAN_NOT_COMPLETE;
  189. CoTaskMemFree(*ppszData);
  190. }
  191. }
  192. else
  193. {
  194. lr = ERROR_OUTOFMEMORY;
  195. }
  196. }
  197. }
  198. else
  199. {
  200. RIPMSG(!!hkey, "SHRegAllocStringA: caller passed null hkey!");
  201. RIPMSG(!!ppszData, "SHRegAllocStringA: caller passed null ppszData!");
  202. lr = ERROR_INVALID_PARAMETER;
  203. }
  204. if (lr != ERROR_SUCCESS && ppszData != NULL)
  205. {
  206. *ppszData = NULL;
  207. }
  208. return HRESULT_FROM_WIN32(lr);
  209. }
  210. STDAPI
  211. SHRegAllocStringW(
  212. IN HKEY hkey,
  213. IN PCWSTR pwszSubKey, // OPTIONAL: NULL or "" ok
  214. IN PCWSTR pwszValue, // OPTIONAL: NULL or "" ok
  215. OUT PWSTR * ppwszData)
  216. {
  217. LONG lr;
  218. if (hkey && ppwszData)
  219. {
  220. WCHAR wszData[128]; // perf: best guess for average case
  221. DWORD cbData = sizeof(wszData);
  222. lr = SHRegGetValueW(hkey, pwszSubKey, pwszValue, SRRF_RT_REG_SZ, NULL, wszData, &cbData);
  223. if (lr == ERROR_SUCCESS || lr == ERROR_MORE_DATA)
  224. {
  225. *ppwszData = (PWSTR)CoTaskMemAlloc(cbData);
  226. if (*ppwszData)
  227. {
  228. if (lr == ERROR_SUCCESS)
  229. {
  230. memcpy(ppwszData, wszData, cbData);
  231. }
  232. else if (SHRegGetValueW(hkey, pwszSubKey, pwszValue, SRRF_RT_REG_SZ, NULL, wszData, &cbData) != ERROR_SUCCESS)
  233. {
  234. lr = ERROR_CAN_NOT_COMPLETE;
  235. CoTaskMemFree(*ppwszData);
  236. }
  237. }
  238. else
  239. {
  240. lr = ERROR_OUTOFMEMORY;
  241. }
  242. }
  243. }
  244. else
  245. {
  246. RIPMSG(!!hkey, "SHRegAllocStringW: caller passed null hkey!");
  247. RIPMSG(!!ppwszData, "SHRegAllocStringW: caller passed null ppwszData!");
  248. lr = ERROR_INVALID_PARAMETER;
  249. }
  250. if (lr != ERROR_SUCCESS && ppwszData != NULL)
  251. {
  252. *ppwszData = NULL;
  253. }
  254. return HRESULT_FROM_WIN32(lr);
  255. }