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.

551 lines
14 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // File: dap.cpp
  4. //
  5. // Contents: CDisplayAttributeProvider
  6. //
  7. //----------------------------------------------------------------------------
  8. #include "private.h"
  9. #include "dap.h"
  10. #include "regsvr.h" // for CLSID_STRLEN
  11. void DllAddRef(void);
  12. void DllRelease(void);
  13. const TCHAR c_szKey[] = TEXT("SOFTWARE\\Microsoft\\");
  14. WCHAR CDisplayAttributeProvider::szProviderName[80] = L"DisplayAttribute";
  15. //+---------------------------------------------------------------------------
  16. //
  17. // CDisplayAttributeProvider
  18. //
  19. //----------------------------------------------------------------------------
  20. CDisplayAttributeProvider::CDisplayAttributeProvider()
  21. {
  22. _pList = NULL;
  23. }
  24. CDisplayAttributeProvider::~CDisplayAttributeProvider()
  25. {
  26. CDisplayAttributeInfo *pInfo = _pList;
  27. while (pInfo)
  28. {
  29. CDisplayAttributeInfo *pNext = pInfo->_pNext;
  30. pInfo->Release();
  31. pInfo = pNext;
  32. }
  33. }
  34. //+---------------------------------------------------------------------------
  35. //
  36. // EnumDisplayAttributeInfo
  37. //
  38. //----------------------------------------------------------------------------
  39. STDMETHODIMP CDisplayAttributeProvider::EnumDisplayAttributeInfo(IEnumTfDisplayAttributeInfo **ppEnum)
  40. {
  41. HRESULT hr = S_OK;
  42. if (!(*ppEnum = new CEnumDisplayAttributeInfo(this)))
  43. {
  44. hr = E_OUTOFMEMORY;
  45. }
  46. return hr;
  47. }
  48. //----------------------------------------------------------------------------
  49. //
  50. // GetDisplayAttributeInfo
  51. //
  52. //----------------------------------------------------------------------------
  53. STDMETHODIMP CDisplayAttributeProvider::GetDisplayAttributeInfo(REFGUID guid, ITfDisplayAttributeInfo **ppInfo)
  54. {
  55. HRESULT hr = E_FAIL;
  56. CDisplayAttributeInfo *pInfo = _pList;
  57. while (pInfo)
  58. {
  59. if (IsEqualGUID(pInfo->_guid, guid))
  60. {
  61. if (SUCCEEDED(hr = pInfo->QueryInterface(IID_ITfDisplayAttributeInfo, (void **)ppInfo)))
  62. {
  63. return hr;
  64. }
  65. }
  66. pInfo = pInfo->_pNext;
  67. }
  68. return hr;
  69. }
  70. //----------------------------------------------------------------------------
  71. //
  72. // Add
  73. //
  74. //----------------------------------------------------------------------------
  75. void CDisplayAttributeProvider::Add(GUID guid, WCHAR *pszDesc, TF_DISPLAYATTRIBUTE *pda)
  76. {
  77. CDisplayAttributeInfo *pInfo;
  78. pInfo = new CDisplayAttributeInfo(guid, pszDesc, pda);
  79. if (pInfo)
  80. {
  81. pInfo->_pNext = _pList;
  82. _pList = pInfo;
  83. }
  84. }
  85. //+---------------------------------------------------------------------------
  86. //
  87. // CDisplayAttributeInfo
  88. //
  89. //----------------------------------------------------------------------------
  90. CDisplayAttributeInfo::CDisplayAttributeInfo(GUID guid, WCHAR *pszDesc, TF_DISPLAYATTRIBUTE *pda)
  91. {
  92. DllAddRef();
  93. _guid = guid;
  94. StringCchCopyW(_szDesc, ARRAYSIZE(_szDesc), pszDesc);
  95. if (FAILED(_OpenAttribute(c_szKey,
  96. CDisplayAttributeProvider::szProviderName,
  97. &guid, &_da)))
  98. _da = *pda;
  99. _daDefault = *pda;
  100. _pNext = NULL;
  101. _cRef = 1;
  102. }
  103. CDisplayAttributeInfo::~CDisplayAttributeInfo()
  104. {
  105. DllRelease();
  106. }
  107. //+---------------------------------------------------------------------------
  108. //
  109. // IUnknown
  110. //
  111. //----------------------------------------------------------------------------
  112. STDAPI CDisplayAttributeInfo::QueryInterface(REFIID riid, void **ppvObj)
  113. {
  114. if (ppvObj == NULL)
  115. return E_POINTER;
  116. *ppvObj = NULL;
  117. if (IsEqualIID(riid, IID_IUnknown) ||
  118. IsEqualIID(riid, IID_ITfDisplayAttributeInfo))
  119. {
  120. *ppvObj = SAFECAST(this, ITfDisplayAttributeInfo *);
  121. }
  122. if (*ppvObj == NULL)
  123. {
  124. return E_NOINTERFACE;
  125. }
  126. AddRef();
  127. return S_OK;
  128. }
  129. STDAPI_(ULONG) CDisplayAttributeInfo::AddRef()
  130. {
  131. _cRef++;
  132. return _cRef;
  133. }
  134. STDAPI_(ULONG) CDisplayAttributeInfo::Release()
  135. {
  136. _cRef--;
  137. if (0 < _cRef)
  138. return _cRef;
  139. delete this;
  140. return 0;
  141. }
  142. //+---------------------------------------------------------------------------
  143. //
  144. // GetGUID
  145. //
  146. //----------------------------------------------------------------------------
  147. HRESULT CDisplayAttributeInfo::GetGUID(GUID *pguid)
  148. {
  149. *pguid = _guid;
  150. return S_OK;
  151. }
  152. //+---------------------------------------------------------------------------
  153. //
  154. // GetDescription
  155. //
  156. //----------------------------------------------------------------------------
  157. HRESULT CDisplayAttributeInfo::GetDescription(BSTR *pbstr)
  158. {
  159. *pbstr = SysAllocString(_szDesc);
  160. if (*pbstr)
  161. return S_OK;
  162. else
  163. return E_OUTOFMEMORY;
  164. }
  165. //+---------------------------------------------------------------------------
  166. //
  167. // GetAttributeInfo
  168. //
  169. //----------------------------------------------------------------------------
  170. HRESULT CDisplayAttributeInfo::GetAttributeInfo(TF_DISPLAYATTRIBUTE *pda)
  171. {
  172. *pda = _da;
  173. return S_OK;
  174. }
  175. //+---------------------------------------------------------------------------
  176. //
  177. // SetAttributeInfo
  178. //
  179. //----------------------------------------------------------------------------
  180. HRESULT CDisplayAttributeInfo::SetAttributeInfo(const TF_DISPLAYATTRIBUTE *pda)
  181. {
  182. _da = *pda;
  183. _SaveAttribute(c_szKey, CDisplayAttributeProvider::szProviderName,
  184. &_guid, &_da);
  185. return S_OK;
  186. }
  187. //+---------------------------------------------------------------------------
  188. //
  189. // Reset()
  190. //
  191. //----------------------------------------------------------------------------
  192. HRESULT CDisplayAttributeInfo::Reset()
  193. {
  194. _da = _daDefault;
  195. _DeleteAttribute(c_szKey, CDisplayAttributeProvider::szProviderName, &_guid);
  196. return S_OK;
  197. }
  198. //----------------------------------------------------------------------------
  199. //
  200. // SaveAttribute
  201. //
  202. //----------------------------------------------------------------------------
  203. HRESULT CDisplayAttributeInfo::_SaveAttribute(const TCHAR *pszKey, WCHAR *pszDesc, const GUID *pguid, TF_DISPLAYATTRIBUTE *pda)
  204. {
  205. DWORD dw;
  206. HKEY hKeyDAM;
  207. HKEY hKeyItem;
  208. TCHAR achGuid[CLSID_STRLEN+1];
  209. if (RegCreateKeyEx(HKEY_CURRENT_USER, pszKey, 0, NULL,
  210. REG_OPTION_NON_VOLATILE,
  211. KEY_ALL_ACCESS,
  212. NULL,
  213. &hKeyDAM,
  214. &dw) != ERROR_SUCCESS)
  215. {
  216. return E_FAIL;
  217. }
  218. CLSIDToStringA(*pguid, achGuid);
  219. int cchDescA = wcslen(pszDesc) * sizeof(WCHAR) + 1;
  220. char *pszDescA = new char[cchDescA];
  221. if (pszDescA)
  222. {
  223. cchDescA = WideCharToMultiByte(CP_ACP, 0,
  224. pszDesc, wcslen(pszDesc),
  225. pszDescA, cchDescA,
  226. NULL, NULL);
  227. *(pszDescA + cchDescA) = L'\0';
  228. if (RegCreateKeyEx(hKeyDAM, pszDescA, 0, NULL,
  229. REG_OPTION_NON_VOLATILE,
  230. KEY_ALL_ACCESS,
  231. NULL,
  232. &hKeyItem,
  233. &dw) == ERROR_SUCCESS)
  234. {
  235. RegSetValueEx(hKeyItem, achGuid, 0, REG_BINARY,
  236. (CONST BYTE *)pda, sizeof(TF_DISPLAYATTRIBUTE));
  237. RegCloseKey(hKeyItem);
  238. }
  239. delete pszDescA;
  240. }
  241. RegCloseKey(hKeyDAM);
  242. return S_OK;
  243. }
  244. //----------------------------------------------------------------------------
  245. //
  246. // OpenAttribute
  247. //
  248. //----------------------------------------------------------------------------
  249. HRESULT CDisplayAttributeInfo::_OpenAttribute(const TCHAR *pszKey, WCHAR *pszDesc, const GUID *pguid, TF_DISPLAYATTRIBUTE *pda)
  250. {
  251. DWORD dw;
  252. HKEY hKeyDAM;
  253. HKEY hKeyItem;
  254. TCHAR achGuid[CLSID_STRLEN+1];
  255. LONG lret = ERROR_SUCCESS;
  256. if (RegCreateKeyEx(HKEY_CURRENT_USER, pszKey, 0, NULL,
  257. REG_OPTION_NON_VOLATILE,
  258. KEY_ALL_ACCESS,
  259. NULL,
  260. &hKeyDAM,
  261. &dw) != ERROR_SUCCESS)
  262. {
  263. return E_FAIL;
  264. }
  265. CLSIDToStringA(*pguid, achGuid);
  266. int cchDescA = wcslen(pszDesc) * sizeof(WCHAR) + 1;
  267. char *pszDescA = new char[cchDescA];
  268. if (pszDescA)
  269. {
  270. cchDescA = WideCharToMultiByte(CP_ACP, 0,
  271. pszDesc, wcslen(pszDesc),
  272. pszDescA, cchDescA,
  273. NULL, NULL);
  274. *(pszDescA + cchDescA) = L'\0';
  275. if (RegCreateKeyEx(hKeyDAM, pszDescA, 0, NULL,
  276. REG_OPTION_NON_VOLATILE,
  277. KEY_ALL_ACCESS,
  278. NULL,
  279. &hKeyItem,
  280. &dw) == ERROR_SUCCESS)
  281. {
  282. DWORD dwType = REG_BINARY;
  283. DWORD dwSize = sizeof(TF_DISPLAYATTRIBUTE);
  284. lret = RegQueryValueEx(hKeyItem, achGuid, 0, &dwType,
  285. (BYTE *)pda, &dwSize);
  286. RegCloseKey(hKeyItem);
  287. }
  288. delete pszDescA;
  289. }
  290. RegCloseKey(hKeyDAM);
  291. return (lret == ERROR_SUCCESS) ? S_OK : E_FAIL;
  292. }
  293. //----------------------------------------------------------------------------
  294. //
  295. // DeleteAttribute
  296. //
  297. //----------------------------------------------------------------------------
  298. HRESULT CDisplayAttributeInfo::_DeleteAttribute(const TCHAR *pszKey, WCHAR *pszDesc, const GUID *pguid)
  299. {
  300. DWORD dw;
  301. HKEY hKeyDAM;
  302. HKEY hKeyItem;
  303. TCHAR achGuid[CLSID_STRLEN+1];
  304. if (RegCreateKeyEx(HKEY_CURRENT_USER, pszKey, 0, NULL,
  305. REG_OPTION_NON_VOLATILE,
  306. KEY_ALL_ACCESS,
  307. NULL,
  308. &hKeyDAM,
  309. &dw) != ERROR_SUCCESS)
  310. {
  311. return E_FAIL;
  312. }
  313. CLSIDToStringA(*pguid, achGuid);
  314. int cchDescA = wcslen(pszDesc) * sizeof(WCHAR) + 1;
  315. char *pszDescA = new char[cchDescA];
  316. if (pszDescA)
  317. {
  318. cchDescA = WideCharToMultiByte(CP_ACP, 0,
  319. pszDesc, wcslen(pszDesc),
  320. pszDescA, cchDescA,
  321. NULL, NULL);
  322. *(pszDescA + cchDescA) = L'\0';
  323. if (RegCreateKeyEx(hKeyDAM, pszDescA, 0, NULL,
  324. REG_OPTION_NON_VOLATILE,
  325. KEY_ALL_ACCESS,
  326. NULL,
  327. &hKeyItem,
  328. &dw) == ERROR_SUCCESS)
  329. {
  330. RegDeleteValue(hKeyItem, achGuid);
  331. RegCloseKey(hKeyItem);
  332. }
  333. delete pszDescA;
  334. }
  335. RegCloseKey(hKeyDAM);
  336. return S_OK;
  337. }
  338. //+---------------------------------------------------------------------------
  339. //
  340. // CEnumDisplayAttributeInfo
  341. //
  342. //----------------------------------------------------------------------------
  343. CEnumDisplayAttributeInfo::CEnumDisplayAttributeInfo(CDisplayAttributeProvider *pProvider)
  344. {
  345. DllAddRef();
  346. _pProvider = pProvider;
  347. _pProvider->AddRef();
  348. _pCur = _pProvider->_pList;
  349. _cRef = 1;
  350. }
  351. CEnumDisplayAttributeInfo::~CEnumDisplayAttributeInfo()
  352. {
  353. _pProvider->Release();
  354. DllRelease();
  355. }
  356. //+---------------------------------------------------------------------------
  357. //
  358. // IUnknown
  359. //
  360. //----------------------------------------------------------------------------
  361. STDAPI CEnumDisplayAttributeInfo::QueryInterface(REFIID riid, void **ppvObj)
  362. {
  363. if (ppvObj == NULL)
  364. return E_POINTER;
  365. *ppvObj = NULL;
  366. if (IsEqualIID(riid, IID_IUnknown) ||
  367. IsEqualIID(riid, IID_IEnumTfDisplayAttributeInfo))
  368. {
  369. *ppvObj = SAFECAST(this, IEnumTfDisplayAttributeInfo *);
  370. }
  371. if (*ppvObj == NULL)
  372. {
  373. return E_NOINTERFACE;
  374. }
  375. AddRef();
  376. return S_OK;
  377. }
  378. STDAPI_(ULONG) CEnumDisplayAttributeInfo::AddRef()
  379. {
  380. _cRef++;
  381. return _cRef;
  382. }
  383. STDAPI_(ULONG) CEnumDisplayAttributeInfo::Release()
  384. {
  385. _cRef--;
  386. if (0 < _cRef)
  387. return _cRef;
  388. delete this;
  389. return 0;
  390. }
  391. //+---------------------------------------------------------------------------
  392. //
  393. // Clone
  394. //
  395. //----------------------------------------------------------------------------
  396. HRESULT CEnumDisplayAttributeInfo::Clone(IEnumTfDisplayAttributeInfo **ppEnum)
  397. {
  398. return E_NOTIMPL;
  399. }
  400. //+---------------------------------------------------------------------------
  401. //
  402. // Next
  403. //
  404. //----------------------------------------------------------------------------
  405. HRESULT CEnumDisplayAttributeInfo::Next(ULONG ulCount, ITfDisplayAttributeInfo **ppInfo, ULONG *pcFetched)
  406. {
  407. ULONG cFetched = 0;
  408. while (cFetched < ulCount)
  409. {
  410. if (!_pCur)
  411. break;
  412. if (FAILED(_pCur->QueryInterface(IID_ITfDisplayAttributeInfo, (void **)ppInfo)))
  413. break;
  414. _pCur= _pCur->_pNext;
  415. ppInfo++;
  416. cFetched++;
  417. }
  418. if (pcFetched)
  419. *pcFetched = cFetched;
  420. return (cFetched == ulCount) ? S_OK : S_FALSE;
  421. }
  422. //+---------------------------------------------------------------------------
  423. //
  424. // Reset
  425. //
  426. //----------------------------------------------------------------------------
  427. HRESULT CEnumDisplayAttributeInfo::Reset()
  428. {
  429. _pCur = _pProvider->_pList;
  430. return S_OK;
  431. }
  432. //+---------------------------------------------------------------------------
  433. //
  434. // Skip
  435. //
  436. //----------------------------------------------------------------------------
  437. HRESULT CEnumDisplayAttributeInfo::Skip(ULONG ulCount)
  438. {
  439. while (ulCount)
  440. {
  441. if (!_pCur)
  442. break;
  443. _pCur = _pCur->_pNext;
  444. ulCount--;
  445. }
  446. return ulCount ? S_FALSE : S_OK;
  447. }