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.

607 lines
14 KiB

  1. //==========================================================================
  2. //
  3. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. // PURPOSE.
  7. //
  8. // Copyright 1998 - 1999 Microsoft Corporation. All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------
  11. #include "precomp.h"
  12. #define HANDLERITEMSKEY TEXT("\\HandlerItems")
  13. #define HANDLERVALUE_DIR1 TEXT("Dir1")
  14. #define HANDLERVALUE_DIR2 TEXT("Dir2")
  15. #define HANDLERVALUE_DISPLAYNAME TEXT("DisplayName")
  16. #define HANDLERVALUE_FILETIME TEXT("TimeStamp")
  17. #define HANDLERVALUE_RECURSIVE TEXT("Recursive")
  18. //+---------------------------------------------------------------------------
  19. //
  20. // Function: SetRegKeyValue, private
  21. //
  22. // Synopsis: Internal utility function to set a Key, Subkey, and value
  23. // in the system Registry under HKEY_CLASSES_ROOT.
  24. //
  25. // Arguments:
  26. //
  27. // Returns:
  28. //
  29. // Modifies:
  30. //
  31. //----------------------------------------------------------------------------
  32. BOOL SetRegKeyValue(
  33. HKEY hKeyTop,
  34. LPTSTR pszKey,
  35. LPTSTR pszSubkey,
  36. LPTSTR pszValue)
  37. {
  38. BOOL bOk = FALSE;
  39. LONG ec;
  40. HKEY hKey;
  41. TCHAR szKey[MAX_STRING_LENGTH];
  42. lstrcpy(szKey, pszKey);
  43. if (NULL != pszSubkey)
  44. {
  45. lstrcat(szKey, TEXT("\\"));
  46. lstrcat(szKey, pszSubkey);
  47. }
  48. ec = RegCreateKeyEx(
  49. hKeyTop,
  50. szKey,
  51. 0,
  52. NULL,
  53. REG_OPTION_NON_VOLATILE,
  54. KEY_READ | KEY_WRITE,
  55. NULL,
  56. &hKey,
  57. NULL);
  58. if (NULL != pszValue && ERROR_SUCCESS == ec)
  59. {
  60. ec = RegSetValueEx(
  61. hKey,
  62. NULL,
  63. 0,
  64. REG_SZ,
  65. (BYTE *)pszValue,
  66. (lstrlen(pszValue)+1)*sizeof(TCHAR));
  67. if (ERROR_SUCCESS == ec)
  68. bOk = TRUE;
  69. RegCloseKey(hKey);
  70. }
  71. return bOk;
  72. }
  73. //+---------------------------------------------------------------------------
  74. //
  75. // Function: GetRegKeyValue, private
  76. //
  77. // Synopsis: Internal utility function to get a Key value
  78. // in the system Registry.
  79. //
  80. // Arguments:
  81. //
  82. // Returns:
  83. //
  84. // Modifies:
  85. //
  86. //----------------------------------------------------------------------------
  87. LRESULT GetRegKeyValue(HKEY hkeyParent, LPCTSTR pcszSubKey,
  88. LPCTSTR pcszValue, PDWORD pdwValueType,
  89. PBYTE pbyteBuf, PDWORD pdwcbBufLen)
  90. {
  91. LONG lResult;
  92. HKEY hkeySubKey;
  93. lResult = RegOpenKeyEx(hkeyParent, pcszSubKey, 0, KEY_QUERY_VALUE,
  94. &hkeySubKey);
  95. if (lResult == ERROR_SUCCESS)
  96. {
  97. LONG lResultClose;
  98. lResult = RegQueryValueEx(hkeySubKey, pcszValue, NULL, pdwValueType,
  99. pbyteBuf, pdwcbBufLen);
  100. lResultClose = RegCloseKey(hkeySubKey);
  101. if (lResult == ERROR_SUCCESS)
  102. lResult = lResultClose;
  103. }
  104. return(lResult);
  105. }
  106. //+---------------------------------------------------------------------------
  107. //
  108. // Function: GetRegKeyValue, private
  109. //
  110. // Synopsis: Internal utility function to add a named data value to an
  111. // existing Key (with optional Subkey) in the system Registry
  112. // under HKEY_CLASSES_ROOT.
  113. //
  114. // Arguments:
  115. //
  116. // Returns:
  117. //
  118. // Modifies:
  119. //
  120. //----------------------------------------------------------------------------
  121. BOOL AddRegNamedValue(
  122. LPTSTR pszKey,
  123. LPTSTR pszSubkey,
  124. LPTSTR pszValueName,
  125. LPTSTR pszValue)
  126. {
  127. BOOL bOk = FALSE;
  128. LONG ec;
  129. HKEY hKey;
  130. TCHAR szKey[MAX_STRING_LENGTH];
  131. lstrcpy(szKey, pszKey);
  132. if (NULL != pszSubkey)
  133. {
  134. lstrcat(szKey, TEXT("\\"));
  135. lstrcat(szKey, pszSubkey);
  136. }
  137. ec = RegOpenKeyEx(
  138. HKEY_CLASSES_ROOT,
  139. szKey,
  140. 0,
  141. KEY_READ | KEY_WRITE,
  142. &hKey);
  143. if (NULL != pszValue && ERROR_SUCCESS == ec)
  144. {
  145. ec = RegSetValueEx(
  146. hKey,
  147. pszValueName,
  148. 0,
  149. REG_SZ,
  150. (BYTE *)pszValue,
  151. (lstrlen(pszValue)+1)*sizeof(TCHAR));
  152. if (ERROR_SUCCESS == ec)
  153. bOk = TRUE;
  154. RegCloseKey(hKey);
  155. }
  156. return bOk;
  157. }
  158. //+---------------------------------------------------------------------------
  159. //
  160. // Function: RegGetTimeStamp, private
  161. //
  162. // Synopsis: Reads in TimeStamp Value under the specified HKEY
  163. //
  164. // Arguments:
  165. //
  166. // Returns:
  167. //
  168. // Modifies:
  169. //
  170. //----------------------------------------------------------------------------
  171. BOOL RegGetTimeStamp(HKEY hKey, FILETIME *pft)
  172. {
  173. DWORD dwType;
  174. FILETIME ft;
  175. LONG lr;
  176. DWORD dwSize = sizeof(FILETIME);
  177. Assert(pft);
  178. lr = RegQueryValueEx( hKey,
  179. HANDLERVALUE_FILETIME,
  180. NULL,
  181. &dwType,
  182. (BYTE *)&ft,
  183. &dwSize );
  184. if ( lr == ERROR_SUCCESS )
  185. {
  186. Assert( dwSize == sizeof(FILETIME) && dwType == REG_BINARY );
  187. *pft = ft;
  188. }
  189. else
  190. {
  191. // set the filetime to way back when to
  192. // any compares will just say older instead
  193. // of having to check success code
  194. (*pft).dwLowDateTime = 0;
  195. (*pft).dwHighDateTime = 0;
  196. }
  197. return TRUE;
  198. }
  199. //+---------------------------------------------------------------------------
  200. //
  201. // Function: RegWriteTimeStamp, private
  202. //
  203. // Synopsis: Writes out TimeStamp Value under the specified HKEY
  204. //
  205. // Arguments:
  206. //
  207. // Returns:
  208. //
  209. // Modifies:
  210. //
  211. //----------------------------------------------------------------------------
  212. BOOL RegWriteTimeStamp(HKEY hkey,FILETIME *pft)
  213. {
  214. LRESULT lr;
  215. lr = RegSetValueEx( hkey,
  216. HANDLERVALUE_FILETIME,
  217. NULL,
  218. REG_BINARY,
  219. (BYTE *) pft,
  220. sizeof(FILETIME) );
  221. return (ERROR_SUCCESS == lr) ? TRUE : FALSE;
  222. }
  223. //+---------------------------------------------------------------------------
  224. //
  225. // Function: CreateHandlerPrefKey, private
  226. //
  227. // Synopsis: given a server clsid does work of openning up the
  228. // handler perf key
  229. //
  230. // Arguments:
  231. //
  232. // Returns:
  233. //
  234. // Modifies:
  235. //
  236. //----------------------------------------------------------------------------
  237. HKEY CreateHandlerPrefKey(CLSID CLSIDServer)
  238. {
  239. TCHAR szFullKeyName[MAX_PATH];
  240. TCHAR szGUID[GUID_SIZE+1];
  241. LONG lResult = -1;
  242. HKEY hKey;
  243. #ifdef _UNICODE
  244. StringFromGUID2(CLSIDServer, szGUID, GUID_SIZE);
  245. #else
  246. BOOL fUsedDefaultChar;
  247. WCHAR wszID[GUID_SIZE+1];
  248. // convert clsidServer
  249. StringFromGUID2(CLSIDServer, wszID, GUID_SIZE);
  250. WideCharToMultiByte(CP_ACP ,0,
  251. wszID,-1,szGUID,GUID_SIZE + 1,
  252. NULL,&fUsedDefaultChar);
  253. #endif // _UNICODE
  254. lstrcpy(szFullKeyName, TEXT("CLSID\\"));
  255. lstrcat(szFullKeyName, szGUID);
  256. lstrcat(szFullKeyName,HANDLERITEMSKEY);
  257. // try to open handler items keyfs
  258. lResult = RegCreateKeyEx(
  259. HKEY_CLASSES_ROOT,
  260. szFullKeyName,
  261. 0,NULL,REG_OPTION_NON_VOLATILE,
  262. KEY_READ | KEY_WRITE,NULL,
  263. &hKey,NULL);
  264. return (ERROR_SUCCESS == lResult) ? hKey : NULL;
  265. }
  266. //+---------------------------------------------------------------------------
  267. //
  268. // Function: CreateHandlerItemPrefKey, private
  269. //
  270. // Synopsis: creates perf key for specified ItemID
  271. //
  272. // Arguments:
  273. //
  274. // Returns:
  275. //
  276. // Modifies:
  277. //
  278. //----------------------------------------------------------------------------
  279. HKEY CreateHandlerItemPrefKey(HKEY hkeyHandler,SYNCMGRITEMID ItemID)
  280. {
  281. TCHAR szGUID[GUID_SIZE+1];
  282. LONG lResult = -1;
  283. HKEY hKeyItem;
  284. // try to open/create item key
  285. #ifdef _UNICODE
  286. StringFromGUID2(ItemID, szGUID, GUID_SIZE);
  287. #else
  288. BOOL fUsedDefaultChar;
  289. WCHAR wszID[GUID_SIZE+1];
  290. // convert clsidServer
  291. StringFromGUID2(ItemID, wszID, GUID_SIZE);
  292. WideCharToMultiByte(CP_ACP ,0,
  293. wszID,-1,szGUID,GUID_SIZE + 1,
  294. NULL,&fUsedDefaultChar);
  295. #endif // _UNICODE
  296. // try to open handler items keyfs
  297. lResult = RegCreateKeyEx(
  298. hkeyHandler,
  299. szGUID,
  300. 0,NULL,REG_OPTION_NON_VOLATILE,
  301. KEY_READ | KEY_WRITE,NULL,
  302. &hKeyItem,NULL);
  303. return (ERROR_SUCCESS == lResult) ? hKeyItem : NULL;
  304. }
  305. //+---------------------------------------------------------------------------
  306. //
  307. // Function: CreateHandlerItemPrefKey, private
  308. //
  309. // Synopsis: creates perf key for specified ItemID
  310. //
  311. // Arguments:
  312. //
  313. // Returns:
  314. //
  315. // Modifies:
  316. //
  317. //----------------------------------------------------------------------------
  318. HKEY CreateHandlerItemPrefKey(CLSID CLSIDServer,SYNCMGRITEMID ItemID)
  319. {
  320. HKEY hKeyHandler;
  321. HKEY hKeyItem;
  322. if (hKeyHandler = CreateHandlerPrefKey(CLSIDServer))
  323. {
  324. hKeyItem = CreateHandlerItemPrefKey(hKeyHandler,ItemID);
  325. RegCloseKey(hKeyHandler); // close handler key.
  326. }
  327. return hKeyItem;
  328. }
  329. //+---------------------------------------------------------------------------
  330. //
  331. // Function: Reg_GetItemSettingsForHandlerItem, public
  332. //
  333. // Synopsis: Returns settings for the specified Item.
  334. //
  335. // Arguments:
  336. //
  337. // Returns:
  338. //
  339. // Modifies:
  340. //
  341. //----------------------------------------------------------------------------
  342. STDMETHODIMP Reg_GetItemSettingsForHandlerItem(HKEY hKeyHandler,SYNCMGRITEMID ItemID,
  343. LPSAMPLEITEMSETTINGS pSampleItemSettings)
  344. {
  345. HKEY hKey;
  346. TCHAR *pDisplayName;
  347. DWORD dwType;
  348. DWORD dwSize;
  349. hKey = CreateHandlerItemPrefKey(hKeyHandler,ItemID);
  350. if (!hKey)
  351. {
  352. Assert(hKey);
  353. return E_UNEXPECTED;
  354. }
  355. pSampleItemSettings->syncmgrItem.ItemID = ItemID;
  356. // write out our info.
  357. #ifdef _UNICODE
  358. pDisplayName = pSampleItemSettings->syncmgrItem.wszItemName;
  359. #else
  360. char szDisplayName[MAX_SYNCMGRITEMNAME];
  361. pDisplayName = szDisplayName;
  362. #endif // _UNICODE
  363. // read in the values,
  364. dwType = REG_SZ;
  365. dwSize = MAX_SYNCMGRITEMNAME*sizeof(TCHAR);
  366. RegQueryValueEx( hKey,
  367. HANDLERVALUE_DISPLAYNAME,
  368. NULL,
  369. &dwType,
  370. (BYTE *) pDisplayName,
  371. &dwSize);
  372. #ifndef _UNICODE
  373. MultiByteToWideChar(CP_ACP, 0,
  374. pDisplayName,-1,
  375. pSampleItemSettings->syncmgrItem.wszItemName,
  376. MAX_SYNCMGRITEMNAME);
  377. #endif
  378. dwType = REG_SZ;
  379. dwSize = MAX_PATH*sizeof(TCHAR);
  380. RegQueryValueEx( hKey,
  381. HANDLERVALUE_DIR1,
  382. NULL,
  383. &dwType,
  384. (BYTE *) pSampleItemSettings->dir1,
  385. &dwSize);
  386. dwType = REG_SZ;
  387. dwSize = MAX_PATH*sizeof(TCHAR);
  388. RegQueryValueEx( hKey,
  389. HANDLERVALUE_DIR2,
  390. NULL,
  391. &dwType,
  392. (BYTE *) pSampleItemSettings->dir2,
  393. &dwSize);
  394. dwType = REG_DWORD;
  395. dwSize = sizeof(DWORD);
  396. RegQueryValueEx( hKey,
  397. HANDLERVALUE_RECURSIVE,
  398. NULL,
  399. &dwType,
  400. (BYTE *) &(pSampleItemSettings->fRecursive),
  401. &dwSize);
  402. RegGetTimeStamp(hKey,&(pSampleItemSettings->syncmgrItem.ftLastUpdate));
  403. RegCloseKey(hKey);
  404. return NOERROR;
  405. }
  406. //+---------------------------------------------------------------------------
  407. //
  408. // Function: Reg_SetItemSettingsForHandlerItem, public
  409. //
  410. // Synopsis: Writes out settings for a handler item.
  411. //
  412. // Arguments:
  413. //
  414. // Returns:
  415. //
  416. // Modifies:
  417. //
  418. //----------------------------------------------------------------------------
  419. STDMETHODIMP Reg_SetItemSettingsForHandlerItem(CLSID CLSIDServer,SYNCMGRITEMID ItemID,LPSAMPLEITEMSETTINGS pSampleItemSettings)
  420. {
  421. HKEY hKey;
  422. TCHAR *pDisplayName;
  423. hKey = CreateHandlerItemPrefKey(CLSIDServer,ItemID);
  424. if (!hKey)
  425. {
  426. Assert(hKey);
  427. return E_UNEXPECTED;
  428. }
  429. // write out our info.
  430. #ifdef _UNICODE
  431. pDisplayName = pSampleItemSettings->syncmgrItem.wszItemName;
  432. #else
  433. BOOL fUsedDefaultChar;
  434. char szDisplayName[MAX_SYNCMGRITEMNAME];
  435. WideCharToMultiByte(CP_ACP ,0,
  436. pSampleItemSettings->syncmgrItem.wszItemName,-1,
  437. szDisplayName,MAX_SYNCMGRITEMNAME,
  438. NULL,&fUsedDefaultChar);
  439. pDisplayName = szDisplayName;
  440. #endif // _UNICODE
  441. RegSetValueEx(hKey,HANDLERVALUE_DISPLAYNAME,
  442. 0,
  443. REG_SZ,
  444. (BYTE *) pDisplayName,
  445. (lstrlen(pDisplayName) + 1)*sizeof(TCHAR));
  446. RegSetValueEx(hKey,HANDLERVALUE_DIR1,
  447. 0,
  448. REG_SZ,
  449. (BYTE *) pSampleItemSettings->dir1,
  450. (lstrlen(pSampleItemSettings->dir1) + 1)*sizeof(TCHAR));
  451. RegSetValueEx(hKey,HANDLERVALUE_DIR2,
  452. 0,
  453. REG_SZ,
  454. (BYTE *) pSampleItemSettings->dir2,
  455. (lstrlen(pSampleItemSettings->dir2) + 1)*sizeof(TCHAR));
  456. RegSetValueEx(hKey,HANDLERVALUE_RECURSIVE,
  457. 0,
  458. REG_DWORD,
  459. (BYTE *) &(pSampleItemSettings->fRecursive),
  460. sizeof(DWORD));
  461. RegWriteTimeStamp(hKey,&(pSampleItemSettings->syncmgrItem.ftLastUpdate));
  462. RegCloseKey(hKey);
  463. return NOERROR;
  464. }
  465. //+---------------------------------------------------------------------------
  466. //
  467. // Function: Reg_DeleteItemIdForHandlerItem, public
  468. //
  469. // Synopsis: Deletes an Items preferences from the registry..
  470. //
  471. // Arguments:
  472. //
  473. // Returns:
  474. //
  475. // Modifies:
  476. //
  477. //----------------------------------------------------------------------------
  478. STDMETHODIMP Reg_DeleteItemIdForHandlerItem(CLSID CLSIDServer,SYNCMGRITEMID ItemID)
  479. {
  480. HKEY hKeyHandler;
  481. if (hKeyHandler = CreateHandlerPrefKey( CLSIDServer))
  482. {
  483. TCHAR szGUID[GUID_SIZE+1];
  484. #ifdef _UNICODE
  485. StringFromGUID2(ItemID, szGUID, GUID_SIZE);
  486. #else
  487. BOOL fUsedDefaultChar;
  488. WCHAR wszID[GUID_SIZE+1];
  489. // convert clsidServer
  490. StringFromGUID2(ItemID, wszID, GUID_SIZE);
  491. WideCharToMultiByte(CP_ACP ,0,
  492. wszID,-1,szGUID,GUID_SIZE + 1,
  493. NULL,&fUsedDefaultChar);
  494. #endif // _UNICODE
  495. RegDeleteKey(hKeyHandler,szGUID);
  496. RegCloseKey(hKeyHandler);
  497. }
  498. return NOERROR;
  499. }