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.

250 lines
7.9 KiB

  1. /****************************************************************************
  2. *
  3. * File: reginfo.cpp
  4. * Project: DxDiag (DirectX Diagnostic Tool)
  5. * Author: Mike Anderson (manders@microsoft.com)
  6. * Purpose: Gather and hold registry information
  7. *
  8. * (C) Copyright 1998 Microsoft Corp. All rights reserved.
  9. *
  10. ****************************************************************************/
  11. #include <Windows.h>
  12. #include <tchar.h>
  13. #include "reginfo.h"
  14. static HRESULT AddError(RegError** ppRegErrorFirst, RegError* pRegErrorNew);
  15. static BOOL EqualMemory(BYTE* pb1, BYTE* pb2, DWORD numBytes);
  16. /****************************************************************************
  17. *
  18. * CheckRegDword
  19. *
  20. ****************************************************************************/
  21. HRESULT CheckRegDword(RegError** ppRegErrorFirst, HKEY hkeyRoot, TCHAR* pszKey,
  22. TCHAR* pszValue, DWORD dwExpected)
  23. {
  24. HKEY hkey = NULL;
  25. RegError regErrorNew;
  26. ZeroMemory(&regErrorNew, sizeof(RegError));
  27. regErrorNew.m_hkeyRoot = hkeyRoot;
  28. lstrcpy(regErrorNew.m_szKey, pszKey);
  29. lstrcpy(regErrorNew.m_szValue, pszValue);
  30. regErrorNew.m_dwTypeExpected = REG_DWORD;
  31. regErrorNew.m_dwExpected = dwExpected;
  32. if (ERROR_SUCCESS != RegOpenKeyEx(hkeyRoot, pszKey, 0, KEY_READ, &hkey))
  33. {
  34. regErrorNew.m_ret = RET_MISSINGKEY;
  35. }
  36. else
  37. {
  38. regErrorNew.m_dwExpectedSize = sizeof(DWORD);
  39. regErrorNew.m_dwActualSize = regErrorNew.m_dwExpectedSize; // RegQueryValueEx will change this
  40. if (ERROR_SUCCESS != RegQueryValueEx(hkey, pszValue, 0, &regErrorNew.m_dwTypeActual,
  41. (LPBYTE)&regErrorNew.m_dwActual, &regErrorNew.m_dwActualSize))
  42. {
  43. regErrorNew.m_ret = RET_MISSINGVALUE;
  44. }
  45. else if (regErrorNew.m_dwTypeActual != regErrorNew.m_dwTypeExpected)
  46. {
  47. regErrorNew.m_ret = RET_VALUEWRONGTYPE;
  48. }
  49. else if (regErrorNew.m_dwActual != dwExpected)
  50. {
  51. regErrorNew.m_ret = RET_VALUEWRONGDATA;
  52. }
  53. RegCloseKey(hkey);
  54. }
  55. if (regErrorNew.m_ret == RET_NOERROR)
  56. return S_OK;
  57. else
  58. return AddError(ppRegErrorFirst, &regErrorNew);
  59. }
  60. /****************************************************************************
  61. *
  62. * CheckRegString
  63. *
  64. ****************************************************************************/
  65. HRESULT CheckRegString(RegError** ppRegErrorFirst, HKEY hkeyRoot, TCHAR* pszKey,
  66. TCHAR* pszValue, TCHAR* pszExpected, CheckRegFlags crf,
  67. HRESULT* phrError )
  68. {
  69. HKEY hkey = NULL;
  70. RegError regErrorNew;
  71. ZeroMemory(&regErrorNew, sizeof(RegError));
  72. regErrorNew.m_hkeyRoot = hkeyRoot;
  73. lstrcpy(regErrorNew.m_szKey, pszKey);
  74. lstrcpy(regErrorNew.m_szValue, pszValue);
  75. regErrorNew.m_dwTypeExpected = REG_SZ;
  76. lstrcpy(regErrorNew.m_szExpected, pszExpected);
  77. if (ERROR_SUCCESS != RegOpenKeyEx(hkeyRoot, pszKey, 0, KEY_READ, &hkey))
  78. {
  79. regErrorNew.m_ret = RET_MISSINGKEY;
  80. }
  81. else
  82. {
  83. regErrorNew.m_dwExpectedSize = lstrlen(pszExpected) + 1;
  84. regErrorNew.m_dwActualSize = sizeof(regErrorNew.m_szActual); // RegQueryValueEx will change this
  85. if (ERROR_SUCCESS != RegQueryValueEx(hkey, pszValue, 0, &regErrorNew.m_dwTypeActual,
  86. (LPBYTE)&regErrorNew.m_szActual, &regErrorNew.m_dwActualSize))
  87. {
  88. regErrorNew.m_ret = RET_MISSINGVALUE;
  89. }
  90. else if (regErrorNew.m_dwTypeActual != regErrorNew.m_dwTypeExpected)
  91. {
  92. regErrorNew.m_ret = RET_VALUEWRONGTYPE;
  93. }
  94. else if (lstrcmp(regErrorNew.m_szExpected, TEXT("*")) != 0)
  95. {
  96. TCHAR* pszCompare = regErrorNew.m_szActual;
  97. if (crf & CRF_LEAF)
  98. {
  99. pszCompare = _tcsrchr(regErrorNew.m_szActual, TEXT('\\'));
  100. if (pszCompare == NULL)
  101. pszCompare = regErrorNew.m_szActual;
  102. else
  103. pszCompare++; // skip past backslash
  104. }
  105. if (lstrcmpi(regErrorNew.m_szExpected, pszCompare) != 0)
  106. {
  107. regErrorNew.m_ret = RET_VALUEWRONGDATA;
  108. }
  109. }
  110. RegCloseKey(hkey);
  111. }
  112. if( phrError )
  113. *phrError = regErrorNew.m_ret;
  114. if (regErrorNew.m_ret == RET_NOERROR)
  115. return S_OK;
  116. else
  117. return AddError(ppRegErrorFirst, &regErrorNew);
  118. }
  119. /****************************************************************************
  120. *
  121. * CheckRegBinary
  122. *
  123. ****************************************************************************/
  124. HRESULT CheckRegBinary(RegError** ppRegErrorFirst, HKEY hkeyRoot, TCHAR* pszKey,
  125. TCHAR* pszValue, BYTE* pbDataExpected, DWORD dwSizeExpected)
  126. {
  127. HKEY hkey = NULL;
  128. RegError regErrorNew;
  129. if (dwSizeExpected > sizeof(regErrorNew.m_bExpected))
  130. return E_INVALIDARG;
  131. ZeroMemory(&regErrorNew, sizeof(RegError));
  132. regErrorNew.m_hkeyRoot = hkeyRoot;
  133. lstrcpy(regErrorNew.m_szKey, pszKey);
  134. lstrcpy(regErrorNew.m_szValue, pszValue);
  135. regErrorNew.m_dwTypeExpected = REG_BINARY;
  136. CopyMemory(regErrorNew.m_bExpected, pbDataExpected, dwSizeExpected);
  137. if (ERROR_SUCCESS != RegOpenKeyEx(hkeyRoot, pszKey, 0, KEY_READ, &hkey))
  138. {
  139. regErrorNew.m_ret = RET_MISSINGKEY;
  140. }
  141. else
  142. {
  143. regErrorNew.m_dwExpectedSize = dwSizeExpected;
  144. regErrorNew.m_dwActualSize = sizeof(regErrorNew.m_bExpected); // RegQueryValueEx will change this
  145. if (ERROR_SUCCESS != RegQueryValueEx(hkey, pszValue, 0, &regErrorNew.m_dwTypeActual,
  146. (LPBYTE)&regErrorNew.m_bActual, &regErrorNew.m_dwActualSize))
  147. {
  148. regErrorNew.m_ret = RET_MISSINGVALUE;
  149. }
  150. else if (regErrorNew.m_dwTypeActual != regErrorNew.m_dwTypeExpected)
  151. {
  152. regErrorNew.m_ret = RET_VALUEWRONGTYPE;
  153. }
  154. else if (regErrorNew.m_dwActualSize != regErrorNew.m_dwExpectedSize)
  155. {
  156. regErrorNew.m_ret = RET_VALUEWRONGDATA;
  157. }
  158. else if (!EqualMemory(regErrorNew.m_bExpected, regErrorNew.m_bActual, regErrorNew.m_dwActualSize))
  159. {
  160. regErrorNew.m_ret = RET_VALUEWRONGDATA;
  161. }
  162. RegCloseKey(hkey);
  163. }
  164. if (regErrorNew.m_ret == RET_NOERROR)
  165. return S_OK;
  166. else
  167. return AddError(ppRegErrorFirst, &regErrorNew);
  168. }
  169. /****************************************************************************
  170. *
  171. * AddError - Allocate a RegError node, copy data from pRegErrorNew, and
  172. * insert the node at the beginning of the ppRegErrorFirst linked list.
  173. *
  174. ****************************************************************************/
  175. HRESULT AddError(RegError** ppRegErrorFirst, RegError* pRegErrorNew)
  176. {
  177. RegError* pRegErrorInsert;
  178. pRegErrorInsert = new RegError;
  179. if (pRegErrorInsert == NULL)
  180. return E_OUTOFMEMORY;
  181. *pRegErrorInsert = *pRegErrorNew;
  182. pRegErrorInsert->m_pRegErrorNext = *ppRegErrorFirst;
  183. *ppRegErrorFirst = pRegErrorInsert;
  184. return S_OK;
  185. }
  186. /****************************************************************************
  187. *
  188. * EqualMemory
  189. *
  190. ****************************************************************************/
  191. BOOL EqualMemory(BYTE* pb1, BYTE* pb2, DWORD numBytes)
  192. {
  193. while (numBytes > 0)
  194. {
  195. if (*pb1 != *pb2)
  196. return FALSE;
  197. pb1++;
  198. pb2++;
  199. numBytes--;
  200. }
  201. return TRUE;
  202. }
  203. /****************************************************************************
  204. *
  205. * DestroyReg
  206. *
  207. ****************************************************************************/
  208. VOID DestroyReg( RegError** ppRegErrorFirst )
  209. {
  210. if( ppRegErrorFirst && *ppRegErrorFirst )
  211. {
  212. RegError* pRegError;
  213. RegError* pRegErrorNext;
  214. for (pRegError = *ppRegErrorFirst; pRegError != NULL;
  215. pRegError = pRegErrorNext)
  216. {
  217. pRegErrorNext = pRegError->m_pRegErrorNext;
  218. delete pRegError;
  219. }
  220. *ppRegErrorFirst = NULL;
  221. }
  222. }