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.

184 lines
4.8 KiB

  1. #include <windows.h>
  2. #include "debug.h"
  3. #include "reg.h"
  4. extern "C" HANDLE hHeap;
  5. //------------------------------------------------------------------------------
  6. //
  7. //
  8. // Registry helpers
  9. //
  10. //
  11. //------------------------------------------------------------------------------
  12. LONG RegQueryBinaryValue(IN HKEY hkey, IN PCTSTR pValueName, OUT PBYTE *ppValue, OUT ULONG *pcbValue)
  13. {
  14. LONG result;
  15. DWORD typeValue;
  16. DWORD cbValue = 0;
  17. BOOL f;
  18. result = RegQueryValueEx(hkey, pValueName, 0, &typeValue, NULL, &cbValue);
  19. if (ERROR_SUCCESS == result)
  20. {
  21. if (REG_BINARY == typeValue)
  22. {
  23. PBYTE pValue;
  24. pValue = (PBYTE)HeapAlloc(hHeap, 0, cbValue);
  25. if (pValue)
  26. {
  27. result = RegQueryValueEx(hkey, pValueName, 0, &typeValue, (PBYTE)pValue, &cbValue);
  28. if (ERROR_SUCCESS == result)
  29. {
  30. if (REG_BINARY == typeValue)
  31. {
  32. *ppValue = pValue;
  33. *pcbValue = cbValue;
  34. } else {
  35. result = ERROR_FILE_NOT_FOUND;
  36. f = HeapFree(hHeap, 0, pValue);
  37. ASSERT(f);
  38. }
  39. } else {
  40. f = HeapFree(hHeap, 0, pValue);
  41. ASSERT(f);
  42. }
  43. } else {
  44. result = ERROR_OUTOFMEMORY;
  45. }
  46. } else {
  47. result = ERROR_FILE_NOT_FOUND;
  48. }
  49. }
  50. return result;
  51. }
  52. LONG RegQuerySzValue(HKEY hkey, PCTSTR pValueName, PTSTR *ppstrValue)
  53. {
  54. LONG result;
  55. DWORD typeValue;
  56. DWORD cbstrValue = 0;
  57. BOOL f;
  58. result = RegQueryValueEx(hkey, pValueName, 0, &typeValue, NULL, &cbstrValue);
  59. if (ERROR_SUCCESS == result)
  60. {
  61. if (REG_SZ == typeValue)
  62. {
  63. PTSTR pstrValue;
  64. pstrValue = (PTSTR)HeapAlloc(hHeap, 0, cbstrValue);
  65. if (pstrValue)
  66. {
  67. result = RegQueryValueEx(hkey, pValueName, 0, &typeValue, (PBYTE)pstrValue, &cbstrValue);
  68. if (ERROR_SUCCESS == result)
  69. {
  70. if (REG_SZ == typeValue)
  71. {
  72. *ppstrValue = pstrValue;
  73. } else {
  74. result = ERROR_FILE_NOT_FOUND;
  75. f = HeapFree(hHeap, 0, pstrValue);
  76. ASSERT(f);
  77. }
  78. } else {
  79. f = HeapFree(hHeap, 0, pstrValue);
  80. ASSERT(f);
  81. }
  82. } else {
  83. result = ERROR_OUTOFMEMORY;
  84. }
  85. } else {
  86. result = ERROR_FILE_NOT_FOUND;
  87. }
  88. }
  89. return result;
  90. }
  91. LONG RegQueryMultiSzValue(HKEY hkey, PCTSTR pValueName, PTSTR *ppstrValue)
  92. {
  93. LONG result;
  94. DWORD typeValue;
  95. DWORD cbstrValue = 0;
  96. BOOL f;
  97. result = RegQueryValueEx(hkey, pValueName, 0, &typeValue, NULL, &cbstrValue);
  98. if (ERROR_SUCCESS == result)
  99. {
  100. if (REG_MULTI_SZ == typeValue)
  101. {
  102. PTSTR pstrValue;
  103. pstrValue = (PTSTR)HeapAlloc(hHeap, 0, cbstrValue);
  104. if (pstrValue)
  105. {
  106. result = RegQueryValueEx(hkey, pValueName, 0, &typeValue, (PBYTE)pstrValue, &cbstrValue);
  107. if (ERROR_SUCCESS == result)
  108. {
  109. if (REG_MULTI_SZ == typeValue)
  110. {
  111. *ppstrValue = pstrValue;
  112. } else {
  113. result = ERROR_FILE_NOT_FOUND;
  114. f = HeapFree(hHeap, 0, pstrValue);
  115. ASSERT(f);
  116. }
  117. } else {
  118. f = HeapFree(hHeap, 0, pstrValue);
  119. ASSERT(f);
  120. }
  121. } else {
  122. result = ERROR_OUTOFMEMORY;
  123. }
  124. } else {
  125. result = ERROR_FILE_NOT_FOUND;
  126. }
  127. }
  128. return result;
  129. }
  130. LONG RegQueryDwordValue(HKEY hkey, PCTSTR pValueName, PDWORD pdwValue)
  131. {
  132. DWORD cbdwValue;
  133. LONG result;
  134. cbdwValue = sizeof(*pdwValue);
  135. result = RegQueryValueEx(hkey, pValueName, 0, NULL, (PBYTE)pdwValue, &cbdwValue);
  136. return result;
  137. }
  138. LONG RegSetSzValue(HKEY hkey, PCTSTR pValueName, PCTSTR pstrValue)
  139. {
  140. DWORD cbstrValue = (lstrlen(pstrValue) + 1) * sizeof(pstrValue[0]);
  141. return RegSetValueEx(hkey, pValueName, 0, REG_SZ, (PBYTE)pstrValue, cbstrValue);
  142. }
  143. LONG RegSetMultiSzValue(HKEY hkey, PCTSTR pValueName, PCTSTR pstrValue)
  144. {
  145. PCTSTR pstr;
  146. DWORD cch;
  147. DWORD cchValue;
  148. DWORD cbValue;
  149. pstr = pstrValue;
  150. cchValue = 0;
  151. do {
  152. cch = lstrlen(pstr);
  153. cchValue += cch+1;
  154. pstr += cch+1;
  155. } while (cch > 0);
  156. cbValue = cchValue * sizeof(TCHAR);
  157. return RegSetValueEx(hkey, pValueName, 0, REG_MULTI_SZ, (PBYTE)pstrValue, cbValue);
  158. }
  159. LONG RegSetBinaryValue(IN HKEY hkey, IN PCTSTR pValueName, IN PBYTE pValue, IN ULONG cbValue)
  160. {
  161. return RegSetValueEx(hkey, pValueName, 0, REG_BINARY, (PBYTE)pValue, cbValue);
  162. }
  163. LONG RegSetDwordValue(HKEY hkey, PCTSTR pValueName, DWORD dwValue)
  164. {
  165. return RegSetValueEx(hkey, pValueName, 0, REG_DWORD, (PBYTE)&dwValue, sizeof(dwValue));
  166. }