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.

215 lines
4.6 KiB

  1. #include "pch.h"
  2. #pragma hdrstop
  3. #include "registry.h"
  4. LONG
  5. RegQueryDword (
  6. IN HKEY hkey,
  7. IN LPCTSTR pszValueName,
  8. OUT LPDWORD pdwValue
  9. )
  10. {
  11. LONG lr;
  12. DWORD dwType;
  13. DWORD dwSize;
  14. ASSERT (hkey);
  15. ASSERT (pszValueName);
  16. ASSERT (pdwValue);
  17. dwSize = sizeof(DWORD);
  18. lr = RegQueryValueEx (
  19. hkey,
  20. pszValueName,
  21. NULL,
  22. &dwType,
  23. (LPBYTE)pdwValue,
  24. &dwSize);
  25. if (!lr && (REG_DWORD != dwType))
  26. {
  27. *pdwValue = 0;
  28. lr = ERROR_INVALID_DATATYPE;
  29. }
  30. return lr;
  31. }
  32. LONG
  33. RegQueryValueWithAlloc (
  34. IN HKEY hkey,
  35. IN LPCTSTR pszValueName,
  36. IN DWORD dwTypeMustBe,
  37. OUT LPBYTE* ppbData,
  38. OUT LPDWORD pdwSize
  39. )
  40. {
  41. LONG lr;
  42. DWORD dwType;
  43. DWORD dwSize;
  44. ASSERT (hkey);
  45. ASSERT (pszValueName);
  46. ASSERT (ppbData);
  47. ASSERT (pdwSize);
  48. // Initialize the output parameters.
  49. //
  50. *ppbData = NULL;
  51. *pdwSize = 0;
  52. // Get the size of the buffer required.
  53. //
  54. dwSize = 0;
  55. lr = RegQueryValueEx (
  56. hkey,
  57. pszValueName,
  58. NULL,
  59. &dwType,
  60. NULL,
  61. &dwSize);
  62. if (!lr && (dwType == dwTypeMustBe) && dwSize)
  63. {
  64. LPBYTE pbData;
  65. // Allocate the buffer.
  66. //
  67. lr = ERROR_OUTOFMEMORY;
  68. pbData = (LPBYTE)MemAlloc (0, dwSize);
  69. if (pbData)
  70. {
  71. // Get the data.
  72. //
  73. lr = RegQueryValueEx (
  74. hkey,
  75. pszValueName,
  76. NULL,
  77. &dwType,
  78. pbData,
  79. &dwSize);
  80. if (!lr)
  81. {
  82. *ppbData = pbData;
  83. *pdwSize = dwSize;
  84. }
  85. else
  86. {
  87. MemFree (pbData);
  88. }
  89. }
  90. }
  91. else if (!lr)
  92. {
  93. lr = ERROR_INVALID_DATA;
  94. }
  95. return lr;
  96. }
  97. LONG
  98. RegQueryString (
  99. IN HKEY hkey,
  100. IN LPCTSTR pszValueName,
  101. IN DWORD dwTypeMustBe,
  102. OUT PTSTR* ppszData
  103. )
  104. {
  105. LONG lr;
  106. DWORD dwSize;
  107. ASSERT (hkey);
  108. ASSERT (pszValueName);
  109. lr = RegQueryValueWithAlloc (
  110. hkey,
  111. pszValueName,
  112. dwTypeMustBe,
  113. (LPBYTE*)ppszData,
  114. &dwSize);
  115. return lr;
  116. }
  117. LONG
  118. RegQueryStringA (
  119. IN HKEY hkey,
  120. IN LPCTSTR pszValueName,
  121. IN DWORD dwTypeMustBe,
  122. OUT PSTR* ppszData
  123. )
  124. {
  125. LONG lr;
  126. PTSTR pszUnicode;
  127. ASSERT (hkey);
  128. ASSERT (pszValueName);
  129. ASSERT (ppszData);
  130. // Initialize the output parameter.
  131. //
  132. *ppszData = NULL;
  133. lr = RegQueryString (
  134. hkey,
  135. pszValueName,
  136. dwTypeMustBe,
  137. &pszUnicode);
  138. if (!lr)
  139. {
  140. INT cb;
  141. INT cchUnicode = lstrlen (pszUnicode) + 1;
  142. // Compute the number of bytes required to hold the ANSI string.
  143. //
  144. cb = WideCharToMultiByte (
  145. CP_ACP, // CodePage
  146. 0, // dwFlags
  147. pszUnicode,
  148. cchUnicode,
  149. NULL, // no buffer to receive translated string
  150. 0, // return the number of bytes required
  151. NULL, // lpDefaultChar
  152. NULL); // lpUsedDefaultChar
  153. if (cb)
  154. {
  155. PSTR pszAnsi;
  156. lr = ERROR_OUTOFMEMORY;
  157. pszAnsi = MemAlloc (0, cb);
  158. if (pszAnsi)
  159. {
  160. lr = NOERROR;
  161. // Now translate the UNICODE string to ANSI.
  162. //
  163. cb = WideCharToMultiByte (
  164. CP_ACP, // CodePage
  165. 0, // dwFlags
  166. pszUnicode,
  167. cchUnicode,
  168. pszAnsi, // buffer to receive translated string
  169. cb, // return the number of bytes required
  170. NULL, // lpDefaultChar
  171. NULL); // lpUsedDefaultChar
  172. if (cb)
  173. {
  174. *ppszData = pszAnsi;
  175. }
  176. else
  177. {
  178. MemFree (pszAnsi);
  179. lr = GetLastError ();
  180. }
  181. }
  182. }
  183. MemFree (pszUnicode);
  184. }
  185. return lr;
  186. }