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.

340 lines
11 KiB

  1. #ifndef __MRU_H_INCLUDED
  2. #define __MRU_H_INCLUDED
  3. #include <windows.h>
  4. #include <shlobj.h>
  5. #include <shlobjp.h>
  6. #include "simstr.h"
  7. #include "simidlst.h"
  8. #include "destdata.h"
  9. #define CURRENT_REGISTRY_DATA_FORMAT_VERSION 2
  10. class CMruStringList : public CSimpleLinkedList<CSimpleString>
  11. {
  12. private:
  13. int m_nNumToWrite;
  14. enum
  15. {
  16. DefaultNumToWrite=20
  17. };
  18. private:
  19. CMruStringList( const CMruStringList & );
  20. CMruStringList &operator=( const CMruStringList & );
  21. public:
  22. CMruStringList( int nNumToWrite=DefaultNumToWrite )
  23. : m_nNumToWrite(nNumToWrite)
  24. {
  25. }
  26. bool Read( HKEY hRoot, LPCTSTR pszKey, LPCTSTR pszValueName )
  27. {
  28. CSimpleReg reg( hRoot, pszKey, false, KEY_READ );
  29. if (reg.OK())
  30. {
  31. if (REG_MULTI_SZ==reg.Type(pszValueName))
  32. {
  33. int nSize = reg.Size(pszValueName);
  34. if (nSize)
  35. {
  36. PBYTE pData = new BYTE[nSize];
  37. if (pData)
  38. {
  39. if (reg.QueryBin( pszValueName, pData, nSize ))
  40. {
  41. for (LPTSTR pszCurr=reinterpret_cast<LPTSTR>(pData);*pszCurr;pszCurr+=lstrlen(pszCurr)+1)
  42. {
  43. Append( pszCurr );
  44. }
  45. }
  46. delete[] pData;
  47. }
  48. }
  49. }
  50. }
  51. return(true);
  52. }
  53. bool Write( HKEY hRoot, LPCTSTR pszKey, LPCTSTR pszValueName )
  54. {
  55. CSimpleReg reg( hRoot, pszKey, true, KEY_WRITE );
  56. if (reg.OK())
  57. {
  58. int nLengthInChars = 0, nCount;
  59. Iterator i;
  60. for (i=Begin(),nCount=0;i != End() && nCount < m_nNumToWrite;++i,++nCount)
  61. nLengthInChars += (*i).Length() + 1;
  62. if (nLengthInChars)
  63. {
  64. ++nLengthInChars;
  65. LPTSTR pszMultiStr = new TCHAR[nLengthInChars];
  66. if (pszMultiStr)
  67. {
  68. LPTSTR pszCurr = pszMultiStr;
  69. for (i = Begin(), nCount=0;i != End() && nCount < m_nNumToWrite;++i,++nCount)
  70. {
  71. lstrcpy(pszCurr,(*i).String());
  72. pszCurr += (*i).Length() + 1;
  73. }
  74. *pszCurr = TEXT('\0');
  75. reg.SetBin( pszValueName, reinterpret_cast<PBYTE>(pszMultiStr), nLengthInChars*sizeof(TCHAR), REG_MULTI_SZ );
  76. delete[] pszMultiStr;
  77. }
  78. }
  79. }
  80. return(true);
  81. }
  82. void Add( CSimpleString str )
  83. {
  84. if (str.Length())
  85. {
  86. Remove(str);
  87. Prepend(str);
  88. }
  89. }
  90. void PopulateComboBox( HWND hWnd )
  91. {
  92. SendMessage( hWnd, CB_RESETCONTENT, 0, 0 );
  93. for (Iterator i = Begin();i != End();++i)
  94. {
  95. SendMessage( hWnd, CB_ADDSTRING, 0, (LPARAM)((*i).String()));
  96. }
  97. }
  98. };
  99. class CMruShellIdList : public CSimpleLinkedList<CSimpleIdList>
  100. {
  101. private:
  102. int m_nNumToWrite;
  103. enum
  104. {
  105. DefaultNumToWrite=20
  106. };
  107. struct REGISTRY_SIGNATURE
  108. {
  109. DWORD dwSize;
  110. DWORD dwVersion;
  111. DWORD dwCount;
  112. };
  113. private:
  114. CMruShellIdList( const CMruShellIdList & );
  115. CMruShellIdList &operator=( const CMruShellIdList & );
  116. public:
  117. CMruShellIdList( int nNumToWrite=DefaultNumToWrite )
  118. : m_nNumToWrite(nNumToWrite)
  119. {
  120. }
  121. bool Read( HKEY hRoot, LPCTSTR pszKey, LPCTSTR pszValueName )
  122. {
  123. CSimpleReg reg( hRoot, pszKey, false, KEY_READ );
  124. if (reg.OK())
  125. {
  126. if (REG_BINARY==reg.Type(pszValueName))
  127. {
  128. int nSize = reg.Size(pszValueName);
  129. if (nSize >= sizeof(REGISTRY_SIGNATURE))
  130. {
  131. PBYTE pData = new BYTE[nSize];
  132. if (pData)
  133. {
  134. if (reg.QueryBin( pszValueName, pData, nSize ))
  135. {
  136. REGISTRY_SIGNATURE rs;
  137. CopyMemory( &rs, pData, sizeof(REGISTRY_SIGNATURE) );
  138. if (rs.dwSize == sizeof(REGISTRY_SIGNATURE) && rs.dwVersion == CURRENT_REGISTRY_DATA_FORMAT_VERSION && rs.dwCount)
  139. {
  140. PBYTE pCurr = pData + sizeof(REGISTRY_SIGNATURE);
  141. for (int i=0;i<(int)rs.dwCount;i++)
  142. {
  143. DWORD dwItemSize;
  144. CopyMemory( &dwItemSize, pCurr, sizeof(DWORD) );
  145. if (dwItemSize)
  146. {
  147. Append( CSimpleIdList( pCurr+sizeof(DWORD), dwItemSize ) );
  148. }
  149. pCurr += dwItemSize + sizeof(DWORD);
  150. }
  151. }
  152. }
  153. delete[] pData;
  154. }
  155. }
  156. }
  157. }
  158. return(true);
  159. }
  160. bool Write( HKEY hRoot, LPCTSTR pszKey, LPCTSTR pszValueName )
  161. {
  162. CSimpleReg reg( hRoot, pszKey, true, KEY_WRITE );
  163. if (reg.OK())
  164. {
  165. int nLengthInBytes = sizeof(REGISTRY_SIGNATURE), nCount=0;
  166. Iterator ListIter=Begin();
  167. while (ListIter != End() && nCount < m_nNumToWrite)
  168. {
  169. nLengthInBytes += (*ListIter).Size() + sizeof(DWORD);
  170. ++ListIter;
  171. ++nCount;
  172. }
  173. PBYTE pItems = new BYTE[nLengthInBytes];
  174. if (pItems)
  175. {
  176. REGISTRY_SIGNATURE rs;
  177. rs.dwSize = sizeof(REGISTRY_SIGNATURE);
  178. rs.dwVersion = CURRENT_REGISTRY_DATA_FORMAT_VERSION;
  179. rs.dwCount = nCount;
  180. PBYTE pCurr = pItems;
  181. CopyMemory( pCurr, &rs, sizeof(REGISTRY_SIGNATURE) );
  182. pCurr += sizeof(REGISTRY_SIGNATURE);
  183. ListIter=Begin();
  184. while (ListIter != End() && nCount > 0)
  185. {
  186. DWORD dwItemSize = (*ListIter).Size();
  187. CopyMemory( pCurr, &dwItemSize, sizeof(DWORD) );
  188. pCurr += sizeof(DWORD);
  189. CopyMemory( pCurr, (*ListIter).IdList(), (*ListIter).Size() );
  190. pCurr += (*ListIter).Size();
  191. ++ListIter;
  192. --nCount;
  193. }
  194. reg.SetBin( pszValueName, pItems, nLengthInBytes, REG_BINARY );
  195. delete[] pItems;
  196. }
  197. }
  198. return(true);
  199. }
  200. Iterator Add( CSimpleIdList item )
  201. {
  202. if (item.IsValid())
  203. {
  204. Remove(item);
  205. return Prepend(item);
  206. }
  207. return End();
  208. }
  209. };
  210. class CMruDestinationData : public CSimpleLinkedList<CDestinationData>
  211. {
  212. private:
  213. int m_nNumToWrite;
  214. enum
  215. {
  216. DefaultNumToWrite=20
  217. };
  218. struct REGISTRY_SIGNATURE
  219. {
  220. DWORD dwSize;
  221. DWORD dwVersion;
  222. DWORD dwCount;
  223. };
  224. private:
  225. CMruDestinationData( const CMruDestinationData & );
  226. CMruDestinationData &operator=( const CMruDestinationData & );
  227. public:
  228. CMruDestinationData( int nNumToWrite=DefaultNumToWrite )
  229. : m_nNumToWrite(nNumToWrite)
  230. {
  231. }
  232. bool Read( HKEY hRoot, LPCTSTR pszKey, LPCTSTR pszValueName )
  233. {
  234. CSimpleReg reg( hRoot, pszKey, false, KEY_READ );
  235. if (reg.OK())
  236. {
  237. if (REG_BINARY==reg.Type(pszValueName))
  238. {
  239. int nSize = reg.Size(pszValueName);
  240. if (nSize >= sizeof(REGISTRY_SIGNATURE))
  241. {
  242. PBYTE pData = new BYTE[nSize];
  243. if (pData)
  244. {
  245. if (reg.QueryBin( pszValueName, pData, nSize ))
  246. {
  247. REGISTRY_SIGNATURE rs;
  248. CopyMemory( &rs, pData, sizeof(REGISTRY_SIGNATURE) );
  249. if (rs.dwSize == sizeof(REGISTRY_SIGNATURE) && rs.dwVersion == CURRENT_REGISTRY_DATA_FORMAT_VERSION && rs.dwCount)
  250. {
  251. PBYTE pCurr = pData + sizeof(REGISTRY_SIGNATURE);
  252. for (int i=0;i<(int)rs.dwCount;i++)
  253. {
  254. DWORD dwItemSize;
  255. CopyMemory( &dwItemSize, pCurr, sizeof(DWORD) );
  256. pCurr += sizeof(DWORD);
  257. if (dwItemSize)
  258. {
  259. CDestinationData DestinationData;
  260. DestinationData.SetRegistryData(pCurr,dwItemSize);
  261. Append( DestinationData );
  262. }
  263. pCurr += dwItemSize;
  264. }
  265. }
  266. }
  267. delete[] pData;
  268. }
  269. }
  270. }
  271. }
  272. return(true);
  273. }
  274. bool Write( HKEY hRoot, LPCTSTR pszKey, LPCTSTR pszValueName )
  275. {
  276. CSimpleReg reg( hRoot, pszKey, true, KEY_WRITE );
  277. if (reg.OK())
  278. {
  279. //
  280. // Find the size needed for the data
  281. //
  282. int nLengthInBytes = sizeof(REGISTRY_SIGNATURE), nCount=0;
  283. Iterator ListIter=Begin();
  284. while (ListIter != End() && nCount < m_nNumToWrite)
  285. {
  286. nLengthInBytes += (*ListIter).RegistryDataSize() + sizeof(DWORD);
  287. ++nCount;
  288. ++ListIter;
  289. }
  290. PBYTE pItems = new BYTE[nLengthInBytes];
  291. if (pItems)
  292. {
  293. REGISTRY_SIGNATURE rs;
  294. rs.dwSize = sizeof(REGISTRY_SIGNATURE);
  295. rs.dwVersion = CURRENT_REGISTRY_DATA_FORMAT_VERSION;
  296. rs.dwCount = nCount;
  297. PBYTE pCurr = pItems;
  298. CopyMemory( pCurr, &rs, sizeof(REGISTRY_SIGNATURE) );
  299. pCurr += sizeof(REGISTRY_SIGNATURE);
  300. int nLengthRemaining = nLengthInBytes - sizeof(REGISTRY_SIGNATURE);
  301. ListIter=Begin();
  302. while (ListIter != End() && nCount > 0)
  303. {
  304. DWORD dwSize = (*ListIter).RegistryDataSize();
  305. CopyMemory( pCurr, &dwSize, sizeof(DWORD) );
  306. pCurr += sizeof(DWORD);
  307. (*ListIter).GetRegistryData( pCurr, nLengthRemaining );
  308. pCurr += (*ListIter).RegistryDataSize();
  309. --nCount;
  310. ++ListIter;
  311. }
  312. reg.SetBin( pszValueName, pItems, nLengthInBytes, REG_BINARY );
  313. delete[] pItems;
  314. }
  315. }
  316. return(true);
  317. }
  318. Iterator Add( CDestinationData item )
  319. {
  320. if (item.IsValid())
  321. {
  322. Remove(item);
  323. return Prepend(item);
  324. }
  325. return End();
  326. }
  327. };
  328. #endif //__MRU_H_INCLUDED