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.

276 lines
7.4 KiB

  1. #include "precomp.hxx"
  2. /*
  3. This function opens the key identified by the KeyName, and is logically
  4. under the ParentKey in the registry.
  5. If you need to open HKCR, specify HKCR as ParentKey and KeyName to be NULL. If
  6. you need to open a Key under HKCR, specify the KeyName to be the name of the key
  7. under HKCR to be opened.
  8. */
  9. BasicRegistry::BasicRegistry(
  10. HKEY ParentKey,
  11. char * KeyName )
  12. {
  13. LONG error;
  14. // assume that this wont return an error.
  15. error = RegOpenKeyEx( ParentKey,
  16. KeyName,
  17. 0,
  18. KEY_ALL_ACCESS,
  19. &Key );
  20. if( error != ERROR_SUCCESS )
  21. {
  22. Key = 0;
  23. }
  24. else
  25. {
  26. strcpy(StoredKeyName, KeyName);
  27. }
  28. KeyIndex = 0;
  29. }
  30. /*
  31. This function enumerates the sub-key under this key. If you have to enumerate a key
  32. under HKCR, the class should be an HKCR class and the key will be be key to
  33. enumerate under HKCR
  34. */
  35. LONG
  36. BasicRegistry::NextKey(
  37. char * pNameOfKeyToEnumUnderThis,
  38. DWORD * pSizeOfKeyNameBuffer,
  39. BasicRegistry **ppNewChildKey,
  40. FILETIME ftLow,
  41. FILETIME ftHigh )
  42. {
  43. HKEY hKeyTemp;
  44. FILETIME ft;
  45. DWORD dwSaveMaxSize = *pSizeOfKeyNameBuffer;
  46. LONG error;
  47. do
  48. {
  49. error = RegEnumKeyEx(
  50. Key,
  51. KeyIndex,
  52. pNameOfKeyToEnumUnderThis,
  53. pSizeOfKeyNameBuffer,
  54. 0, // reserved - mbz
  55. 0, // class of key
  56. 0, // sizeof class buffer
  57. &ft );
  58. if( error )
  59. return ERROR_NO_MORE_ITEMS;
  60. if (CompareFileTime(&ftLow, &ftHigh) != 0) // if the high and low
  61. // times are not equal
  62. // then check the file
  63. // time.
  64. {
  65. if ((CompareFileTime(&ftLow, &ft) <= 0) &&
  66. (CompareFileTime(&ft, &ftHigh) <= 0))
  67. {
  68. break;
  69. }
  70. else
  71. {
  72. // time didn't match
  73. // get next key
  74. KeyIndex++;
  75. *pSizeOfKeyNameBuffer = dwSaveMaxSize;
  76. }
  77. }
  78. else
  79. {
  80. break;
  81. }
  82. } while ( TRUE );
  83. error = RegOpenKeyEx(
  84. Key,
  85. pNameOfKeyToEnumUnderThis,
  86. 0,
  87. KEY_ALL_ACCESS,
  88. &hKeyTemp );
  89. if( error == ERROR_SUCCESS )
  90. {
  91. *ppNewChildKey = new BasicRegistry( hKeyTemp );
  92. }
  93. else
  94. error = ERROR_NO_MORE_ITEMS;
  95. KeyIndex++;
  96. return error;
  97. }
  98. // Gets a key whose name starts with a number. useful for typelib version and
  99. // language id checking.
  100. LONG
  101. BasicRegistry::NextNumericKey(
  102. char * pBufferForKeyName,
  103. DWORD * pSizeOfKeyNameBuffer,
  104. BasicRegistry ** ppNewChildKey,
  105. FILETIME ftLow,
  106. FILETIME ftHigh)
  107. {
  108. HKEY hKeyTemp;
  109. FILETIME ft;
  110. DWORD dwSaveMaxSize = *pSizeOfKeyNameBuffer;
  111. LONG error;
  112. do
  113. {
  114. error = RegEnumKeyEx(
  115. Key,
  116. KeyIndex,
  117. pBufferForKeyName,
  118. pSizeOfKeyNameBuffer,
  119. 0, // reserved - mbz
  120. 0, // class of key
  121. 0, // sizeof class buffer
  122. &ft );
  123. if( error )
  124. return ERROR_NO_MORE_ITEMS;
  125. if( isdigit( pBufferForKeyName[0] ) )
  126. {
  127. if (CompareFileTime(&ftLow, &ftHigh) != 0) // if the high and low
  128. // times are not equal
  129. // then check the file
  130. // time.
  131. {
  132. if ((CompareFileTime(&ftLow, &ft) <= 0) &&
  133. (CompareFileTime(&ft, &ftHigh) <= 0))
  134. {
  135. break;
  136. }
  137. else
  138. {
  139. // time didn't match
  140. // get next key
  141. KeyIndex++;
  142. *pSizeOfKeyNameBuffer = dwSaveMaxSize;
  143. }
  144. }
  145. else
  146. {
  147. break;
  148. }
  149. }
  150. } while ( TRUE );
  151. error = RegOpenKeyEx(
  152. Key,
  153. pBufferForKeyName,
  154. 0,
  155. KEY_ALL_ACCESS,
  156. &hKeyTemp );
  157. if( error == ERROR_SUCCESS )
  158. {
  159. *ppNewChildKey = new BasicRegistry( hKeyTemp );
  160. }
  161. else
  162. error = ERROR_NO_MORE_ITEMS;
  163. KeyIndex++;
  164. return error;
  165. }
  166. /*
  167. Find a subkey within the key with the given subkey name. If the subkey is not found,
  168. no key is created (and none needs to be deleted)
  169. */
  170. LONG
  171. BasicRegistry::Find( char * SubKeyName,
  172. BasicRegistry ** ppSubKey )
  173. {
  174. LONG error;
  175. BasicRegistry * SubKey = new BasicRegistry( Key, SubKeyName );
  176. if( SubKey->GetKey() != 0 )
  177. {
  178. error = ERROR_SUCCESS;
  179. }
  180. else
  181. {
  182. delete SubKey;
  183. error = ERROR_NO_MORE_ITEMS;
  184. SubKey = 0;
  185. }
  186. if( ppSubKey)
  187. *ppSubKey = SubKey;
  188. return error;
  189. }
  190. /*
  191. Get the value on the key.
  192. */
  193. LONG
  194. BasicRegistry::QueryValue(
  195. char * ValueName,
  196. char * ValueResult,
  197. DWORD * SizeOfValueResult )
  198. {
  199. LONG error = RegQueryValue( Key,
  200. ValueName,
  201. ValueResult,
  202. (long *)SizeOfValueResult );
  203. return error == ERROR_SUCCESS ? error : ERROR_NO_MORE_ITEMS;
  204. }
  205. LONG
  206. BasicRegistry::QueryValueEx(
  207. char * ValueName,
  208. char * ValueResult,
  209. DWORD * SizeOfValueResult )
  210. {
  211. LONG error = RegQueryValueEx(Key,
  212. ValueName,
  213. 0,
  214. 0,
  215. (unsigned char *)ValueResult,
  216. (unsigned long *)SizeOfValueResult );
  217. return error == ERROR_SUCCESS ? error : ERROR_NO_MORE_ITEMS;
  218. }
  219. // figure out if this key is any of the classifications we are interested in.
  220. KEY_CLASS
  221. BasicRegistry::ClassifyKey(
  222. char * KeyName )
  223. {
  224. LONG error;
  225. char ValueResult[ MAX_KEY_NAME_LENGTH ];
  226. DWORD SizeOfValueResult = sizeof( ValueResult )/sizeof( char );
  227. error = QueryValue( KeyName, ValueResult, &SizeOfValueResult );
  228. if( error != ERROR_SUCCESS )
  229. return KEY_UNKNOWN;
  230. // See if it is a file extenstion.
  231. if( ValueResult[0] == '.' )
  232. return KEY_FILEEXT;
  233. // See if it is a progid. It is a progid, if it has a clsid key underneath
  234. // it.
  235. error = Find( "CLSID", 0 );
  236. if (error == ERROR_SUCCESS )
  237. return KEY_PROGID;
  238. return KEY_UNKNOWN;
  239. }