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.

4690 lines
141 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1998.
  5. //
  6. // File: Reg.cpp
  7. //
  8. // Contents: Registration routines
  9. //
  10. // Classes:
  11. //
  12. // Notes:
  13. //
  14. // History: 05-Nov-97 rogerg Created.
  15. // 11-18-97 susia Added Autosync and user reg key functions
  16. //
  17. //--------------------------------------------------------------------------
  18. #include "precomp.h"
  19. #ifdef _SENS
  20. #include <eventsys.h> // include event system
  21. #include <sens.h>
  22. #include <sensevts.h>
  23. #endif // _SENS
  24. // temporariy define new mstask flag in case hasn't
  25. // propogated to sdk\inc
  26. #ifndef TASK_FLAG_RUN_ONLY_IF_LOGGED_ON
  27. #define TASK_FLAG_RUN_ONLY_IF_LOGGED_ON (0x2000)
  28. #endif // TASK_FLAG_RUN_ONLY_IF_LOGGED_ON
  29. extern HINSTANCE g_hmodThisDll; // Handle to this DLL itself.
  30. extern CRITICAL_SECTION g_DllCriticalSection; // Global Critical Section for this DLL
  31. // only return success on NT 5.0
  32. BOOL GetUserDefaultSecurityAttribs(SECURITY_ATTRIBUTES *psa
  33. ,PSECURITY_DESCRIPTOR psd,
  34. PACL *ppOutAcl)
  35. {
  36. BOOL bRetVal;
  37. int cbAcl;
  38. PACL pAcl = NULL;
  39. PSID pInteractiveUserSid = NULL;
  40. PSID pLocalSystemSid = NULL;
  41. PSID pAdminsSid = NULL;
  42. SID_IDENTIFIER_AUTHORITY LocalSystemAuthority = SECURITY_NT_AUTHORITY;
  43. *ppOutAcl = NULL;
  44. bRetVal = FALSE;
  45. // in the structure.
  46. bRetVal = InitializeSecurityDescriptor(
  47. psd, // Pointer to SD
  48. SECURITY_DESCRIPTOR_REVISION // SD revision
  49. );
  50. if (!bRetVal)
  51. {
  52. AssertSz(0,"Unable to Init SecurityDescriptor");
  53. goto errRtn;
  54. }
  55. // setup acls.
  56. bRetVal = AllocateAndInitializeSid(
  57. &LocalSystemAuthority, // Pointer to identifier authority
  58. 1, // Count of subauthority
  59. SECURITY_INTERACTIVE_RID, // Subauthority 0
  60. 0, // Subauthority 1
  61. 0, // Subauthority 2
  62. 0, // Subauthority 3
  63. 0, // Subauthority 4
  64. 0, // Subauthority 5
  65. 0, // Subauthority 6
  66. 0, // Subauthority 7
  67. &pInteractiveUserSid // pointer to pointer to SID
  68. );
  69. if (!bRetVal)
  70. {
  71. AssertSz(0,"Alocate sid failed");
  72. goto errRtn;
  73. }
  74. bRetVal = AllocateAndInitializeSid(
  75. &LocalSystemAuthority, // Pointer to identifier authority
  76. 2,
  77. SECURITY_BUILTIN_DOMAIN_RID,
  78. DOMAIN_ALIAS_RID_ADMINS,
  79. 0,
  80. 0, // Subauthority 3
  81. 0, // Subauthority 4
  82. 0, // Subauthority 5
  83. 0, // Subauthority 6
  84. 0, // Subauthority 7
  85. &pAdminsSid // pointer to pointer to SID
  86. );
  87. if (!bRetVal)
  88. {
  89. AssertSz(0,"Alocate sid failed");
  90. goto errRtn;
  91. }
  92. bRetVal = AllocateAndInitializeSid(
  93. &LocalSystemAuthority,// Pointer to identifier authority
  94. 1, // Count of subauthority
  95. SECURITY_LOCAL_SYSTEM_RID, // Subauthority 0
  96. 0, // Subauthority 1
  97. 0, // Subauthority 2
  98. 0, // Subauthority 3
  99. 0, // Subauthority 4
  100. 0, // Subauthority 5
  101. 0, // Subauthority 6
  102. 0, // Subauthority 7
  103. &pLocalSystemSid // pointer to pointer to SID
  104. );
  105. if (!bRetVal)
  106. {
  107. AssertSz(0,"Alocate sid failed");
  108. goto errRtn;
  109. }
  110. cbAcl = sizeof (ACL)
  111. + 3 * sizeof (ACCESS_ALLOWED_ACE)
  112. + GetLengthSid(pInteractiveUserSid)
  113. + GetLengthSid(pLocalSystemSid)
  114. + GetLengthSid(pAdminsSid);
  115. pAcl = (PACL) new char[cbAcl];
  116. if (NULL == pAcl)
  117. {
  118. bRetVal = FALSE;
  119. AssertSz(0,"unable to alloc ACL");
  120. goto errRtn;
  121. }
  122. bRetVal = InitializeAcl(
  123. pAcl, // Pointer to the ACL
  124. cbAcl, // Size of ACL
  125. ACL_REVISION // Revision level of ACL
  126. );
  127. if (!bRetVal)
  128. {
  129. AssertSz(0,"InitAcl failed");
  130. goto errRtn;
  131. }
  132. bRetVal = AddAccessAllowedAce(
  133. pAcl, // Pointer to the ACL
  134. ACL_REVISION, // ACL revision level
  135. SPECIFIC_RIGHTS_ALL | GENERIC_READ | DELETE , // Access Mask
  136. pInteractiveUserSid // Pointer to SID
  137. );
  138. if (!bRetVal)
  139. {
  140. AssertSz(0,"AddAccessAllowed Failed");
  141. goto errRtn;
  142. }
  143. bRetVal = AddAccessAllowedAce(
  144. pAcl, // Pointer to the ACL
  145. ACL_REVISION, // ACL revision level
  146. GENERIC_ALL, // Access Mask
  147. pAdminsSid // Pointer to SID
  148. );
  149. if (!bRetVal)
  150. {
  151. AssertSz(0,"AddAccessAllowed Failed");
  152. goto errRtn;
  153. }
  154. bRetVal = AddAccessAllowedAce(
  155. pAcl, // Pointer to the ACL
  156. ACL_REVISION, // ACL revision level
  157. GENERIC_ALL, // Access Mask
  158. pLocalSystemSid // Pointer to SID
  159. );
  160. if (!bRetVal)
  161. {
  162. AssertSz(0,"AddAccessAllowed Failed");
  163. goto errRtn;
  164. }
  165. bRetVal = SetSecurityDescriptorDacl(psd,TRUE,pAcl,FALSE);
  166. if (!bRetVal)
  167. {
  168. AssertSz(0,"SetSecurityDescriptorDacl Failed");
  169. goto errRtn;
  170. }
  171. psa->nLength = sizeof(SECURITY_ATTRIBUTES);
  172. psa->lpSecurityDescriptor = psd;
  173. psa->bInheritHandle = FALSE;
  174. errRtn:
  175. if (pInteractiveUserSid)
  176. {
  177. FreeSid(pInteractiveUserSid);
  178. }
  179. if (pLocalSystemSid)
  180. {
  181. FreeSid(pLocalSystemSid);
  182. }
  183. if (pAdminsSid)
  184. {
  185. FreeSid(pAdminsSid);
  186. }
  187. //
  188. // On failure, we clean the ACL up. On success, the caller cleans
  189. // it up after using it.
  190. //
  191. if (FALSE == bRetVal)
  192. {
  193. if (pAcl)
  194. {
  195. delete[] pAcl;
  196. }
  197. }
  198. else
  199. {
  200. Assert(pAcl);
  201. *ppOutAcl = pAcl;
  202. }
  203. return bRetVal;
  204. }
  205. const WCHAR SZ_USERSIDKEY[] = TEXT("SID");
  206. // calls regOpen or create based on fCreate param
  207. LONG RegGetKeyHelper(HKEY hkey,LPCWSTR pszKey,REGSAM samDesired,BOOL fCreate,
  208. HKEY *phkResult,DWORD *pdwDisposition)
  209. {
  210. LONG lRet = -1;
  211. Assert(pdwDisposition);
  212. *pdwDisposition = 0;
  213. if (fCreate)
  214. {
  215. lRet = RegCreateKeyEx(hkey,pszKey,0,NULL,REG_OPTION_NON_VOLATILE,
  216. samDesired,NULL,phkResult,pdwDisposition);
  217. }
  218. else
  219. {
  220. lRet = RegOpenKeyEx(hkey,pszKey,0,samDesired,phkResult);
  221. if (ERROR_SUCCESS == lRet)
  222. {
  223. *pdwDisposition = REG_OPENED_EXISTING_KEY;
  224. }
  225. }
  226. return lRet;
  227. }
  228. // called to create a new UserKey or subkey
  229. LONG RegCreateUserSubKey(
  230. HKEY hKey,
  231. LPCWSTR lpSubKey,
  232. REGSAM samDesired,
  233. PHKEY phkResult)
  234. {
  235. LONG lRet;
  236. DWORD dwDisposition;
  237. lRet = RegCreateKeyEx(hKey,lpSubKey,0,NULL,REG_OPTION_NON_VOLATILE,
  238. samDesired,NULL,phkResult,&dwDisposition);
  239. // !! if subkey contains \\ don't traverse back through the list
  240. if ( (ERROR_SUCCESS == lRet) && (REG_CREATED_NEW_KEY == dwDisposition))
  241. {
  242. HKEY hKeySecurity;
  243. SECURITY_ATTRIBUTES sa;
  244. SECURITY_DESCRIPTOR sd;
  245. PACL pOutAcl;
  246. if (ERROR_SUCCESS == RegOpenKeyEx(hKey,
  247. lpSubKey,
  248. REG_OPTION_OPEN_LINK, WRITE_DAC,&hKeySecurity) )
  249. {
  250. if (GetUserDefaultSecurityAttribs(&sa,&sd,&pOutAcl))
  251. {
  252. RegSetKeySecurity(hKeySecurity,
  253. (SECURITY_INFORMATION) DACL_SECURITY_INFORMATION,
  254. &sd);
  255. delete[] pOutAcl;
  256. }
  257. RegCloseKey(hKeySecurity);
  258. }
  259. }
  260. return lRet;
  261. }
  262. STDAPI_(HKEY) RegOpenUserKey(HKEY hkeyParent,REGSAM samDesired,BOOL fCreate,BOOL fCleanReg)
  263. {
  264. TCHAR pszDomainAndUser[MAX_DOMANDANDMACHINENAMESIZE];
  265. HKEY hKeyUser;
  266. BOOL fSetUserSid = FALSE;
  267. WCHAR szUserSID[MAX_PATH + 1];
  268. DWORD dwDisposition;
  269. LONG ret;
  270. GetDefaultDomainAndUserName(pszDomainAndUser,TEXT("_"),ARRAYSIZE(pszDomainAndUser));
  271. // If suppose to clean the settings for the user/ delete the key
  272. if (fCleanReg)
  273. {
  274. RegDeleteKeyNT(hkeyParent,pszDomainAndUser);
  275. }
  276. if (ERROR_SUCCESS != (ret = RegGetKeyHelper(hkeyParent,pszDomainAndUser,samDesired,fCreate,
  277. &hKeyUser,&dwDisposition)))
  278. {
  279. hKeyUser = NULL;
  280. }
  281. if (hKeyUser)
  282. {
  283. WCHAR szRegSID[MAX_PATH + 1];
  284. DWORD cbRegSID = sizeof(szRegSID);
  285. if (ERROR_SUCCESS !=SHRegGetValue(hKeyUser,NULL,
  286. SZ_USERSIDKEY,SRRF_RT_REG_SZ | SRRF_NOEXPAND, NULL,
  287. (LPBYTE) szRegSID, &cbRegSID))
  288. {
  289. fSetUserSid = TRUE;
  290. // if have to set the sid need to make sure openned
  291. // with set Value and if didn't close key and
  292. // let create re-open it with the desired access.
  293. if (!(samDesired & KEY_SET_VALUE))
  294. {
  295. RegCloseKey(hKeyUser);
  296. hKeyUser = NULL;
  297. }
  298. }
  299. else
  300. {
  301. if (GetUserTextualSid(szUserSID, ARRAYSIZE(szUserSID)))
  302. {
  303. if (lstrcmp(szRegSID, szUserSID))
  304. {
  305. // if don't have access privledges
  306. // to delete the User this will fail.
  307. // may want a call into SENS to delete the
  308. // User Key on Failure.
  309. RegCloseKey(hKeyUser);
  310. hKeyUser = NULL; // set to NULL so check below fails.
  311. RegDeleteKeyNT(hkeyParent,pszDomainAndUser);
  312. }
  313. }
  314. }
  315. }
  316. if (NULL == hKeyUser)
  317. {
  318. if (ERROR_SUCCESS != (ret = RegGetKeyHelper(hkeyParent,pszDomainAndUser,
  319. samDesired,fCreate,
  320. &hKeyUser,&dwDisposition)))
  321. {
  322. hKeyUser = NULL;
  323. }
  324. else
  325. {
  326. if (REG_CREATED_NEW_KEY == dwDisposition)
  327. {
  328. fSetUserSid = TRUE;
  329. }
  330. }
  331. }
  332. // on creation setup the security
  333. if ( (ERROR_SUCCESS == ret) && (REG_CREATED_NEW_KEY == dwDisposition))
  334. {
  335. HKEY hKeySecurity;
  336. SECURITY_ATTRIBUTES sa;
  337. SECURITY_DESCRIPTOR sd;
  338. PACL pOutAcl;
  339. // !! should have own call for sync type key security
  340. if (ERROR_SUCCESS == RegOpenKeyEx(hkeyParent,pszDomainAndUser,
  341. REG_OPTION_OPEN_LINK, WRITE_DAC,&hKeySecurity) )
  342. {
  343. if (GetUserDefaultSecurityAttribs(&sa,&sd,&pOutAcl))
  344. {
  345. RegSetKeySecurity(hKeySecurity,
  346. (SECURITY_INFORMATION) DACL_SECURITY_INFORMATION,
  347. &sd);
  348. delete[] pOutAcl;
  349. }
  350. RegCloseKey(hKeySecurity);
  351. }
  352. }
  353. // setup the User sid.
  354. // depends on key being openned with KEY_SET_VALUE
  355. if (hKeyUser && fSetUserSid && (samDesired & KEY_SET_VALUE))
  356. {
  357. if (GetUserTextualSid(szUserSID, ARRAYSIZE(szUserSID)))
  358. {
  359. DWORD dwType = REG_SZ;
  360. RegSetValueEx (hKeyUser,SZ_USERSIDKEY,NULL,
  361. dwType,
  362. (LPBYTE) szUserSID,
  363. (lstrlen(szUserSID) + 1)*sizeof(WCHAR));
  364. }
  365. }
  366. return hKeyUser;
  367. }
  368. STDAPI_(HKEY) RegGetSyncTypeKey(DWORD dwSyncType,REGSAM samDesired,BOOL fCreate)
  369. {
  370. HKEY hKeySyncType;
  371. LPCWSTR pszKey;
  372. LONG ret;
  373. DWORD dwDisposition;
  374. // get appropriate key to open based on sync type
  375. switch(dwSyncType)
  376. {
  377. case SYNCTYPE_MANUAL:
  378. pszKey = MANUALSYNC_REGKEY;
  379. break;
  380. case SYNCTYPE_AUTOSYNC:
  381. pszKey = AUTOSYNC_REGKEY;
  382. break;
  383. case SYNCTYPE_IDLE:
  384. pszKey = IDLESYNC_REGKEY;
  385. break;
  386. case SYNCTYPE_SCHEDULED:
  387. pszKey = SCHEDSYNC_REGKEY;
  388. break;
  389. case SYNCTYPE_PROGRESS:
  390. pszKey = PROGRESS_REGKEY;
  391. break;
  392. default:
  393. AssertSz(0,"Unknown SyncType");
  394. pszKey = NULL;
  395. break;
  396. }
  397. if (NULL == pszKey)
  398. {
  399. return NULL;
  400. }
  401. // first try to open the existing key
  402. if (ERROR_SUCCESS != (ret = RegGetKeyHelper(HKEY_LOCAL_MACHINE,pszKey,samDesired,fCreate,
  403. &hKeySyncType,&dwDisposition)))
  404. {
  405. // if can't open key try to create
  406. if (ERROR_ACCESS_DENIED == ret )
  407. {
  408. // if access denied, call sens to reset
  409. // the security on the topelevel keys.
  410. SyncMgrExecCmd_ResetRegSecurity();
  411. ret = RegGetKeyHelper(HKEY_LOCAL_MACHINE,pszKey,samDesired,fCreate,
  412. &hKeySyncType,&dwDisposition);
  413. }
  414. if (ERROR_SUCCESS != ret)
  415. {
  416. hKeySyncType = NULL;
  417. }
  418. }
  419. if ( (ERROR_SUCCESS == ret) && (REG_CREATED_NEW_KEY == dwDisposition))
  420. {
  421. HKEY hKeySecurity;
  422. SECURITY_ATTRIBUTES sa;
  423. SECURITY_DESCRIPTOR sd;
  424. PACL pOutAcl;
  425. // !! should have own call for sync type key security
  426. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,pszKey,
  427. REG_OPTION_OPEN_LINK, WRITE_DAC,&hKeySecurity) )
  428. {
  429. if (GetUserDefaultSecurityAttribs(&sa,&sd,&pOutAcl))
  430. {
  431. RegSetKeySecurity(hKeySecurity,
  432. (SECURITY_INFORMATION) DACL_SECURITY_INFORMATION,
  433. &sd);
  434. delete[] pOutAcl;
  435. }
  436. RegCloseKey(hKeySecurity);
  437. }
  438. }
  439. return hKeySyncType;
  440. }
  441. STDAPI_(HKEY) RegGetCurrentUserKey(DWORD dwSyncType,REGSAM samDesired,BOOL fCreate)
  442. {
  443. HKEY hKeySyncType;
  444. HKEY hKeyUser = NULL;
  445. hKeySyncType = RegGetSyncTypeKey(dwSyncType,samDesired ,fCreate);
  446. if (hKeySyncType)
  447. {
  448. hKeyUser = RegOpenUserKey(hKeySyncType,samDesired,fCreate,FALSE /* fClean */);
  449. RegCloseKey(hKeySyncType);
  450. }
  451. return hKeyUser;
  452. }
  453. // tries to open and set security if necessary on the handler keys.
  454. STDAPI_(HKEY) RegGetHandlerTopLevelKey(REGSAM samDesired)
  455. {
  456. HKEY hKeyTopLevel;
  457. LONG ret;
  458. DWORD dwDisposition;
  459. // if open failed then try a create.
  460. if (ERROR_SUCCESS != (ret = RegCreateKeyEx (HKEY_LOCAL_MACHINE,HANDLERS_REGKEY,0, NULL,
  461. REG_OPTION_NON_VOLATILE,samDesired,NULL,
  462. &hKeyTopLevel,
  463. &dwDisposition)))
  464. {
  465. // if got an access denige on the handlers key
  466. // call sens to reset.
  467. if (ERROR_ACCESS_DENIED == ret )
  468. {
  469. // if access denied, call sens to reset
  470. // the security on the topelevel keys.
  471. // and try again
  472. SyncMgrExecCmd_ResetRegSecurity();
  473. ret = RegCreateKeyEx (HKEY_LOCAL_MACHINE,HANDLERS_REGKEY,0, NULL,
  474. REG_OPTION_NON_VOLATILE,samDesired,NULL,
  475. &hKeyTopLevel,
  476. &dwDisposition);
  477. }
  478. if (ERROR_SUCCESS != ret)
  479. {
  480. hKeyTopLevel = NULL;
  481. }
  482. }
  483. if ( (ERROR_SUCCESS == ret) && (REG_CREATED_NEW_KEY == dwDisposition))
  484. {
  485. HKEY hKeySecurity;
  486. SECURITY_ATTRIBUTES sa;
  487. SECURITY_DESCRIPTOR sd;
  488. PACL pOutAcl;
  489. // !! should have own call for toplevel handler key security
  490. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  491. HANDLERS_REGKEY,
  492. REG_OPTION_OPEN_LINK, WRITE_DAC,&hKeySecurity) )
  493. {
  494. if (GetUserDefaultSecurityAttribs(&sa,&sd,&pOutAcl))
  495. {
  496. RegSetKeySecurity(hKeySecurity,
  497. (SECURITY_INFORMATION) DACL_SECURITY_INFORMATION,
  498. &sd);
  499. delete[] pOutAcl;
  500. }
  501. RegCloseKey(hKeySecurity);
  502. }
  503. }
  504. return hKeyTopLevel;
  505. }
  506. STDAPI_(HKEY) RegGetHandlerKey(HKEY hkeyParent,LPCWSTR pszHandlerClsid,REGSAM samDesired,
  507. BOOL fCreate)
  508. {
  509. HKEY hKeyHandler = NULL;
  510. LRESULT lRet;
  511. DWORD dwDisposition;
  512. if (ERROR_SUCCESS != (lRet = RegGetKeyHelper(hkeyParent,pszHandlerClsid,samDesired,
  513. fCreate,&hKeyHandler,&dwDisposition)))
  514. {
  515. hKeyHandler = NULL;
  516. }
  517. if (NULL == hKeyHandler)
  518. {
  519. // if got an access denied call sens to unlock
  520. if (ERROR_ACCESS_DENIED == lRet )
  521. {
  522. // if access denied, call sens to reset
  523. // the security on the topelevel keys.
  524. // and try again
  525. SyncMgrExecCmd_ResetRegSecurity();
  526. lRet = RegGetKeyHelper(hkeyParent,pszHandlerClsid,samDesired,
  527. fCreate,&hKeyHandler,&dwDisposition);
  528. }
  529. if (ERROR_SUCCESS != lRet)
  530. {
  531. hKeyHandler = NULL;
  532. }
  533. }
  534. if ( (ERROR_SUCCESS == lRet) && (REG_CREATED_NEW_KEY == dwDisposition))
  535. {
  536. HKEY hKeySecurity;
  537. SECURITY_ATTRIBUTES sa;
  538. SECURITY_DESCRIPTOR sd;
  539. PACL pOutAcl;
  540. // !! should have own call for handler key security
  541. if (ERROR_SUCCESS == RegOpenKeyEx(hkeyParent,
  542. pszHandlerClsid,
  543. REG_OPTION_OPEN_LINK, WRITE_DAC,&hKeySecurity) )
  544. {
  545. if (GetUserDefaultSecurityAttribs(&sa,&sd,&pOutAcl))
  546. {
  547. RegSetKeySecurity(hKeySecurity,
  548. (SECURITY_INFORMATION) DACL_SECURITY_INFORMATION,
  549. &sd);
  550. delete[] pOutAcl;
  551. }
  552. RegCloseKey(hKeySecurity);
  553. }
  554. }
  555. return hKeyHandler;
  556. }
  557. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  558. Function: DWORD RegDeleteKeyNT(HKEY hStartKey , LPTSTR pKeyName )
  559. Summary: Recursively delete a key on NT
  560. Returns: Returns TRUE if successful, FALSE otherwise
  561. ------------------------------------------------------------------------F-F*/
  562. STDAPI_(DWORD) RegDeleteKeyNT(HKEY hStartKey , LPCWSTR pKeyName )
  563. {
  564. DWORD dwRtn, dwSubKeyLength;
  565. LPTSTR pSubKey = NULL;
  566. TCHAR szSubKey[MAX_KEY_LENGTH]; // (256) this should be dynamic.
  567. HKEY hkey;
  568. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  569. CMutexRegistry.Enter();
  570. // do not allow NULL or empty key name
  571. if ( pKeyName && lstrlen(pKeyName))
  572. {
  573. if( (dwRtn=RegOpenKeyEx(hStartKey,pKeyName,
  574. 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &hkey )) == ERROR_SUCCESS)
  575. {
  576. while (dwRtn == ERROR_SUCCESS )
  577. {
  578. dwSubKeyLength = MAX_KEY_LENGTH;
  579. dwRtn=RegEnumKeyEx(
  580. hkey,
  581. 0, // always index zero
  582. szSubKey,
  583. &dwSubKeyLength,
  584. NULL,
  585. NULL,
  586. NULL,
  587. NULL
  588. );
  589. if(dwRtn == ERROR_NO_MORE_ITEMS)
  590. {
  591. dwRtn = RegDeleteKey(hStartKey, pKeyName);
  592. break;
  593. }
  594. else if(dwRtn == ERROR_SUCCESS)
  595. dwRtn=RegDeleteKeyNT(hkey, szSubKey);
  596. }
  597. RegCloseKey(hkey);
  598. // Do not save return code because error
  599. // has already occurred
  600. }
  601. }
  602. else
  603. dwRtn = ERROR_BADKEY;
  604. CMutexRegistry.Leave();
  605. return dwRtn;
  606. }
  607. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  608. Function: RegGetProgressDetailsState()
  609. Summary: Gets the expanded or collapsed user preference for the progress dialog
  610. and the pushpin preference
  611. Returns: Returns TRUE if succeeded, FALSE otherwise
  612. ------------------------------------------------------------------------F-F*/
  613. STDAPI_(BOOL) RegGetProgressDetailsState(REFCLSID clsidDlg,BOOL *pfPushPin, BOOL *pfExpanded)
  614. {
  615. SCODE sc = S_FALSE;
  616. HKEY hkeyUserProgress,hkeyclsidDlg;
  617. WCHAR wszCLSID[GUID_SIZE + 1];
  618. if (0 == StringFromGUID2(clsidDlg, wszCLSID, ARRAYSIZE(wszCLSID)))
  619. {
  620. AssertSz(0,"Unable to make Guid a String");
  621. return FALSE;
  622. }
  623. //Prgress dialog defaults to collapsed, pushpin out
  624. *pfExpanded = FALSE;
  625. *pfPushPin = FALSE;
  626. hkeyUserProgress = RegGetCurrentUserKey(SYNCTYPE_PROGRESS,KEY_READ,FALSE);
  627. if (hkeyUserProgress)
  628. {
  629. if (ERROR_SUCCESS == RegOpenKeyEx(hkeyUserProgress,wszCLSID,0,KEY_READ,
  630. &hkeyclsidDlg))
  631. {
  632. DWORD cbDataSize = sizeof(*pfExpanded);
  633. if (ERROR_SUCCESS == SHRegGetValue(hkeyclsidDlg,NULL,TEXT("Expanded"),SRRF_RT_REG_DWORD, NULL,
  634. (LPBYTE) pfExpanded, &cbDataSize))
  635. {
  636. cbDataSize = sizeof(*pfPushPin);
  637. if (ERROR_SUCCESS == SHRegGetValue(hkeyclsidDlg,NULL,TEXT("PushPin"),SRRF_RT_REG_DWORD, NULL,
  638. (LPBYTE) pfPushPin, &cbDataSize))
  639. {
  640. sc = S_OK;
  641. }
  642. }
  643. RegCloseKey(hkeyclsidDlg);
  644. }
  645. RegCloseKey(hkeyUserProgress);
  646. }
  647. if (sc == S_OK)
  648. return TRUE;
  649. else
  650. return FALSE;
  651. }
  652. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  653. Function: RegSetProgressDetailsState(BOOL fExpanded)
  654. Summary: Sets the expanded or collapsed user preference for the progress dialog
  655. Returns: Returns TRUE if succeeded, FALSE otherwise
  656. ------------------------------------------------------------------------F-F*/
  657. STDAPI_(BOOL) RegSetProgressDetailsState(REFCLSID clsidDlg,BOOL fPushPin, BOOL fExpanded)
  658. {
  659. BOOL fResult = FALSE;
  660. HKEY hkeyUserProgress,hkeyclsidDlg;
  661. WCHAR wszCLSID[GUID_SIZE + 1];
  662. if (0 == StringFromGUID2(clsidDlg, wszCLSID, ARRAYSIZE(wszCLSID)))
  663. {
  664. AssertSz(0,"Unable to make Guid a String");
  665. return FALSE;
  666. }
  667. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  668. CMutexRegistry.Enter();
  669. hkeyUserProgress = RegGetCurrentUserKey(SYNCTYPE_PROGRESS,KEY_WRITE | KEY_READ,TRUE);
  670. if (hkeyUserProgress)
  671. {
  672. if (ERROR_SUCCESS == RegCreateUserSubKey(hkeyUserProgress,wszCLSID,
  673. KEY_WRITE | KEY_READ,
  674. &hkeyclsidDlg))
  675. {
  676. fResult = TRUE;
  677. if (ERROR_SUCCESS != RegSetValueEx(hkeyclsidDlg,TEXT("Expanded"),NULL, REG_DWORD,
  678. (LPBYTE) &(fExpanded),
  679. sizeof(fExpanded)))
  680. {
  681. fResult = FALSE;
  682. }
  683. if (ERROR_SUCCESS == RegSetValueEx(hkeyclsidDlg,TEXT("PushPin"),NULL, REG_DWORD,
  684. (LPBYTE) &(fPushPin),
  685. sizeof(fPushPin)))
  686. {
  687. fResult = FALSE;
  688. }
  689. RegCloseKey(hkeyclsidDlg);
  690. }
  691. RegCloseKey(hkeyUserProgress);
  692. }
  693. CMutexRegistry.Leave();
  694. return fResult;
  695. }
  696. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  697. Function: RegGetSyncItemSettings( DWORD dwSyncType,
  698. CLSID clsidHandler,
  699. SYNCMGRITEMID ItemId,
  700. const TCHAR *pszConnectionName,
  701. DWORD *pdwCheckState,
  702. DWORD dwDefaultCheckState,
  703. TCHAR *pszSchedName)
  704. Summary: Gets the settings per handler, itemID, and connection name.
  705. If no selections on this connection, the default is ?
  706. Returns: Returns TRUE if successful, FALSE otherwise
  707. ------------------------------------------------------------------------F-F*/
  708. STDAPI_(BOOL) RegGetSyncItemSettings( DWORD dwSyncType,
  709. CLSID clsidHandler,
  710. SYNCMGRITEMID ItemId,
  711. const TCHAR *pszConnectionName,
  712. DWORD *pdwCheckState,
  713. DWORD dwDefaultCheckState,
  714. TCHAR *pszSchedName)
  715. {
  716. HKEY hKeyUser;
  717. *pdwCheckState = dwDefaultCheckState;
  718. // special case schedule
  719. if (SYNCTYPE_SCHEDULED == dwSyncType)
  720. {
  721. // items are always turned off by default and if no schedule name
  722. // don't bother.
  723. *pdwCheckState = FALSE;
  724. if (!pszSchedName)
  725. {
  726. return FALSE;
  727. }
  728. }
  729. // open the user key for the type
  730. hKeyUser = RegGetCurrentUserKey(dwSyncType,KEY_READ,FALSE);
  731. if (NULL == hKeyUser)
  732. {
  733. return FALSE;
  734. }
  735. // for a schedule we need to go ahead and open up the schedule name
  736. // key
  737. HKEY hKeySchedule = NULL;
  738. // Review if want GetCurrentUserKey to Handle this.
  739. if (SYNCTYPE_SCHEDULED == dwSyncType)
  740. {
  741. if (ERROR_SUCCESS != RegOpenKeyEx((hKeyUser),
  742. pszSchedName,0,KEY_READ,
  743. &hKeySchedule))
  744. {
  745. hKeySchedule = NULL;
  746. RegCloseKey(hKeyUser);
  747. return FALSE;
  748. }
  749. }
  750. BOOL fResult;
  751. fResult = RegLookupSettings(hKeySchedule ? hKeySchedule : hKeyUser,
  752. clsidHandler,
  753. ItemId,
  754. pszConnectionName,
  755. pdwCheckState);
  756. if (hKeySchedule)
  757. {
  758. RegCloseKey(hKeySchedule);
  759. }
  760. if (hKeyUser)
  761. {
  762. RegCloseKey(hKeyUser);
  763. }
  764. return fResult;
  765. }
  766. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  767. Function: RegSetSyncItemSettings( DWORD dwSyncType,
  768. CLSID clsidHandler,
  769. SYNCMGRITEMID ItemId,
  770. const TCHAR *pszConnectionName,
  771. DWORD dwCheckState,
  772. TCHAR *pszSchedName)
  773. Summary: Sets the settings per handler, itemID, and connection name.
  774. Returns: Returns TRUE if successful, FALSE otherwise
  775. ------------------------------------------------------------------------F-F*/
  776. STDAPI_(BOOL) RegSetSyncItemSettings( DWORD dwSyncType,
  777. CLSID clsidHandler,
  778. SYNCMGRITEMID ItemId,
  779. const TCHAR *pszConnectionName,
  780. DWORD dwCheckState,
  781. TCHAR *pszSchedName)
  782. {
  783. HKEY hKeyUser;
  784. if (SYNCTYPE_SCHEDULED == dwSyncType)
  785. {
  786. if (NULL == pszSchedName)
  787. {
  788. return FALSE;
  789. }
  790. }
  791. // open the user key for the type
  792. hKeyUser = RegGetCurrentUserKey(dwSyncType,KEY_WRITE | KEY_READ,TRUE);
  793. if (NULL == hKeyUser)
  794. {
  795. return FALSE;
  796. }
  797. // for a schedule we need to go ahead and open up the schedule name
  798. // key
  799. HKEY hKeySchedule = NULL;
  800. if (SYNCTYPE_SCHEDULED == dwSyncType)
  801. {
  802. if (ERROR_SUCCESS != RegCreateUserSubKey(hKeyUser,
  803. pszSchedName,KEY_WRITE | KEY_READ,
  804. &hKeySchedule))
  805. {
  806. hKeySchedule = NULL;
  807. RegCloseKey(hKeyUser);
  808. return FALSE;
  809. }
  810. }
  811. BOOL fResult;
  812. fResult = RegWriteOutSettings(hKeySchedule ? hKeySchedule : hKeyUser,
  813. clsidHandler,
  814. ItemId,
  815. pszConnectionName,
  816. dwCheckState);
  817. if (hKeySchedule)
  818. {
  819. RegCloseKey(hKeySchedule);
  820. }
  821. if (hKeyUser)
  822. {
  823. RegCloseKey(hKeyUser);
  824. }
  825. return fResult;
  826. }
  827. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  828. Function: RegQueryLoadHandlerOnEvent( )
  829. Summary: Determines if there is any reason to load this handler
  830. for the specified event and Connection
  831. Returns: Returns TRUE if successful, FALSE otherwise
  832. ------------------------------------------------------------------------F-F*/
  833. STDAPI_(BOOL) RegQueryLoadHandlerOnEvent(TCHAR *pszClsid,DWORD dwSyncFlags,
  834. TCHAR *pConnectionName)
  835. {
  836. BOOL fLoadHandler = FALSE;
  837. DWORD dwSyncType;
  838. switch(dwSyncFlags & SYNCMGRFLAG_EVENTMASK)
  839. {
  840. case SYNCMGRFLAG_CONNECT:
  841. case SYNCMGRFLAG_PENDINGDISCONNECT:
  842. {
  843. dwSyncType = SYNCTYPE_AUTOSYNC;
  844. }
  845. break;
  846. case SYNCMGRFLAG_IDLE:
  847. {
  848. dwSyncType = SYNCTYPE_IDLE;
  849. }
  850. break;
  851. default:
  852. AssertSz(0,"Unknown SyncType");
  853. return FALSE;
  854. break;
  855. }
  856. // walk done the list openning keys.
  857. HKEY hkeySyncType;
  858. if (hkeySyncType = RegGetCurrentUserKey(dwSyncType,KEY_READ,FALSE))
  859. {
  860. HKEY hkeyConnectionName;
  861. if (ERROR_SUCCESS == RegOpenKeyEx(hkeySyncType,pConnectionName,0,KEY_READ,&hkeyConnectionName))
  862. {
  863. HKEY hkeyClsid;
  864. if (ERROR_SUCCESS == RegOpenKeyEx(hkeyConnectionName,pszClsid,0,KEY_READ,&hkeyClsid))
  865. {
  866. DWORD fQueryResult;
  867. DWORD cbDataSize = sizeof(fQueryResult);
  868. if (ERROR_SUCCESS == SHRegGetValue(hkeyClsid,NULL,TEXT("ItemsChecked"),SRRF_RT_REG_DWORD,
  869. NULL, (LPBYTE) &fQueryResult, &cbDataSize))
  870. {
  871. fLoadHandler = fQueryResult;
  872. }
  873. RegCloseKey(hkeyClsid);
  874. }
  875. RegCloseKey(hkeyConnectionName);
  876. }
  877. RegCloseKey(hkeySyncType);
  878. }
  879. return fLoadHandler;
  880. }
  881. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  882. Function: RegSetSyncHandlerSettings( )
  883. Summary: Sets the handler settings for syncType and Connection.
  884. Returns: Returns TRUE if successful, FALSE otherwise
  885. ------------------------------------------------------------------------F-F*/
  886. STDAPI_(BOOL) RegSetSyncHandlerSettings(DWORD dwSyncType,
  887. const TCHAR *pszConnectionName,
  888. CLSID clsidHandler,
  889. BOOL fItemsChecked)
  890. {
  891. HKEY hKeyUser;
  892. hKeyUser = RegGetCurrentUserKey(dwSyncType,KEY_WRITE | KEY_READ,TRUE);
  893. if (NULL == hKeyUser)
  894. {
  895. return FALSE;
  896. }
  897. SCODE sc;
  898. HKEY hkeyConnection;
  899. HKEY hkeyCLSID;
  900. TCHAR szCLSID[(GUID_SIZE + 1)];
  901. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  902. CMutexRegistry.Enter();
  903. smChkTo(EH_Err2,RegCreateUserSubKey(hKeyUser,
  904. pszConnectionName,
  905. KEY_WRITE | KEY_READ,
  906. &hkeyConnection));
  907. StringFromGUID2(clsidHandler, szCLSID, ARRAYSIZE(szCLSID));
  908. // Write entries under CLSID.
  909. smChkTo(EH_Err3,RegCreateUserSubKey(hkeyConnection,
  910. szCLSID,KEY_WRITE | KEY_READ,
  911. &hkeyCLSID));
  912. RegSetValueEx(hkeyCLSID,TEXT("ItemsChecked"),NULL, REG_DWORD,
  913. (LPBYTE) &fItemsChecked,
  914. sizeof(fItemsChecked));
  915. RegCloseKey(hKeyUser);
  916. RegCloseKey(hkeyConnection);
  917. RegCloseKey(hkeyCLSID);
  918. CMutexRegistry.Leave();
  919. return TRUE;
  920. EH_Err3:
  921. RegCloseKey(hkeyConnection);
  922. EH_Err2:
  923. RegCloseKey(hKeyUser);
  924. CMutexRegistry.Leave();
  925. return FALSE;
  926. }
  927. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  928. Function: RegLookupSettings(const TCHAR *hkeyName,
  929. CLSID clsidHandler,
  930. SYNCMGRITEMID ItemID,
  931. const TCHAR *pszConnectionName,
  932. DWORD pdwCheckState)
  933. Summary: Gets the settings per handler, itemID, and connection name.
  934. Returns: Returns TRUE if there are settings for this item and
  935. connection, flase otherwise.
  936. ------------------------------------------------------------------------F-F*/
  937. STDAPI_(BOOL) RegLookupSettings(HKEY hKeyUser,
  938. CLSID clsidHandler,
  939. SYNCMGRITEMID ItemID,
  940. const TCHAR *pszConnectionName,
  941. DWORD *pdwCheckState)
  942. {
  943. SCODE sc;
  944. HKEY hkeyConnection;
  945. HKEY hkeyItem;
  946. HKEY hKeyHandler;
  947. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  948. CMutexRegistry.Enter();
  949. TCHAR szID[GUID_SIZE+1];
  950. TCHAR szCLSID[2*(GUID_SIZE+1)];
  951. smChkTo(EH_Err2,RegOpenKeyEx(hKeyUser,
  952. pszConnectionName,0,KEY_READ,
  953. &hkeyConnection));
  954. StringFromGUID2(clsidHandler, szCLSID, ARRAYSIZE(szCLSID));
  955. StringFromGUID2(ItemID, szID, ARRAYSIZE(szID));
  956. // Read entries under CLSID.
  957. smChkTo(EH_Err3,RegOpenKeyEx((hkeyConnection),
  958. szCLSID, 0, KEY_READ,
  959. &hKeyHandler));
  960. smChkTo(EH_Err4,RegOpenKeyEx((hKeyHandler),
  961. szID, 0, KEY_READ,
  962. &hkeyItem));
  963. DWORD cbDataSize = sizeof(*pdwCheckState);
  964. smChkTo(EH_Err4,SHRegGetValue(hkeyItem,NULL,TEXT("CheckState"),SRRF_RT_REG_DWORD, NULL, (LPBYTE)pdwCheckState, &cbDataSize));
  965. RegCloseKey(hkeyConnection);
  966. RegCloseKey(hkeyItem);
  967. RegCloseKey(hKeyHandler);
  968. CMutexRegistry.Leave();
  969. return TRUE;
  970. EH_Err4:
  971. RegCloseKey(hKeyHandler);
  972. EH_Err3:
  973. RegCloseKey(hkeyConnection);
  974. EH_Err2:
  975. CMutexRegistry.Leave();
  976. return FALSE;
  977. }
  978. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  979. Function: RegWriteOutSettings(HKEY hKeyUser,
  980. CLSID clsidHandler,
  981. SYNCMGRITEMID ItemID,
  982. const TCHAR *pszConnectionName,
  983. DWORD dwCheckState)
  984. Summary: Sets the settings per handler, itemID, and connection name.
  985. Returns: Returns TRUE if we can set them, FALSE if there is an error
  986. ------------------------------------------------------------------------F-F*/
  987. STDAPI_(BOOL) RegWriteOutSettings(HKEY hKeyUser,
  988. CLSID clsidHandler,
  989. SYNCMGRITEMID ItemID,
  990. const TCHAR *pszConnectionName,
  991. DWORD dwCheckState)
  992. {
  993. SCODE sc;
  994. HKEY hkeyConnection;
  995. HKEY hKeyHandler;
  996. HKEY hkeyItem;
  997. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  998. CMutexRegistry.Enter();
  999. TCHAR szID[GUID_SIZE+1];
  1000. TCHAR szCLSID[2*(GUID_SIZE+1)];
  1001. smChkTo(EH_Err2,RegCreateUserSubKey(hKeyUser,
  1002. pszConnectionName,KEY_WRITE | KEY_READ,
  1003. &hkeyConnection));
  1004. StringFromGUID2(clsidHandler, szCLSID, ARRAYSIZE(szCLSID));
  1005. StringFromGUID2(ItemID, szID, ARRAYSIZE(szID));
  1006. smChkTo(EH_Err3,RegCreateUserSubKey(hkeyConnection,
  1007. szCLSID,KEY_WRITE | KEY_READ,
  1008. &hKeyHandler));
  1009. // Write entries under CLSID.
  1010. smChkTo(EH_Err4,RegCreateUserSubKey(hKeyHandler,
  1011. szID,KEY_WRITE | KEY_READ,
  1012. &hkeyItem));
  1013. smChkTo(EH_Err4,RegSetValueEx(hkeyItem,TEXT("CheckState"),NULL, REG_DWORD,
  1014. (LPBYTE) &dwCheckState,
  1015. sizeof(dwCheckState)));
  1016. RegCloseKey(hkeyConnection);
  1017. RegCloseKey(hkeyItem);
  1018. RegCloseKey(hKeyHandler);
  1019. CMutexRegistry.Leave();
  1020. return TRUE;
  1021. EH_Err4:
  1022. RegCloseKey(hKeyHandler);
  1023. EH_Err3:
  1024. RegCloseKey(hkeyConnection);
  1025. EH_Err2:
  1026. CMutexRegistry.Leave();
  1027. return FALSE;
  1028. }
  1029. /****************************************************************************
  1030. AutoSync Registry Functions
  1031. ***************************************************************************F-F*/
  1032. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1033. Function: RegGetAutoSyncSettings( LPCONNECTIONSETTINGS lpConnectionSettings)
  1034. Summary: Gets the logon, logoff and prompt me first user selections.
  1035. If no selections on this connection, the default is ?
  1036. Returns: Returns TRUE if successful, FALSE otherwise
  1037. ------------------------------------------------------------------------F-F*/
  1038. STDAPI_(BOOL) RegGetAutoSyncSettings(LPCONNECTIONSETTINGS lpConnectionSettings)
  1039. {
  1040. SCODE sc;
  1041. HKEY hkeyConnection;
  1042. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  1043. CMutexRegistry.Enter();
  1044. // Set these first to default values, in case there are no current
  1045. // user preferences in the registry
  1046. lpConnectionSettings->dwLogon = FALSE;
  1047. lpConnectionSettings->dwLogoff = FALSE;
  1048. lpConnectionSettings->dwPromptMeFirst = FALSE;
  1049. HKEY hKeyUser;
  1050. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_AUTOSYNC,KEY_READ,FALSE);
  1051. if (NULL == hKeyUser)
  1052. {
  1053. goto EH_Err;
  1054. }
  1055. DWORD cbDataSize = sizeof(lpConnectionSettings->dwLogon);
  1056. smChkTo(EH_Err3,RegOpenKeyEx(hKeyUser, lpConnectionSettings->pszConnectionName,0,KEY_READ,
  1057. &hkeyConnection));
  1058. smChkTo(EH_Err4,SHRegGetValue(hkeyConnection,NULL,TEXT("Logon"),SRRF_RT_REG_DWORD, NULL,
  1059. (LPBYTE) &(lpConnectionSettings->dwLogon),
  1060. &cbDataSize));
  1061. cbDataSize = sizeof(lpConnectionSettings->dwLogoff);
  1062. smChkTo(EH_Err4,SHRegGetValue(hkeyConnection,NULL,TEXT("Logoff"),SRRF_RT_REG_DWORD, NULL,
  1063. (LPBYTE) &(lpConnectionSettings->dwLogoff),
  1064. &cbDataSize));
  1065. cbDataSize = sizeof(lpConnectionSettings->dwPromptMeFirst);
  1066. smChkTo(EH_Err4,SHRegGetValue(hkeyConnection,NULL,TEXT("PromptMeFirst"),SRRF_RT_REG_DWORD, NULL,
  1067. (LPBYTE) &(lpConnectionSettings->dwPromptMeFirst),
  1068. &cbDataSize));
  1069. RegCloseKey(hkeyConnection);
  1070. RegCloseKey(hKeyUser);
  1071. CMutexRegistry.Leave();
  1072. return TRUE;
  1073. EH_Err4:
  1074. RegCloseKey(hkeyConnection);
  1075. EH_Err3:
  1076. RegCloseKey(hKeyUser);
  1077. EH_Err:
  1078. CMutexRegistry.Leave();
  1079. return FALSE;
  1080. }
  1081. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1082. Function: RegUpdateUserAutosyncKey
  1083. Summary: given an Autosync User Key makes sure it is in the latest
  1084. format and if not updates it.
  1085. Returns:
  1086. ------------------------------------------------------------------------F-F*/
  1087. void RegUpdateUserAutosyncKey(HKEY hkeyUser,BOOL fForce)
  1088. {
  1089. DWORD dwUserLogonLogoff;
  1090. DWORD cbDataSize = sizeof(dwUserLogonLogoff);
  1091. DWORD dwIndex = 0;
  1092. TCHAR lpName[MAX_PATH];
  1093. DWORD cbName = MAX_PATH;
  1094. DWORD dwLogon = 0;
  1095. DWORD dwLogoff = 0;
  1096. if (!fForce && (ERROR_SUCCESS == SHRegGetValue(hkeyUser,NULL,TEXT("Logon"),SRRF_RT_REG_DWORD, NULL,
  1097. (LPBYTE) &dwUserLogonLogoff,
  1098. &cbDataSize)) )
  1099. {
  1100. // if can open Logon Key this is up to date.
  1101. return;
  1102. }
  1103. // need to enum connection names and update the toplevel information.
  1104. while ( ERROR_SUCCESS == RegEnumKey(hkeyUser,dwIndex,
  1105. lpName,cbName) )
  1106. {
  1107. LONG lRet;
  1108. HKEY hKeyConnection;
  1109. lRet = RegOpenKeyEx( hkeyUser,
  1110. lpName,
  1111. NULL,
  1112. KEY_READ,
  1113. &hKeyConnection );
  1114. if (ERROR_SUCCESS == lRet)
  1115. {
  1116. cbDataSize = sizeof(dwUserLogonLogoff);
  1117. if (ERROR_SUCCESS == SHRegGetValue(hKeyConnection,NULL,TEXT("Logon"),SRRF_RT_REG_DWORD, NULL,
  1118. (LPBYTE) &dwUserLogonLogoff,
  1119. &cbDataSize) )
  1120. {
  1121. dwLogon |= dwUserLogonLogoff;
  1122. }
  1123. cbDataSize = sizeof(dwUserLogonLogoff);
  1124. if (ERROR_SUCCESS == SHRegGetValue(hKeyConnection,NULL,TEXT("Logoff"),SRRF_RT_REG_DWORD, NULL,
  1125. (LPBYTE) &dwUserLogonLogoff,
  1126. &cbDataSize) )
  1127. {
  1128. dwLogoff |= dwUserLogonLogoff;
  1129. }
  1130. RegCloseKey(hKeyConnection);
  1131. }
  1132. dwIndex++;
  1133. }
  1134. // write out new flags even if errors occured. work thing that happens is
  1135. // we don't set up someones autosync automatically.
  1136. RegSetValueEx(hkeyUser,TEXT("Logon"),NULL, REG_DWORD,
  1137. (LPBYTE) &(dwLogon), sizeof(dwLogon));
  1138. RegSetValueEx(hkeyUser,TEXT("Logoff"),NULL, REG_DWORD,
  1139. (LPBYTE) &(dwLogoff), sizeof(dwLogoff));
  1140. RegWriteTimeStamp(hkeyUser);
  1141. }
  1142. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1143. Function: RegUpdateAutoSyncKeyValueSettings
  1144. Summary: walks through the UserList Updating the AutoSync
  1145. Returns:
  1146. ------------------------------------------------------------------------F-F*/
  1147. void RegUpdateAutoSyncKeyValue(HKEY hkeyAutoSync,DWORD dwLogonDefault,DWORD dwLogoffDefault)
  1148. {
  1149. DWORD dwIndex = 0;
  1150. WCHAR lpName[MAX_PATH];
  1151. DWORD cbName = MAX_PATH;
  1152. LONG lRet;
  1153. DWORD dwLogon = 0;
  1154. DWORD dwLogoff = 0;
  1155. BOOL fSetLogon,fSetLogoff;
  1156. // need to walk the autosync user key and set the top level information
  1157. // based on whther someone logon/logoff
  1158. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  1159. CMutexRegistry.Enter();
  1160. while ( ERROR_SUCCESS == (lRet = RegEnumKey(hkeyAutoSync,dwIndex,
  1161. lpName,cbName) ))
  1162. {
  1163. DWORD dwUserLogonLogoff;
  1164. DWORD cbDataSize = sizeof(dwUserLogonLogoff);
  1165. HKEY hKeyDomainUser;
  1166. lRet = RegOpenKeyEx( hkeyAutoSync,
  1167. lpName,
  1168. NULL,
  1169. KEY_READ,
  1170. &hKeyDomainUser );
  1171. if (ERROR_SUCCESS == lRet)
  1172. {
  1173. // If Query fails don't want to count this as a failed to enum
  1174. // error so don't set lRet
  1175. if (ERROR_SUCCESS == (SHRegGetValue(hKeyDomainUser,NULL,TEXT("Logon"),SRRF_RT_REG_DWORD, NULL,
  1176. (LPBYTE) &dwUserLogonLogoff,
  1177. &cbDataSize) ))
  1178. {
  1179. dwLogon |= dwUserLogonLogoff;
  1180. }
  1181. cbDataSize = sizeof(dwUserLogonLogoff);
  1182. if (ERROR_SUCCESS == lRet)
  1183. {
  1184. if (ERROR_SUCCESS == (SHRegGetValue(hKeyDomainUser,NULL,TEXT("Logoff"),SRRF_RT_REG_DWORD, NULL,
  1185. (LPBYTE) &dwUserLogonLogoff,
  1186. &cbDataSize) ) )
  1187. {
  1188. dwLogoff |= dwUserLogonLogoff;
  1189. }
  1190. }
  1191. RegCloseKey(hKeyDomainUser);
  1192. }
  1193. if (ERROR_SUCCESS != lRet)
  1194. {
  1195. break;
  1196. }
  1197. dwIndex++;
  1198. }
  1199. fSetLogon = FALSE;
  1200. fSetLogoff = FALSE;
  1201. // if an error occured, then use the passed in defaults,
  1202. // if set to 1, else don't set.
  1203. if ( (ERROR_SUCCESS != lRet) && (ERROR_NO_MORE_ITEMS != lRet))
  1204. {
  1205. if ( (-1 != dwLogonDefault) && (0 != dwLogonDefault))
  1206. {
  1207. fSetLogon = TRUE;
  1208. dwLogon = dwLogonDefault;
  1209. }
  1210. if ( (-1 != dwLogoffDefault) && (0 != dwLogoffDefault))
  1211. {
  1212. fSetLogoff = TRUE;
  1213. dwLogoff = dwLogoffDefault;
  1214. }
  1215. }
  1216. else
  1217. {
  1218. fSetLogon = TRUE;
  1219. fSetLogoff = TRUE;
  1220. }
  1221. // write out new flags even if errors occured. work thing that happens is
  1222. // we don't set up someones autosync automatically.
  1223. if (fSetLogon)
  1224. {
  1225. RegSetValueEx(hkeyAutoSync,TEXT("Logon"),NULL, REG_DWORD,
  1226. (LPBYTE) &(dwLogon), sizeof(dwLogon));
  1227. }
  1228. if (fSetLogoff)
  1229. {
  1230. RegSetValueEx(hkeyAutoSync,TEXT("Logoff"),NULL, REG_DWORD,
  1231. (LPBYTE) &(dwLogoff), sizeof(dwLogoff));
  1232. }
  1233. RegWriteTimeStamp(hkeyAutoSync);
  1234. CMutexRegistry.Leave();
  1235. }
  1236. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1237. Function: RegUpdateUserIdleKey
  1238. Summary: given an Idle User Key makes sure it is in the latest
  1239. format and if not updates it.
  1240. Returns:
  1241. ------------------------------------------------------------------------F-F*/
  1242. void RegUpdateUserIdleKey(HKEY hkeyUser,BOOL fForce)
  1243. {
  1244. DWORD dwUserIdleEnabled;
  1245. DWORD cbDataSize = sizeof(dwUserIdleEnabled);
  1246. DWORD dwIndex = 0;
  1247. TCHAR lpName[MAX_PATH];
  1248. DWORD cbName = MAX_PATH;
  1249. DWORD dwIdleEnabled = 0;
  1250. if (!fForce && (ERROR_SUCCESS == SHRegGetValue(hkeyUser,NULL,TEXT("IdleEnabled"),SRRF_RT_REG_DWORD, NULL,
  1251. (LPBYTE) &dwUserIdleEnabled,
  1252. &cbDataSize)) )
  1253. {
  1254. // if can open Logon Key this is up to date.
  1255. return;
  1256. }
  1257. // need to enum connection names and update the toplevel information.
  1258. while ( ERROR_SUCCESS == RegEnumKey(hkeyUser,dwIndex,
  1259. lpName,cbName) )
  1260. {
  1261. LONG lRet;
  1262. HKEY hKeyConnection;
  1263. lRet = RegOpenKeyEx( hkeyUser,
  1264. lpName,
  1265. NULL,
  1266. KEY_READ,
  1267. &hKeyConnection );
  1268. if (ERROR_SUCCESS == lRet)
  1269. {
  1270. cbDataSize = sizeof(dwUserIdleEnabled);
  1271. if (ERROR_SUCCESS == SHRegGetValue(hKeyConnection,NULL,TEXT("IdleEnabled"),SRRF_RT_REG_DWORD, NULL,
  1272. (LPBYTE) &dwUserIdleEnabled,
  1273. &cbDataSize) )
  1274. {
  1275. dwIdleEnabled |= dwUserIdleEnabled;
  1276. }
  1277. RegCloseKey(hKeyConnection);
  1278. }
  1279. dwIndex++;
  1280. }
  1281. // write out new flags even if errors occured. work thing that happens is
  1282. // we don't set up someones autosync automatically.
  1283. RegSetValueEx(hkeyUser,TEXT("IdleEnabled"),NULL, REG_DWORD,
  1284. (LPBYTE) &(dwIdleEnabled), sizeof(dwIdleEnabled));
  1285. RegWriteTimeStamp(hkeyUser);
  1286. }
  1287. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1288. Function: RegUpdateIdleKeyValue
  1289. Summary: walks through the UserList Updating the Idle RegKey
  1290. Returns:
  1291. ------------------------------------------------------------------------F-F*/
  1292. void RegUpdateIdleKeyValue(HKEY hkeyIdle,DWORD dwDefault)
  1293. {
  1294. DWORD dwIndex = 0;
  1295. WCHAR lpName[MAX_PATH];
  1296. DWORD dwIdleEnabled = 0;
  1297. LONG lRet = -1;
  1298. BOOL fSetDefault;
  1299. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  1300. CMutexRegistry.Enter();
  1301. // need to walk the idle user key and set the top level information
  1302. // based on whther someone logon/logoff
  1303. fSetDefault = FALSE;
  1304. while ( ERROR_SUCCESS == (lRet = RegEnumKey(hkeyIdle,dwIndex,
  1305. lpName,ARRAYSIZE(lpName))) )
  1306. {
  1307. DWORD dwUserIdleEnabled;
  1308. DWORD cbDataSize = sizeof(dwUserIdleEnabled);
  1309. HKEY hKeyDomainUser;
  1310. lRet = RegOpenKeyEx( hkeyIdle,
  1311. lpName,
  1312. NULL,
  1313. KEY_READ,
  1314. &hKeyDomainUser );
  1315. if (ERROR_SUCCESS == lRet)
  1316. {
  1317. // if query fails don't consider this an error as far as
  1318. // setDefault goes.
  1319. if (ERROR_SUCCESS == (SHRegGetValue(hKeyDomainUser,NULL,TEXT("IdleEnabled"),SRRF_RT_REG_DWORD, NULL,
  1320. (LPBYTE) &dwUserIdleEnabled,
  1321. &cbDataSize) ))
  1322. {
  1323. dwIdleEnabled |= dwUserIdleEnabled;
  1324. }
  1325. RegCloseKey(hKeyDomainUser);
  1326. }
  1327. if (ERROR_SUCCESS != lRet)
  1328. {
  1329. break;
  1330. }
  1331. dwIndex++;
  1332. }
  1333. // if an error occured, then use the passed in defaults,
  1334. // if set to 1, else don't set.
  1335. if ( (ERROR_SUCCESS != lRet) && (ERROR_NO_MORE_ITEMS != lRet))
  1336. {
  1337. if ( (-1 != dwDefault) && (0 != dwDefault))
  1338. {
  1339. fSetDefault = TRUE;
  1340. dwIdleEnabled = dwDefault;
  1341. }
  1342. }
  1343. else
  1344. {
  1345. fSetDefault = TRUE;
  1346. }
  1347. // write out new flags even if errors occured. work thing that happens is
  1348. // we don't set up someones autosync automatically.
  1349. if (fSetDefault)
  1350. {
  1351. RegSetValueEx(hkeyIdle,TEXT("IdleEnabled"),NULL, REG_DWORD,
  1352. (LPBYTE) &(dwIdleEnabled), sizeof(dwIdleEnabled));
  1353. }
  1354. RegWriteTimeStamp(hkeyIdle);
  1355. CMutexRegistry.Leave();
  1356. }
  1357. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1358. Function: RegSetAutoSyncSettings( LPCONNECTIONSETTINGS lpConnectionSettings,
  1359. int iNumConnections)
  1360. Summary: Sets the logon, logoff and prompt me first user selections.
  1361. Returns: Returns TRUE if successful, FALSE otherwise
  1362. ------------------------------------------------------------------------F-F*/
  1363. STDAPI_(BOOL) RegSetAutoSyncSettings(LPCONNECTIONSETTINGS lpConnectionSettings,
  1364. int iNumConnections,
  1365. CRasUI *pRas,
  1366. BOOL fCleanReg,
  1367. BOOL fSetMachineState,
  1368. BOOL fPerUser)
  1369. {
  1370. SCODE sc = S_OK;
  1371. HKEY hAutoSync;
  1372. HKEY hKeyUser;
  1373. HKEY hkeyConnection;
  1374. DWORD dwUserConfigured = 1;
  1375. DWORD dwLogonDefault = -1;
  1376. DWORD dwLogoffDefault = -1;
  1377. int i;
  1378. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  1379. CMutexRegistry.Enter();
  1380. // make sure keys are converted to newest format
  1381. RegUpdateTopLevelKeys();
  1382. Assert(-1 != TRUE); // we rely on TRUE boolean being something other than -1
  1383. hAutoSync = RegGetSyncTypeKey(SYNCTYPE_AUTOSYNC,KEY_WRITE| KEY_READ,TRUE);
  1384. if (NULL == hAutoSync)
  1385. {
  1386. goto EH_Err;
  1387. }
  1388. hKeyUser = RegOpenUserKey(hAutoSync,KEY_WRITE| KEY_READ,TRUE,fCleanReg);
  1389. if (NULL == hKeyUser)
  1390. {
  1391. goto EH_Err2;
  1392. }
  1393. if (fPerUser)
  1394. {
  1395. smChkTo(EH_Err3,RegSetValueEx(hKeyUser,TEXT("UserConfigured"),NULL, REG_DWORD,
  1396. (LPBYTE) &dwUserConfigured,
  1397. sizeof(dwUserConfigured)));
  1398. }
  1399. else
  1400. {
  1401. DWORD cbDataSize = sizeof(dwUserConfigured);
  1402. //If this value isn't added yet,
  1403. if (ERROR_SUCCESS == SHRegGetValue(hKeyUser,NULL,TEXT("UserConfigured"),SRRF_RT_REG_DWORD, NULL,
  1404. (LPBYTE) &dwUserConfigured,
  1405. &cbDataSize))
  1406. {
  1407. //if the user setup their configuration, we won't override on a
  1408. //subsequent handler registration.
  1409. if (dwUserConfigured)
  1410. {
  1411. RegCloseKey(hKeyUser);
  1412. RegCloseKey(hAutoSync);
  1413. CMutexRegistry.Leave();
  1414. return TRUE;
  1415. }
  1416. }
  1417. }
  1418. for (i=0; i<iNumConnections; i++)
  1419. {
  1420. smChkTo(EH_Err3,RegCreateUserSubKey (hKeyUser,
  1421. lpConnectionSettings[i].pszConnectionName,
  1422. KEY_WRITE | KEY_READ,
  1423. &hkeyConnection));
  1424. if (-1 != lpConnectionSettings[i].dwLogon)
  1425. {
  1426. if (0 != lpConnectionSettings[i].dwLogon)
  1427. {
  1428. dwLogonDefault = lpConnectionSettings[i].dwLogon;
  1429. }
  1430. smChkTo(EH_Err4,RegSetValueEx(hkeyConnection,TEXT("Logon"),NULL, REG_DWORD,
  1431. (LPBYTE) &(lpConnectionSettings[i].dwLogon),
  1432. sizeof(lpConnectionSettings[i].dwLogon)));
  1433. }
  1434. if (-1 != lpConnectionSettings[i].dwLogoff)
  1435. {
  1436. if (0 != lpConnectionSettings[i].dwLogoff)
  1437. {
  1438. dwLogoffDefault = lpConnectionSettings[i].dwLogoff;
  1439. }
  1440. smChkTo(EH_Err4,RegSetValueEx(hkeyConnection,TEXT("Logoff"),NULL, REG_DWORD,
  1441. (LPBYTE) &(lpConnectionSettings[i].dwLogoff),
  1442. sizeof(lpConnectionSettings[i].dwLogoff)));
  1443. }
  1444. if (-1 != lpConnectionSettings[i].dwPromptMeFirst)
  1445. {
  1446. smChkTo(EH_Err4,RegSetValueEx(hkeyConnection,TEXT("PromptMeFirst"),NULL, REG_DWORD,
  1447. (LPBYTE) &(lpConnectionSettings[i].dwPromptMeFirst),
  1448. sizeof(lpConnectionSettings[i].dwPromptMeFirst)));
  1449. }
  1450. RegCloseKey(hkeyConnection);
  1451. }
  1452. // update the toplevel User information
  1453. RegUpdateUserAutosyncKey(hKeyUser,TRUE /* fForce */);
  1454. // update the top-level key
  1455. RegUpdateAutoSyncKeyValue(hAutoSync,dwLogonDefault,dwLogoffDefault);
  1456. RegCloseKey(hKeyUser);
  1457. RegCloseKey(hAutoSync);
  1458. // update our global sens state based on Autosync and Registration
  1459. if (fSetMachineState)
  1460. {
  1461. RegRegisterForEvents(FALSE /* fUninstall */);
  1462. }
  1463. CMutexRegistry.Leave();
  1464. return TRUE;
  1465. EH_Err4:
  1466. RegCloseKey(hkeyConnection);
  1467. EH_Err3:
  1468. RegCloseKey(hKeyUser);
  1469. EH_Err2:
  1470. RegCloseKey(hAutoSync);
  1471. EH_Err:
  1472. CMutexRegistry.Leave();
  1473. return FALSE;
  1474. }
  1475. /****************************************************************************
  1476. Scheduled Sync Registry Functions
  1477. ***************************************************************************F-F*/
  1478. //--------------------------------------------------------------------------------
  1479. //
  1480. // FUNCTION: RegGetSchedFriendlyName(LPCTSTR ptszScheduleGUIDName,
  1481. // LPTSTR ptstrFriendlyName,
  1482. // UINT cchFriendlyName)
  1483. //
  1484. // PURPOSE: Get the friendly name of this Schedule.
  1485. //
  1486. // History: 27-Feb-98 susia Created.
  1487. //
  1488. //--------------------------------------------------------------------------------
  1489. STDAPI_(BOOL) RegGetSchedFriendlyName(LPCTSTR ptszScheduleGUIDName,
  1490. LPTSTR ptstrFriendlyName,
  1491. UINT cchFriendlyName)
  1492. {
  1493. BOOL fResult = FALSE;
  1494. HKEY hKeyUser;
  1495. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ,FALSE);
  1496. if (NULL != hKeyUser)
  1497. {
  1498. HKEY hkeySchedName;
  1499. if (NOERROR == RegOpenKeyEx (hKeyUser, ptszScheduleGUIDName, 0,KEY_READ,
  1500. &hkeySchedName))
  1501. {
  1502. DWORD cbFriendlyName = cchFriendlyName * sizeof(TCHAR);
  1503. if (ERROR_SUCCESS == SHRegGetValue(hkeySchedName,NULL,TEXT("FriendlyName"),SRRF_RT_REG_SZ | SRRF_NOEXPAND, NULL,
  1504. (LPBYTE) ptstrFriendlyName, &cbFriendlyName))
  1505. {
  1506. fResult = TRUE;
  1507. }
  1508. RegCloseKey(hkeySchedName);
  1509. }
  1510. }
  1511. RegCloseKey(hKeyUser);
  1512. return fResult;
  1513. }
  1514. //--------------------------------------------------------------------------------
  1515. //
  1516. // FUNCTION: RegSetSchedFriendlyName(LPCTSTR ptszScheduleGUIDName,
  1517. // LPCTSTR ptstrFriendlyName)
  1518. //
  1519. // PURPOSE: Set the friendly name of this Schedule.
  1520. //
  1521. // History: 27-Feb-98 susia Created.
  1522. //
  1523. //--------------------------------------------------------------------------------
  1524. STDAPI_(BOOL) RegSetSchedFriendlyName(LPCTSTR ptszScheduleGUIDName,
  1525. LPCTSTR ptstrFriendlyName)
  1526. {
  1527. SCODE sc;
  1528. HKEY hkeySchedName;
  1529. HKEY hKeyUser;
  1530. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  1531. CMutexRegistry.Enter();
  1532. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_WRITE | KEY_READ,TRUE);
  1533. if (NULL == hKeyUser)
  1534. {
  1535. goto EH_Err;
  1536. }
  1537. smChkTo(EH_Err3,RegCreateUserSubKey (hKeyUser, ptszScheduleGUIDName,
  1538. KEY_WRITE | KEY_READ,
  1539. &hkeySchedName));
  1540. smChkTo(EH_Err4,RegSetValueEx (hkeySchedName,TEXT("FriendlyName"),NULL,
  1541. REG_SZ,
  1542. (LPBYTE) ptstrFriendlyName,
  1543. (lstrlen(ptstrFriendlyName) + 1)*sizeof(TCHAR)));
  1544. RegCloseKey(hkeySchedName);
  1545. RegCloseKey(hKeyUser);
  1546. CMutexRegistry.Leave();
  1547. return TRUE;
  1548. EH_Err4:
  1549. RegCloseKey(hkeySchedName);
  1550. EH_Err3:
  1551. RegCloseKey(hKeyUser);
  1552. EH_Err:
  1553. CMutexRegistry.Leave();
  1554. return FALSE;
  1555. }
  1556. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1557. Function: RegGetSchedSyncSettings( LPCONNECTIONSETTINGS lpConnectionSettings,
  1558. TCHAR *pszSchedName)
  1559. Summary: Gets the MakeAutoConnection user selections.
  1560. If no selections on this connection, the default is FALSE
  1561. Returns: Returns TRUE if successful, FALSE otherwise
  1562. ------------------------------------------------------------------------F-F*/
  1563. STDAPI_(BOOL) RegGetSchedSyncSettings(LPCONNECTIONSETTINGS lpConnectionSettings,
  1564. TCHAR *pszSchedName)
  1565. {
  1566. SCODE sc;
  1567. HKEY hkeySchedName,
  1568. hkeyConnection;
  1569. DWORD cbDataSize = sizeof(lpConnectionSettings->dwHidden);
  1570. HKEY hKeyUser;
  1571. // Set these first to default values, in case there are no current
  1572. // user preferences in the registry
  1573. lpConnectionSettings->dwConnType = SYNCSCHEDINFO_FLAGS_CONNECTION_LAN;
  1574. lpConnectionSettings->dwMakeConnection = FALSE;
  1575. lpConnectionSettings->dwHidden = FALSE;
  1576. lpConnectionSettings->dwReadOnly = FALSE;
  1577. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ,FALSE);
  1578. if (NULL == hKeyUser)
  1579. {
  1580. goto EH_Err2;
  1581. }
  1582. smChkTo(EH_Err3,RegOpenKeyEx (hKeyUser,
  1583. pszSchedName,0,KEY_READ,
  1584. &hkeySchedName));
  1585. smChkTo(EH_Err4,SHRegGetValue(hkeySchedName,NULL,TEXT("ScheduleHidden"),SRRF_RT_REG_DWORD, NULL,
  1586. (LPBYTE) &(lpConnectionSettings->dwHidden),
  1587. &cbDataSize));
  1588. cbDataSize = sizeof(lpConnectionSettings->dwReadOnly);
  1589. smChkTo(EH_Err4,SHRegGetValue(hkeySchedName,NULL,TEXT("ScheduleReadOnly"),SRRF_RT_REG_DWORD, NULL,
  1590. (LPBYTE) &(lpConnectionSettings->dwReadOnly),
  1591. &cbDataSize));
  1592. smChkTo(EH_Err4,RegOpenKeyEx (hkeySchedName,
  1593. lpConnectionSettings->pszConnectionName,
  1594. 0,KEY_READ,
  1595. &hkeyConnection));
  1596. cbDataSize = sizeof(lpConnectionSettings->dwMakeConnection);
  1597. smChkTo(EH_Err5,SHRegGetValue(hkeyConnection,NULL,TEXT("MakeAutoConnection"),SRRF_RT_REG_DWORD, NULL,
  1598. (LPBYTE) &(lpConnectionSettings->dwMakeConnection),
  1599. &cbDataSize));
  1600. cbDataSize = sizeof(lpConnectionSettings->dwConnType);
  1601. smChkTo(EH_Err5,SHRegGetValue(hkeyConnection,NULL,TEXT("Connection Type"),SRRF_RT_REG_DWORD, NULL,
  1602. (LPBYTE) &(lpConnectionSettings->dwConnType),
  1603. &cbDataSize));
  1604. RegCloseKey(hkeyConnection);
  1605. RegCloseKey(hkeySchedName);
  1606. RegCloseKey(hKeyUser);
  1607. return TRUE;
  1608. EH_Err5:
  1609. RegCloseKey(hkeyConnection);
  1610. EH_Err4:
  1611. RegCloseKey(hkeySchedName);
  1612. EH_Err3:
  1613. RegCloseKey(hKeyUser);
  1614. EH_Err2:
  1615. return FALSE;
  1616. }
  1617. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1618. Function: RegSetSchedSyncSettings( LPCONNECTIONSETTINGS lpConnectionSettings,
  1619. TCHAR *pszSchedName)
  1620. Summary: Sets the hidden and readonly schedule flags.
  1621. Returns: Returns TRUE if successful, FALSE otherwise
  1622. ------------------------------------------------------------------------F-F*/
  1623. STDAPI_(BOOL) RegSetSchedSyncSettings(LPCONNECTIONSETTINGS lpConnectionSettings,
  1624. TCHAR *pszSchedName)
  1625. {
  1626. BOOL fRetVal = FALSE;
  1627. HKEY hKeyUser,
  1628. hkeySchedName,
  1629. hkeyConnection;
  1630. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  1631. CMutexRegistry.Enter();
  1632. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ | KEY_WRITE,TRUE);
  1633. if (hKeyUser)
  1634. {
  1635. if (ERROR_SUCCESS == RegCreateUserSubKey (hKeyUser,
  1636. pszSchedName,
  1637. KEY_WRITE | KEY_READ,
  1638. &hkeySchedName))
  1639. {
  1640. if (ERROR_SUCCESS == RegSetValueEx(hkeySchedName,TEXT("ScheduleHidden"),NULL, REG_DWORD,
  1641. (LPBYTE) &(lpConnectionSettings->dwHidden),
  1642. sizeof(lpConnectionSettings->dwHidden)))
  1643. {
  1644. if (ERROR_SUCCESS == RegSetValueEx(hkeySchedName,TEXT("ScheduleReadOnly"),NULL, REG_DWORD,
  1645. (LPBYTE) &(lpConnectionSettings->dwReadOnly),
  1646. sizeof(lpConnectionSettings->dwReadOnly)))
  1647. {
  1648. if (ERROR_SUCCESS == RegCreateUserSubKey (hkeySchedName, lpConnectionSettings->pszConnectionName,
  1649. KEY_WRITE | KEY_READ, &hkeyConnection))
  1650. {
  1651. if (ERROR_SUCCESS == RegSetValueEx(hkeyConnection,TEXT("MakeAutoConnection"),NULL, REG_DWORD,
  1652. (LPBYTE) &(lpConnectionSettings->dwMakeConnection),
  1653. sizeof(lpConnectionSettings->dwMakeConnection)))
  1654. {
  1655. if (ERROR_SUCCESS == RegSetValueEx(hkeyConnection,TEXT("Connection Type"),NULL, REG_DWORD,
  1656. (LPBYTE) &(lpConnectionSettings->dwConnType),
  1657. sizeof(lpConnectionSettings->dwConnType)))
  1658. {
  1659. fRetVal = TRUE;
  1660. }
  1661. }
  1662. RegCloseKey(hkeyConnection);
  1663. }
  1664. }
  1665. }
  1666. RegCloseKey(hkeySchedName);
  1667. }
  1668. RegCloseKey(hKeyUser);
  1669. }
  1670. CMutexRegistry.Leave();
  1671. return fRetVal;
  1672. }
  1673. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1674. Function: RegGetSchedConnectionName(TCHAR *pszSchedName,
  1675. TCHAR *pszConnectionName,
  1676. DWORD cbConnectionName)
  1677. Summary: returns the Connection Name for the Scheduled Item.
  1678. Returns: Returns TRUE if successful, FALSE otherwise
  1679. ------------------------------------------------------------------------F-F*/
  1680. STDAPI_(BOOL) RegGetSchedConnectionName(TCHAR *pszSchedName,TCHAR *pszConnectionName,
  1681. DWORD cbConnectionName)
  1682. {
  1683. SCODE sc;
  1684. BOOL fResult = FALSE;
  1685. HKEY hKeyUser,hkeySchedName;
  1686. DWORD dwIndex = 0;
  1687. DWORD cb = cbConnectionName;
  1688. //First Set the connectionName to a default.
  1689. // for now we always assume a LAN card is present.
  1690. // if add support for connection manager should
  1691. // update this.
  1692. LoadString(g_hmodThisDll, IDS_LAN_CONNECTION, pszConnectionName, cbConnectionName);
  1693. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ,FALSE);
  1694. if (NULL == hKeyUser)
  1695. {
  1696. goto EH_Err;
  1697. }
  1698. smChkTo(EH_Err3, RegOpenKeyEx (hKeyUser,pszSchedName,
  1699. 0,KEY_READ, &hkeySchedName));
  1700. // next enumeration of keys is the names of the Schedules connection
  1701. // currently just have one so only get the first.
  1702. if ( ERROR_SUCCESS == RegEnumKeyEx(hkeySchedName,dwIndex,
  1703. pszConnectionName,&cb,NULL,NULL,NULL,NULL))
  1704. {
  1705. fResult = TRUE;
  1706. }
  1707. RegCloseKey(hkeySchedName);
  1708. RegCloseKey(hKeyUser);
  1709. return fResult;
  1710. EH_Err3:
  1711. RegCloseKey(hKeyUser);
  1712. EH_Err:
  1713. return FALSE;
  1714. }
  1715. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1716. Function: BOOL RegSetSIDForSchedule( TCHAR *pszSchedName)
  1717. Summary: sets the SID for this schedule
  1718. Returns: Returns TRUE if successful, FALSE otherwise
  1719. ------------------------------------------------------------------------F-F*/
  1720. STDAPI_(BOOL) RegSetSIDForSchedule(TCHAR *pszSchedName)
  1721. {
  1722. SCODE sc;
  1723. HKEY hkeySchedName;
  1724. TCHAR szSID[MAX_PATH];
  1725. DWORD dwType = REG_SZ;
  1726. if (!GetUserTextualSid(szSID, ARRAYSIZE(szSID)))
  1727. {
  1728. return FALSE;
  1729. }
  1730. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  1731. CMutexRegistry.Enter();
  1732. HKEY hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ | KEY_WRITE,TRUE);
  1733. if (NULL == hKeyUser)
  1734. {
  1735. goto EH_Err2;
  1736. }
  1737. smChkTo(EH_Err3,RegCreateUserSubKey (hKeyUser, pszSchedName,
  1738. KEY_WRITE | KEY_READ,
  1739. &hkeySchedName));
  1740. smChkTo(EH_Err4,RegSetValueEx (hkeySchedName,TEXT("SID"),NULL,
  1741. dwType,
  1742. (LPBYTE) szSID,
  1743. (lstrlen(szSID) + 1)*sizeof(TCHAR)));
  1744. RegCloseKey(hkeySchedName);
  1745. RegCloseKey(hKeyUser);
  1746. CMutexRegistry.Leave();
  1747. return TRUE;
  1748. EH_Err4:
  1749. RegCloseKey(hkeySchedName);
  1750. EH_Err3:
  1751. RegCloseKey(hKeyUser);
  1752. EH_Err2:
  1753. CMutexRegistry.Leave();
  1754. return FALSE;
  1755. }
  1756. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1757. Function: BOOL RegGetSIDForSchedule( TCHAR *ptszTextualSidSched,
  1758. DWORD *pcbSizeSid, TCHAR *pszSchedName)
  1759. Summary: returns the SID for this schedule
  1760. Returns: Returns TRUE if successful, FALSE otherwise
  1761. ------------------------------------------------------------------------F-F*/
  1762. STDAPI_(BOOL) RegGetSIDForSchedule(TCHAR *ptszTextualSidSched, DWORD cchTextualSidSched, TCHAR *pszSchedName)
  1763. {
  1764. SCODE sc;
  1765. HKEY hkeySchedName;
  1766. DWORD cbValue;
  1767. ptszTextualSidSched[0] = TEXT('\0');
  1768. HKEY hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ,FALSE);
  1769. if (NULL == hKeyUser)
  1770. {
  1771. goto EH_Err2;
  1772. }
  1773. smChkTo(EH_Err3, RegOpenKeyEx (hKeyUser,pszSchedName,
  1774. 0,KEY_READ, &hkeySchedName));
  1775. cbValue = cchTextualSidSched * sizeof(*ptszTextualSidSched);
  1776. if (ERROR_SUCCESS != (sc = SHRegGetValue(hkeySchedName,NULL,TEXT("SID"),SRRF_RT_REG_SZ | SRRF_NOEXPAND, NULL,
  1777. (LPBYTE) ptszTextualSidSched, &cbValue)))
  1778. {
  1779. //handle migration from schedules without SIDs
  1780. // like from Beta to current builds
  1781. RegSetSIDForSchedule(pszSchedName);
  1782. cbValue = cchTextualSidSched * sizeof(*ptszTextualSidSched);
  1783. SHRegGetValue(hkeySchedName,NULL,TEXT("SID"),SRRF_RT_REG_SZ | SRRF_NOEXPAND, NULL,
  1784. (LPBYTE) ptszTextualSidSched, &cbValue);
  1785. }
  1786. RegCloseKey(hkeySchedName);
  1787. RegCloseKey(hKeyUser);
  1788. return TRUE;
  1789. EH_Err3:
  1790. RegCloseKey(hKeyUser);
  1791. EH_Err2:
  1792. return FALSE;
  1793. }
  1794. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1795. BOOL RemoveScheduledJobFile(TCHAR *pszTaskName)
  1796. Summary: Remove the TaskScheduler .job file
  1797. Note: Try to use ITask->Delete first
  1798. Call only when TaskScheduler isn't present or if ITask->Delete failed
  1799. Returns: Returns TRUE always
  1800. ------------------------------------------------------------------------F-F*/
  1801. STDAPI_(BOOL) RemoveScheduledJobFile(TCHAR *pszTaskName)
  1802. {
  1803. SCODE sc;
  1804. TCHAR pszFullFileName[MAX_PATH+1];
  1805. TCHAR pszTaskFolderPath[MAX_PATH+1];
  1806. HKEY hkeyTaskSchedulerPath;
  1807. DWORD cbDataSize = sizeof(pszTaskFolderPath);
  1808. if (ERROR_SUCCESS == (sc = RegOpenKeyEx (HKEY_LOCAL_MACHINE,
  1809. TEXT("SOFTWARE\\Microsoft\\SchedulingAgent"),
  1810. NULL,KEY_READ,&hkeyTaskSchedulerPath)))
  1811. {
  1812. sc = SHRegGetValue(hkeyTaskSchedulerPath,NULL,TEXT("TasksFolder"),
  1813. SRRF_RT_REG_SZ | SRRF_NOEXPAND, NULL,
  1814. (LPBYTE) pszTaskFolderPath, &cbDataSize);
  1815. RegCloseKey(hkeyTaskSchedulerPath);
  1816. }
  1817. //If this get doesn't exist then bail.
  1818. if (ERROR_SUCCESS != sc)
  1819. {
  1820. return FALSE;
  1821. }
  1822. ExpandEnvironmentStrings(pszTaskFolderPath,pszFullFileName,MAX_PATH);
  1823. if (!PathAppend(pszFullFileName, pszTaskName))
  1824. {
  1825. return FALSE;
  1826. }
  1827. //if we fail this, ignore the error. We tried, there isn't much else we can do.
  1828. //So we have a turd file.
  1829. DeleteFile(pszFullFileName);
  1830. return TRUE;
  1831. }
  1832. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1833. BOOL RegRemoveScheduledTask(TCHAR *pszTaskName)
  1834. Summary: Remove the Scheduled task info from the registry.
  1835. Returns: Returns TRUE if successful, FALSE otherwise
  1836. ------------------------------------------------------------------------F-F*/
  1837. STDAPI_(BOOL) RegRemoveScheduledTask(TCHAR *pszTaskName)
  1838. {
  1839. HKEY hKeyUser;
  1840. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  1841. CMutexRegistry.Enter();
  1842. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ | KEY_WRITE,FALSE);
  1843. if (hKeyUser)
  1844. {
  1845. RegDeleteKeyNT(hKeyUser, pszTaskName);
  1846. RegCloseKey(hKeyUser);
  1847. }
  1848. CMutexRegistry.Leave();
  1849. return TRUE;
  1850. }
  1851. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1852. BOOL RegUninstallSchedules()
  1853. Summary: Uninstall the scheduled tasks.
  1854. Returns: Returns TRUE if successful, FALSE otherwise
  1855. ------------------------------------------------------------------------F-F*/
  1856. STDAPI_(BOOL) RegUninstallSchedules()
  1857. {
  1858. SCODE sc;
  1859. ITaskScheduler *pITaskScheduler = NULL;
  1860. HKEY hkeySchedSync;
  1861. // Obtain a task scheduler class instance.
  1862. sc = CoCreateInstance(
  1863. CLSID_CTaskScheduler,
  1864. NULL,
  1865. CLSCTX_INPROC_SERVER,
  1866. IID_ITaskScheduler,
  1867. (VOID **)&pITaskScheduler);
  1868. if (NOERROR != sc)
  1869. {
  1870. pITaskScheduler = NULL;
  1871. }
  1872. //now go through and delete the schedules
  1873. hkeySchedSync = RegGetSyncTypeKey(SYNCTYPE_SCHEDULED,KEY_READ,FALSE);
  1874. if (hkeySchedSync)
  1875. {
  1876. int iUserCount = 0;
  1877. while (sc == ERROR_SUCCESS )
  1878. {
  1879. HKEY hkeyDomainUser;
  1880. TCHAR szDomainUser[MAX_KEY_LENGTH];
  1881. SCODE sc2;
  1882. DWORD cchDomainUser = ARRAYSIZE(szDomainUser);
  1883. sc = RegEnumKeyEx(hkeySchedSync,iUserCount,szDomainUser,&cchDomainUser,NULL,NULL,NULL,NULL);
  1884. iUserCount++;
  1885. if(sc == ERROR_NO_MORE_ITEMS)
  1886. {
  1887. break;
  1888. }
  1889. sc2 = RegOpenKeyEx (hkeySchedSync,szDomainUser,0,KEY_READ, &hkeyDomainUser);
  1890. if (ERROR_SUCCESS == sc2)
  1891. {
  1892. int iScheduleCount = 0;
  1893. while (sc2 == ERROR_SUCCESS )
  1894. {
  1895. TCHAR ptszScheduleGUIDName[MAX_KEY_LENGTH];
  1896. //Add 4 to ensure that we can hold the .job extension if necessary
  1897. WCHAR pwszScheduleGUIDName[MAX_SCHEDULENAMESIZE + 4];
  1898. DWORD cchScheduleGUIDName = ARRAYSIZE(ptszScheduleGUIDName);
  1899. sc2 = RegEnumKeyEx(hkeyDomainUser,iScheduleCount,ptszScheduleGUIDName,&cchScheduleGUIDName,NULL,NULL,NULL,NULL);
  1900. iScheduleCount++;
  1901. if(sc2 == ERROR_NO_MORE_ITEMS)
  1902. {
  1903. continue;
  1904. }
  1905. else
  1906. {
  1907. Assert(ARRAYSIZE(pwszScheduleGUIDName) >= ARRAYSIZE(ptszScheduleGUIDName));
  1908. StringCchCopy(pwszScheduleGUIDName, ARRAYSIZE(pwszScheduleGUIDName), ptszScheduleGUIDName);
  1909. if ((!pITaskScheduler) ||
  1910. FAILED(pITaskScheduler->Delete(pwszScheduleGUIDName)))
  1911. {
  1912. if (SUCCEEDED(StringCchCat(pwszScheduleGUIDName, ARRAYSIZE(pwszScheduleGUIDName), L".job")))
  1913. {
  1914. RemoveScheduledJobFile(pwszScheduleGUIDName);
  1915. }
  1916. }
  1917. }
  1918. }
  1919. RegCloseKey(hkeyDomainUser);
  1920. }
  1921. }
  1922. RegCloseKey(hkeySchedSync);
  1923. }
  1924. if (pITaskScheduler)
  1925. {
  1926. pITaskScheduler->Release();
  1927. }
  1928. RegDeleteKeyNT(HKEY_LOCAL_MACHINE, SCHEDSYNC_REGKEY);
  1929. return TRUE;
  1930. }
  1931. /****************************************************************************
  1932. Idle Registry Functions
  1933. ***************************************************************************F-F*/
  1934. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1935. Function: RegGetIdleSyncSettings( LPCONNECTIONSETTINGS lpConnectionSettings)
  1936. Summary: Gets the Idle Specific settings.
  1937. Returns: Returns TRUE if successful, FALSE otherwise
  1938. ------------------------------------------------------------------------F-F*/
  1939. STDAPI_(BOOL) RegGetIdleSyncSettings(LPCONNECTIONSETTINGS lpConnectionSettings)
  1940. {
  1941. SCODE sc;
  1942. HKEY hkeyConnection;
  1943. DWORD cbDataSize;
  1944. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  1945. CMutexRegistry.Enter();
  1946. // set up defaults
  1947. lpConnectionSettings->dwIdleEnabled = 0;
  1948. // following are really per user not per connection
  1949. lpConnectionSettings->ulIdleWaitMinutes = UL_DEFAULTIDLEWAITMINUTES;
  1950. lpConnectionSettings->ulIdleRetryMinutes = UL_DEFAULTIDLERETRYMINUTES;
  1951. lpConnectionSettings->ulDelayIdleShutDownTime = UL_DELAYIDLESHUTDOWNTIME;
  1952. lpConnectionSettings->dwRepeatSynchronization = UL_DEFAULTREPEATSYNCHRONIZATION;
  1953. lpConnectionSettings->dwRunOnBatteries = UL_DEFAULTFRUNONBATTERIES;
  1954. HKEY hKeyUser = RegGetCurrentUserKey(SYNCTYPE_IDLE,KEY_READ,FALSE);
  1955. if (NULL == hKeyUser)
  1956. {
  1957. goto EH_Err2;
  1958. }
  1959. cbDataSize = sizeof(lpConnectionSettings->ulIdleWaitMinutes);
  1960. // if got the Idle key open then fill in global settings
  1961. SHRegGetValue(hKeyUser,NULL,SZ_IDLEWAITAFTERIDLEMINUTESKEY,SRRF_RT_REG_DWORD, NULL,
  1962. (LPBYTE) &(lpConnectionSettings->ulIdleWaitMinutes),
  1963. &cbDataSize);
  1964. cbDataSize = sizeof(lpConnectionSettings->dwRepeatSynchronization);
  1965. SHRegGetValue(hKeyUser,NULL,SZ_IDLEREPEATESYNCHRONIZATIONKEY,SRRF_RT_REG_DWORD, NULL,
  1966. (LPBYTE) &(lpConnectionSettings->dwRepeatSynchronization),
  1967. &cbDataSize);
  1968. cbDataSize = sizeof(lpConnectionSettings->ulIdleRetryMinutes);
  1969. SHRegGetValue(hKeyUser,NULL,SZ_IDLERETRYMINUTESKEY,SRRF_RT_REG_DWORD, NULL,
  1970. (LPBYTE) &(lpConnectionSettings->ulIdleRetryMinutes),
  1971. &cbDataSize);
  1972. cbDataSize = sizeof(lpConnectionSettings->ulDelayIdleShutDownTime);
  1973. SHRegGetValue(hKeyUser,NULL,SZ_IDLEDELAYSHUTDOWNTIMEKEY,SRRF_RT_REG_DWORD, NULL,
  1974. (LPBYTE) &(lpConnectionSettings->ulDelayIdleShutDownTime),
  1975. &cbDataSize);
  1976. cbDataSize = sizeof(lpConnectionSettings->dwRunOnBatteries);
  1977. SHRegGetValue(hKeyUser,NULL,SZ_IDLERUNONBATTERIESKEY,SRRF_RT_REG_DWORD, NULL,
  1978. (LPBYTE) &(lpConnectionSettings->dwRunOnBatteries),
  1979. &cbDataSize);
  1980. smChkTo(EH_Err3,RegOpenKeyEx(hKeyUser, lpConnectionSettings->pszConnectionName,0,KEY_READ,
  1981. &hkeyConnection));
  1982. cbDataSize = sizeof(lpConnectionSettings->dwIdleEnabled);
  1983. SHRegGetValue(hkeyConnection,NULL,TEXT("IdleEnabled"),SRRF_RT_REG_DWORD, NULL,
  1984. (LPBYTE) &(lpConnectionSettings->dwIdleEnabled),
  1985. &cbDataSize);
  1986. RegCloseKey(hkeyConnection);
  1987. RegCloseKey(hKeyUser);
  1988. CMutexRegistry.Leave();
  1989. return TRUE;
  1990. EH_Err3:
  1991. RegCloseKey(hKeyUser);
  1992. EH_Err2:
  1993. CMutexRegistry.Leave();
  1994. return FALSE;
  1995. }
  1996. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  1997. Function: RegSetIdleSyncSettings(LPCONNECTIONSETTINGS lpConnectionSettings,
  1998. int iNumConnections,
  1999. CRasUI *pRas,
  2000. BOOL fCleanReg,
  2001. BOOL fPerUser)
  2002. Summary: Sets the Idle Information user selections.
  2003. Returns: Returns TRUE if successful, FALSE otherwise
  2004. ------------------------------------------------------------------------F-F*/
  2005. STDAPI_(BOOL) RegSetIdleSyncSettings(LPCONNECTIONSETTINGS lpConnectionSettings,
  2006. int iNumConnections,
  2007. CRasUI *pRas,
  2008. BOOL fCleanReg,
  2009. BOOL fPerUser)
  2010. {
  2011. HKEY hkeyIdleSync = NULL;
  2012. HKEY hKeyUser;;
  2013. HKEY hkeyConnection;
  2014. HRESULT hr;
  2015. ULONG ulWaitMinutes = UL_DEFAULTWAITMINUTES;
  2016. DWORD dwIdleEnabled;
  2017. BOOL fRunOnBatteries = UL_DEFAULTFRUNONBATTERIES;
  2018. int i;
  2019. DWORD cbDataSize;
  2020. DWORD dwUserConfigured;
  2021. DWORD dwTopLevelDefaultValue = -1;
  2022. RegUpdateTopLevelKeys(); // make sure top-level keys are latest version
  2023. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  2024. CMutexRegistry.Enter();
  2025. Assert(-1 != TRUE); // we rely on TRUE boolean being something other than -1
  2026. hkeyIdleSync = RegGetSyncTypeKey(SYNCTYPE_IDLE,KEY_WRITE| KEY_READ,TRUE);
  2027. if (NULL == hkeyIdleSync)
  2028. {
  2029. goto EH_Err;
  2030. }
  2031. hKeyUser = RegOpenUserKey(hkeyIdleSync,KEY_WRITE| KEY_READ,TRUE,fCleanReg);
  2032. if (NULL == hKeyUser)
  2033. {
  2034. goto EH_Err2;
  2035. }
  2036. if (fPerUser)
  2037. {
  2038. dwUserConfigured = 1;
  2039. if (ERROR_SUCCESS != RegSetValueEx(hKeyUser,TEXT("UserConfigured"),NULL, REG_DWORD,
  2040. (LPBYTE) &dwUserConfigured,
  2041. sizeof(dwUserConfigured)))
  2042. {
  2043. goto EH_Err3;
  2044. }
  2045. }
  2046. else
  2047. {
  2048. cbDataSize = sizeof(dwUserConfigured);
  2049. //If this value isn't added yet,
  2050. if (ERROR_SUCCESS == SHRegGetValue(hKeyUser,NULL,TEXT("UserConfigured"),SRRF_RT_REG_DWORD, NULL,
  2051. (LPBYTE) &dwUserConfigured,
  2052. &cbDataSize))
  2053. {
  2054. //if the user setup their configuration, we won't override on a
  2055. //subsequent handler registration.
  2056. if (dwUserConfigured)
  2057. {
  2058. RegCloseKey(hkeyIdleSync);
  2059. RegCloseKey(hKeyUser);
  2060. CMutexRegistry.Leave();
  2061. return TRUE;
  2062. }
  2063. }
  2064. }
  2065. for (i=0; i<iNumConnections; i++)
  2066. {
  2067. if (ERROR_SUCCESS != RegCreateUserSubKey (hKeyUser,
  2068. lpConnectionSettings[i].pszConnectionName,
  2069. KEY_WRITE | KEY_READ,
  2070. &hkeyConnection))
  2071. {
  2072. goto EH_Err3;
  2073. }
  2074. hr = RegSetValueEx(hkeyConnection,TEXT("IdleEnabled"),NULL, REG_DWORD,
  2075. (LPBYTE) &(lpConnectionSettings[i].dwIdleEnabled),
  2076. sizeof(lpConnectionSettings[i].dwIdleEnabled));
  2077. if (lpConnectionSettings[i].dwIdleEnabled)
  2078. {
  2079. dwTopLevelDefaultValue = lpConnectionSettings[i].dwIdleEnabled;
  2080. }
  2081. RegCloseKey(hkeyConnection);
  2082. }
  2083. // write out the global idle information for Retry minutes and DelayIdleShutDown.
  2084. // then call function to reg/unregister with TS.
  2085. Assert(hkeyIdleSync); // should have already returned if this failed.
  2086. if (iNumConnections) // make sure at least one connection
  2087. {
  2088. // ONLY UPDATE SETTINGS IF NOT -1;
  2089. if (-1 != lpConnectionSettings[0].ulIdleRetryMinutes)
  2090. {
  2091. hr = RegSetValueEx(hKeyUser,SZ_IDLERETRYMINUTESKEY,NULL, REG_DWORD,
  2092. (LPBYTE) &(lpConnectionSettings[0].ulIdleRetryMinutes),
  2093. sizeof(lpConnectionSettings[0].ulIdleRetryMinutes));
  2094. }
  2095. if (-1 != lpConnectionSettings[0].ulDelayIdleShutDownTime)
  2096. {
  2097. hr = RegSetValueEx(hKeyUser,SZ_IDLEDELAYSHUTDOWNTIMEKEY,NULL, REG_DWORD,
  2098. (LPBYTE) &(lpConnectionSettings[0].ulDelayIdleShutDownTime),
  2099. sizeof(lpConnectionSettings[0].ulDelayIdleShutDownTime));
  2100. }
  2101. if (-1 != lpConnectionSettings[0].dwRepeatSynchronization)
  2102. {
  2103. hr = RegSetValueEx(hKeyUser,SZ_IDLEREPEATESYNCHRONIZATIONKEY,NULL, REG_DWORD,
  2104. (LPBYTE) &(lpConnectionSettings[0].dwRepeatSynchronization),
  2105. sizeof(lpConnectionSettings[0].dwRepeatSynchronization));
  2106. }
  2107. if (-1 != lpConnectionSettings[0].ulIdleWaitMinutes)
  2108. {
  2109. hr = RegSetValueEx(hKeyUser,SZ_IDLEWAITAFTERIDLEMINUTESKEY,NULL, REG_DWORD,
  2110. (LPBYTE) &(lpConnectionSettings[0].ulIdleWaitMinutes),
  2111. sizeof(lpConnectionSettings[0].ulIdleWaitMinutes));
  2112. }
  2113. if (-1 != lpConnectionSettings[0].dwRunOnBatteries)
  2114. {
  2115. hr = RegSetValueEx(hKeyUser,SZ_IDLERUNONBATTERIESKEY,NULL, REG_DWORD,
  2116. (LPBYTE) &(lpConnectionSettings[0].dwRunOnBatteries),
  2117. sizeof(lpConnectionSettings[0].dwRunOnBatteries));
  2118. }
  2119. ulWaitMinutes = lpConnectionSettings[0].ulIdleWaitMinutes;
  2120. fRunOnBatteries = lpConnectionSettings[0].dwRunOnBatteries;
  2121. // if -1 is passed in for ulWait or fRun on Batteries we need to
  2122. // get these and set them up so they can be passed onto Task Schedule
  2123. if (-1 == ulWaitMinutes)
  2124. {
  2125. cbDataSize = sizeof(ulWaitMinutes);
  2126. if (!(ERROR_SUCCESS == SHRegGetValue(hKeyUser,NULL,SZ_IDLEWAITAFTERIDLEMINUTESKEY,SRRF_RT_REG_DWORD, NULL,
  2127. (LPBYTE) &ulWaitMinutes,
  2128. &cbDataSize)) )
  2129. {
  2130. ulWaitMinutes = UL_DEFAULTIDLEWAITMINUTES;
  2131. }
  2132. }
  2133. if (-1 == fRunOnBatteries)
  2134. {
  2135. cbDataSize = sizeof(fRunOnBatteries);
  2136. if (!(ERROR_SUCCESS == SHRegGetValue(hKeyUser,NULL,SZ_IDLERUNONBATTERIESKEY,SRRF_RT_REG_DWORD, NULL,
  2137. (LPBYTE) &fRunOnBatteries,
  2138. &cbDataSize)) )
  2139. {
  2140. fRunOnBatteries = UL_DEFAULTFRUNONBATTERIES;
  2141. }
  2142. }
  2143. }
  2144. RegUpdateUserIdleKey(hKeyUser,TRUE /* fForce */); // set userlevel IdleFlags
  2145. // read in dwIdleEnabled key now that the UserKey is Updated.
  2146. cbDataSize = sizeof(dwIdleEnabled);
  2147. if (!(ERROR_SUCCESS == SHRegGetValue(hKeyUser,NULL,TEXT("IdleEnabled"),SRRF_RT_REG_DWORD, NULL,
  2148. (LPBYTE) &dwIdleEnabled,
  2149. &cbDataSize)) )
  2150. {
  2151. AssertSz(0,"Unable to query User IdleEnabledKey");
  2152. dwIdleEnabled = FALSE;
  2153. }
  2154. RegCloseKey(hKeyUser);
  2155. // update the toplevel IdleSyncInfo
  2156. RegUpdateIdleKeyValue(hkeyIdleSync,dwTopLevelDefaultValue);
  2157. RegCloseKey(hkeyIdleSync);
  2158. CMutexRegistry.Leave();
  2159. RegRegisterForIdleTrigger(dwIdleEnabled,ulWaitMinutes,fRunOnBatteries);
  2160. return TRUE;
  2161. EH_Err3:
  2162. RegCloseKey(hKeyUser);
  2163. EH_Err2:
  2164. RegCloseKey(hkeyIdleSync);
  2165. EH_Err:
  2166. CMutexRegistry.Leave();
  2167. return FALSE;
  2168. }
  2169. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2170. Function: RegRegisterForIdleTrigger()
  2171. Summary: Sets or removes the Idle trigger.
  2172. Returns: Returns TRUE if successful, FALSE otherwise
  2173. ------------------------------------------------------------------------F-F*/
  2174. STDAPI_(BOOL) RegRegisterForIdleTrigger(BOOL fRegister,ULONG ulWaitMinutes,BOOL fRunOnBatteries)
  2175. {
  2176. HRESULT hr;
  2177. CSyncMgrSynchronize *pSyncMgrSynchronize;
  2178. LPSYNCSCHEDULEMGR pScheduleMgr;
  2179. // Review - Currently the mobsync dll is registered as apartment
  2180. // and this function can be called from Logon/Logoff which is FreeThreaded
  2181. // Correct fix is to change DLL to be registered as BOTH but until then
  2182. // just create the class directly.
  2183. pSyncMgrSynchronize = new CSyncMgrSynchronize;
  2184. hr = E_OUTOFMEMORY;
  2185. if (pSyncMgrSynchronize)
  2186. {
  2187. hr = pSyncMgrSynchronize->QueryInterface(IID_ISyncScheduleMgr,(void **) &pScheduleMgr);
  2188. pSyncMgrSynchronize->Release();
  2189. }
  2190. if (NOERROR != hr)
  2191. {
  2192. return FALSE;
  2193. }
  2194. if (fRegister)
  2195. {
  2196. ISyncSchedule *pSyncSchedule = NULL;
  2197. SYNCSCHEDULECOOKIE SyncScheduleCookie;
  2198. BOOL fNewlyCreated = FALSE;
  2199. SyncScheduleCookie = GUID_IDLESCHEDULE;
  2200. // if there isn't an existing schedule create one, else update
  2201. // the existing.
  2202. if (HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) == (hr = pScheduleMgr->OpenSchedule(&SyncScheduleCookie,0,&pSyncSchedule)))
  2203. {
  2204. SyncScheduleCookie = GUID_IDLESCHEDULE;
  2205. // see if it is an exiting schedule.
  2206. hr = pScheduleMgr->CreateSchedule(L"Idle",0,&SyncScheduleCookie,&pSyncSchedule);
  2207. fNewlyCreated = TRUE;
  2208. }
  2209. // If created or found a schedule update the trigger settings.
  2210. if (NOERROR == hr)
  2211. {
  2212. ITaskTrigger *pTrigger;
  2213. pSyncSchedule->SetFlags(SYNCSCHEDINFO_FLAGS_READONLY | SYNCSCHEDINFO_FLAGS_HIDDEN);
  2214. if (NOERROR == pSyncSchedule->GetTrigger(&pTrigger))
  2215. {
  2216. TASK_TRIGGER trigger;
  2217. ITask *pTask;
  2218. trigger.cbTriggerSize = sizeof(trigger);
  2219. if (SUCCEEDED(pTrigger->GetTrigger(&trigger)))
  2220. {
  2221. DWORD dwFlags;
  2222. // need to set Idle, ULONG ulWaitMinutes,BOOL fRunOnBatteries
  2223. trigger.cbTriggerSize = sizeof(trigger);
  2224. trigger.TriggerType = TASK_EVENT_TRIGGER_ON_IDLE;
  2225. trigger.rgFlags = 0;
  2226. pTrigger->SetTrigger(&trigger);
  2227. if (SUCCEEDED(pSyncSchedule->GetITask(&pTask)))
  2228. {
  2229. // set up if run on battery.
  2230. if (SUCCEEDED(pTask->GetFlags(&dwFlags)))
  2231. {
  2232. dwFlags &= ~TASK_FLAG_DONT_START_IF_ON_BATTERIES;
  2233. dwFlags |= !fRunOnBatteries ? TASK_FLAG_DONT_START_IF_ON_BATTERIES : 0;
  2234. dwFlags |= TASK_FLAG_RUN_ONLY_IF_LOGGED_ON; // don't require password.
  2235. pTask->SetFlags(dwFlags);
  2236. }
  2237. // if this schedule was just created, get the current user name and reset
  2238. // account information password to NULL, If existing schedule don't change
  2239. // since user may have added a password for schedule to run while not logged on.
  2240. if (fNewlyCreated)
  2241. {
  2242. TCHAR szAccountName[MAX_DOMANDANDMACHINENAMESIZE];
  2243. WCHAR *pszAccountName = NULL;
  2244. // Review, this never returns an errorl
  2245. *szAccountName = WCHAR('\0');
  2246. GetDefaultDomainAndUserName(szAccountName,TEXT("\\"),ARRAYSIZE(szAccountName));
  2247. pszAccountName = szAccountName;
  2248. Assert(pszAccountName);
  2249. if (pszAccountName)
  2250. {
  2251. pTask->SetAccountInformation(pszAccountName,NULL);
  2252. }
  2253. }
  2254. // set up the IdleWaitTime.
  2255. pTask->SetIdleWait((WORD) ulWaitMinutes,1);
  2256. // turn off the option to kill task after xxx minutes.
  2257. pTask->SetMaxRunTime(INFINITE);
  2258. pTask->Release();
  2259. }
  2260. pTrigger->Release();
  2261. }
  2262. pSyncSchedule->Save();
  2263. }
  2264. pSyncSchedule->Release();
  2265. }
  2266. }
  2267. else
  2268. {
  2269. SYNCSCHEDULECOOKIE SyncScheduleCookie = GUID_IDLESCHEDULE;
  2270. // see if there is an existing schedule and if so remove it.
  2271. pScheduleMgr->RemoveSchedule(&SyncScheduleCookie);
  2272. }
  2273. pScheduleMgr->Release();
  2274. // set the temporary sens flags according so it can start on an idle trigger.
  2275. // not an error to not be able to get and set key since sens will
  2276. // be running anyways eventually.
  2277. HKEY hkeyAutoSync;
  2278. DWORD dwFlags = 0;
  2279. DWORD cbDataSize = sizeof(dwFlags);
  2280. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  2281. CMutexRegistry.Enter();
  2282. hkeyAutoSync = RegGetSyncTypeKey(SYNCTYPE_AUTOSYNC,KEY_WRITE | KEY_READ,TRUE);
  2283. if (NULL == hkeyAutoSync)
  2284. {
  2285. CMutexRegistry.Leave();
  2286. return FALSE;
  2287. }
  2288. if (ERROR_SUCCESS == SHRegGetValue(hkeyAutoSync,NULL,TEXT("Flags"),SRRF_RT_REG_DWORD, NULL,
  2289. (LPBYTE) &(dwFlags),&cbDataSize))
  2290. {
  2291. // Here we are setting Idle only so retail the other settings.
  2292. dwFlags &= ~AUTOSYNC_IDLE;
  2293. dwFlags |= (fRegister? AUTOSYNC_IDLE : 0);
  2294. RegSetValueEx(hkeyAutoSync,TEXT("Flags"),NULL, REG_DWORD,
  2295. (LPBYTE) &(dwFlags), sizeof(dwFlags));
  2296. }
  2297. RegCloseKey(hkeyAutoSync);
  2298. CMutexRegistry.Leave();
  2299. return TRUE;
  2300. }
  2301. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2302. BOOL RegGetSyncSettings(DWORD dwSyncType,LPCONNECTIONSETTINGS lpConnectionSettings)
  2303. Summary: Get ConnectionSettings appropriate to the synctype..
  2304. Returns: Returns TRUE if successful, FALSE otherwise
  2305. ------------------------------------------------------------------------F-F*/
  2306. STDAPI_(BOOL) RegGetSyncSettings(DWORD dwSyncType,LPCONNECTIONSETTINGS lpConnectionSettings)
  2307. {
  2308. switch(dwSyncType)
  2309. {
  2310. case SYNCTYPE_AUTOSYNC:
  2311. return RegGetAutoSyncSettings(lpConnectionSettings);
  2312. break;
  2313. case SYNCTYPE_IDLE:
  2314. return RegGetIdleSyncSettings(lpConnectionSettings);
  2315. break;
  2316. default:
  2317. AssertSz(0,"Unknown SyncType in RegGetSyncSettings");
  2318. break;
  2319. }
  2320. return FALSE;
  2321. }
  2322. /****************************************************************************
  2323. Manual Sync Registry Functions
  2324. ***************************************************************************F-F*/
  2325. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2326. BOOL RegRemoveManualSyncSettings(TCHAR *pszTaskName)
  2327. Summary: Remove the manual settings info from the registry.
  2328. Returns: Returns TRUE if successful, FALSE otherwise
  2329. ------------------------------------------------------------------------F-F*/
  2330. STDAPI_(BOOL) RegRemoveManualSyncSettings(TCHAR *pszConnectionName)
  2331. {
  2332. HKEY hkeyUser;
  2333. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  2334. CMutexRegistry.Enter();
  2335. hkeyUser = RegGetCurrentUserKey(SYNCTYPE_MANUAL,KEY_WRITE | KEY_READ,FALSE);
  2336. if (hkeyUser)
  2337. {
  2338. RegDeleteKeyNT(hkeyUser, pszConnectionName);
  2339. RegCloseKey(hkeyUser);
  2340. }
  2341. CMutexRegistry.Leave();
  2342. return TRUE;
  2343. }
  2344. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2345. BOOL RegWriteEvents(BOOL fWanLogon,BOOL fWanLogoff,BOOL fLanLogon,BOOL fLanLogoff)
  2346. Summary: Write out the Wan/Lan Logon/Logoff preferences fo SENS knows whether to invoke us.
  2347. Returns: Returns TRUE if successful, FALSE otherwise
  2348. ------------------------------------------------------------------------F-F*/
  2349. // Run key under HKLM
  2350. const WCHAR wszRunKey[] = TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
  2351. const WCHAR wszRunKeyCommandLine[] = TEXT("%SystemRoot%\\system32\\mobsync.exe /logon");
  2352. STDAPI_(BOOL) RegWriteEvents(BOOL Logon,BOOL Logoff)
  2353. {
  2354. HRESULT hr;
  2355. HKEY hkeyAutoSync;
  2356. DWORD dwFlags = 0;
  2357. DWORD dwType = REG_DWORD;
  2358. DWORD cbDataSize = sizeof(DWORD);
  2359. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  2360. CMutexRegistry.Enter();
  2361. hkeyAutoSync = RegGetSyncTypeKey(SYNCTYPE_AUTOSYNC,KEY_WRITE | KEY_READ,TRUE);
  2362. if (NULL == hkeyAutoSync)
  2363. {
  2364. CMutexRegistry.Leave();
  2365. return FALSE;
  2366. }
  2367. RegQueryValueEx(hkeyAutoSync,TEXT("Flags"),NULL, &dwType,
  2368. (LPBYTE) &(dwFlags),
  2369. &cbDataSize);
  2370. // Review, Shouldn't need to worry about schedule/idle once IsNetworkAlive
  2371. // is setup properly. Leave for now first time anyone sets idle or schedule
  2372. // stuck
  2373. // Here we are setting autosync only,
  2374. // so retain the registry settings for scheduled and idle.
  2375. dwFlags &= ~(AUTOSYNC_WAN_LOGON | AUTOSYNC_LAN_LOGON | AUTOSYNC_LOGONWITHRUNKEY
  2376. | AUTOSYNC_WAN_LOGOFF | AUTOSYNC_LAN_LOGOFF);
  2377. dwFlags |= (Logoff ? AUTOSYNC_WAN_LOGOFF : 0);
  2378. dwFlags |= (Logoff ? AUTOSYNC_LAN_LOGOFF : 0);
  2379. // Since now use Run key instead of SENS always set both Logon Flags
  2380. // that SENS looks for to do a CreateProcess on us to FALSE.
  2381. // Then set the AUTOSYNC_LOGONWITHRUNKEY key to true so sens still gets loaded.
  2382. dwFlags |= (Logon ? AUTOSYNC_LOGONWITHRUNKEY : 0);
  2383. hr = RegSetValueEx(hkeyAutoSync,TEXT("Flags"),NULL, REG_DWORD,
  2384. (LPBYTE) &(dwFlags), sizeof(DWORD));
  2385. RegCloseKey(hkeyAutoSync);
  2386. // now add /delete the run key appropriately.
  2387. HKEY hKeyRun;
  2388. // call private RegOpen since don't want to set security on RunKey
  2389. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, wszRunKey, NULL, KEY_READ | KEY_WRITE, &hKeyRun))
  2390. {
  2391. if (Logon)
  2392. {
  2393. RegSetValueEx(hKeyRun, SZ_SYNCMGRNAME, 0, REG_EXPAND_SZ,
  2394. (BYTE *) wszRunKeyCommandLine,(lstrlen(wszRunKeyCommandLine) + 1)*sizeof(TCHAR));
  2395. }
  2396. else
  2397. {
  2398. RegDeleteValue(hKeyRun, SZ_SYNCMGRNAME);
  2399. }
  2400. RegCloseKey(hKeyRun);
  2401. }
  2402. else
  2403. {
  2404. // if can't open run key try calling SENS if that fails give up.
  2405. SyncMgrExecCmd_UpdateRunKey(Logon);
  2406. }
  2407. CMutexRegistry.Leave();
  2408. return TRUE;
  2409. }
  2410. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2411. BOOL RegFixRunKey(BOOL fScheduled)
  2412. Summary: The original version of SyncMgr for WinMe/Win2000 wrote the
  2413. "run" value as "mobsync.exe /logon". Since this is not a fully-
  2414. qualified path to the mobsync.exe image, the system's search
  2415. path is utilized to locate the image. This can create an
  2416. opportunity for someone to build a 'trojan' mobsync.exe, place
  2417. it in the search path ahead of the real mobsync.exe and have
  2418. the 'trojan' code run whenever a synchronization is invoked.
  2419. To fix this, the path must be stored in the registry using
  2420. fully-qualified syntax.
  2421. i.e. "%SystemRoot%\System32\mobsync.exe /logon"
  2422. This function is called from DllRegisterServer to correct this
  2423. registry entry during setup.
  2424. Returns: Always returns TRUE.
  2425. ------------------------------------------------------------------------F-F*/
  2426. STDAPI_(BOOL) RegFixRunKey(void)
  2427. {
  2428. HKEY hKeyRun;
  2429. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  2430. wszRunKey,
  2431. NULL,
  2432. KEY_READ | KEY_WRITE,
  2433. &hKeyRun))
  2434. {
  2435. TCHAR szRunValue[MAX_PATH];
  2436. DWORD cbValue = sizeof(szRunValue);
  2437. if (ERROR_SUCCESS == SHRegGetValue(hKeyRun,NULL,SZ_SYNCMGRNAME, SRRF_RT_REG_SZ | SRRF_NOEXPAND, NULL,
  2438. (LPBYTE)szRunValue, &cbValue))
  2439. {
  2440. if (0 == lstrcmp(szRunValue, TEXT("mobsync.exe /logon")))
  2441. {
  2442. //
  2443. // Upgrade only if it's our original value.
  2444. //
  2445. RegSetValueEx(hKeyRun,
  2446. SZ_SYNCMGRNAME,
  2447. 0,
  2448. REG_EXPAND_SZ,
  2449. (BYTE *)wszRunKeyCommandLine,
  2450. (lstrlen(wszRunKeyCommandLine) + 1) * sizeof(TCHAR));
  2451. }
  2452. }
  2453. RegCloseKey(hKeyRun);
  2454. }
  2455. return TRUE;
  2456. }
  2457. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2458. BOOL RegRegisterForScheduledTasks(BOOL fScheduled)
  2459. Summary: Register/unregister for scheduled tasks
  2460. so SENS knows whether to invoke us.
  2461. Returns: Returns TRUE if successful, FALSE otherwise
  2462. ------------------------------------------------------------------------F-F*/
  2463. STDAPI_(BOOL) RegRegisterForScheduledTasks(BOOL fScheduled)
  2464. {
  2465. HKEY hkeyAutoSync;
  2466. DWORD dwFlags = 0;
  2467. DWORD cbDataSize = sizeof(dwFlags);
  2468. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  2469. CMutexRegistry.Enter();
  2470. hkeyAutoSync = RegGetSyncTypeKey(SYNCTYPE_AUTOSYNC,KEY_WRITE | KEY_READ,TRUE);
  2471. if (NULL == hkeyAutoSync)
  2472. {
  2473. CMutexRegistry.Leave();
  2474. return FALSE;
  2475. }
  2476. SHRegGetValue(hkeyAutoSync,NULL,TEXT("Flags"),SRRF_RT_REG_DWORD, NULL,
  2477. (LPBYTE) &(dwFlags), &cbDataSize);
  2478. // Here we are setting schedsync only,
  2479. // so retain the registry settings for autosync and idle.
  2480. dwFlags &= AUTOSYNC_WAN_LOGON |
  2481. AUTOSYNC_WAN_LOGOFF |
  2482. AUTOSYNC_LAN_LOGON |
  2483. AUTOSYNC_LAN_LOGOFF |
  2484. AUTOSYNC_IDLE;
  2485. dwFlags |= (fScheduled? AUTOSYNC_SCHEDULED : 0);
  2486. RegSetValueEx(hkeyAutoSync,TEXT("Flags"),NULL, REG_DWORD,
  2487. (LPBYTE) &(dwFlags), sizeof(dwFlags));
  2488. RegCloseKey(hkeyAutoSync);
  2489. CMutexRegistry.Leave();
  2490. return TRUE;
  2491. }
  2492. //+---------------------------------------------------------------------------
  2493. //
  2494. // Member: RegGetCombinedUserRegFlags, private
  2495. //
  2496. // Synopsis: Gets an or'ing together of user settings for setting up globals.
  2497. //
  2498. // Arguments: [dwSyncMgrRegisterFlags] - On Success gets set to flags
  2499. // on failure they are set to zero
  2500. //
  2501. // Returns: Appropriate status code
  2502. //
  2503. // Modifies:
  2504. //
  2505. // History: 24-Aug-98 rogerg Created.
  2506. //
  2507. //----------------------------------------------------------------------------
  2508. BOOL RegGetCombinedUserRegFlags(DWORD *pdwSyncMgrRegisterFlags)
  2509. {
  2510. HKEY hkey;
  2511. BOOL fResult = TRUE;
  2512. DWORD dw;
  2513. DWORD cb = sizeof(dw);
  2514. *pdwSyncMgrRegisterFlags = 0;
  2515. // update the AutoSync Key
  2516. hkey = RegGetSyncTypeKey(SYNCTYPE_AUTOSYNC,KEY_READ,FALSE);
  2517. if (hkey)
  2518. {
  2519. if (ERROR_SUCCESS == SHRegGetValue(hkey,NULL,TEXT("Logon"),SRRF_RT_REG_DWORD, NULL,
  2520. (LPBYTE) &dw, &cb) )
  2521. {
  2522. *pdwSyncMgrRegisterFlags |= dw ? SYNCMGRREGISTERFLAG_CONNECT : 0;
  2523. }
  2524. cb = sizeof(dw);
  2525. if (ERROR_SUCCESS == SHRegGetValue(hkey,NULL,TEXT("Logoff"),SRRF_RT_REG_DWORD, NULL,
  2526. (LPBYTE) &dw, &cb) )
  2527. {
  2528. *pdwSyncMgrRegisterFlags |= dw ? SYNCMGRREGISTERFLAG_PENDINGDISCONNECT : 0;
  2529. }
  2530. RegCloseKey(hkey);
  2531. }
  2532. // update the Idle Key
  2533. hkey = RegGetSyncTypeKey(SYNCTYPE_IDLE,KEY_READ,FALSE);
  2534. if (hkey)
  2535. {
  2536. cb = sizeof(dw);
  2537. if (ERROR_SUCCESS == SHRegGetValue(hkey,NULL,TEXT("IdleEnabled"),SRRF_RT_REG_DWORD, NULL,
  2538. (LPBYTE) &dw, &cb) )
  2539. {
  2540. *pdwSyncMgrRegisterFlags |= dw ? SYNCMGRREGISTERFLAG_IDLE : 0;
  2541. }
  2542. RegCloseKey(hkey);
  2543. }
  2544. return TRUE; // always return true but don't set flags on error.
  2545. }
  2546. //+---------------------------------------------------------------------------
  2547. //
  2548. // Member: RegGetCombinedHandlerRegFlags, private
  2549. //
  2550. // Synopsis: Gets an or'ing together of handler registration Keys
  2551. //
  2552. // Arguments: [dwSyncMgrRegisterFlags] - On Success gets set to flags
  2553. // on failure they are set to zero
  2554. // [ft] - On Success filed with timestamp
  2555. //
  2556. // Returns: Appropriate status code
  2557. //
  2558. // Modifies:
  2559. //
  2560. // History: 24-Aug-98 rogerg Created.
  2561. //
  2562. //----------------------------------------------------------------------------
  2563. BOOL RegGetCombinedHandlerRegFlags(DWORD *pdwSyncMgrRegisterFlags,FILETIME *pft)
  2564. {
  2565. HKEY hkey;
  2566. *pdwSyncMgrRegisterFlags = 0;
  2567. hkey = RegGetHandlerTopLevelKey(KEY_READ);
  2568. if (hkey)
  2569. {
  2570. DWORD dwRegistrationFlags;
  2571. DWORD cbDataSize = sizeof(dwRegistrationFlags);
  2572. if (ERROR_SUCCESS == SHRegGetValue(hkey,NULL,SZ_REGISTRATIONFLAGSKEY,SRRF_RT_REG_DWORD, NULL,
  2573. (LPBYTE) &dwRegistrationFlags,&cbDataSize) )
  2574. {
  2575. *pdwSyncMgrRegisterFlags = dwRegistrationFlags;
  2576. }
  2577. RegGetTimeStamp(hkey,pft);
  2578. RegCloseKey(hkey);
  2579. }
  2580. return TRUE; // always return true but don't set flags on error.
  2581. }
  2582. //+---------------------------------------------------------------------------
  2583. //
  2584. // Member: RegGetChangedHandlerFlags, private
  2585. //
  2586. // Synopsis: Gets an or'ing together of handler registration Keys
  2587. // that have changed since the given FILETIME
  2588. //
  2589. // Arguments: [pft] - Pointer to FileTime for Compare
  2590. // [pdwChangedFlags] - On Success filed with flags that channged
  2591. //
  2592. // Returns: TRUE if could gather flags.
  2593. //
  2594. // Modifies:
  2595. //
  2596. // History: 24-Aug-98 rogerg Created.
  2597. //
  2598. //----------------------------------------------------------------------------
  2599. BOOL RegGetChangedHandlerFlags(FILETIME *pft,DWORD *pdwHandlerChandedFlags)
  2600. {
  2601. HKEY hkeyHandler;
  2602. *pdwHandlerChandedFlags = 0;
  2603. hkeyHandler = RegGetHandlerTopLevelKey(KEY_READ);
  2604. if (hkeyHandler)
  2605. {
  2606. TCHAR lpName[MAX_PATH + 1];
  2607. DWORD dwRegistrationFlags = 0;
  2608. FILETIME ftHandlerReg;
  2609. DWORD dwIndex = 0;
  2610. LONG lRet;
  2611. HKEY hKeyClsid;
  2612. // enumerate the keys
  2613. while ( ERROR_SUCCESS == RegEnumKey(hkeyHandler,dwIndex,lpName,ARRAYSIZE(lpName)) )
  2614. {
  2615. lRet = RegOpenKeyEx( hkeyHandler,
  2616. lpName,
  2617. NULL,
  2618. KEY_READ,
  2619. &hKeyClsid );
  2620. if (ERROR_SUCCESS == lRet)
  2621. {
  2622. RegGetTimeStamp(hKeyClsid,&ftHandlerReg);
  2623. // handler reg is new time than our gvien time add it to the flags.
  2624. if (CompareFileTime(pft,&ftHandlerReg) < 0)
  2625. {
  2626. DWORD dwHandlerRegFlags;
  2627. DWORD cbDataSize = sizeof(dwHandlerRegFlags);
  2628. if (ERROR_SUCCESS == SHRegGetValue(hKeyClsid,NULL,SZ_REGISTRATIONFLAGSKEY,SRRF_RT_REG_DWORD, NULL,
  2629. (LPBYTE) &dwHandlerRegFlags,&cbDataSize) )
  2630. {
  2631. dwRegistrationFlags |= dwHandlerRegFlags;
  2632. }
  2633. }
  2634. RegCloseKey(hKeyClsid);
  2635. }
  2636. dwIndex++;
  2637. }
  2638. *pdwHandlerChandedFlags = dwRegistrationFlags;
  2639. RegCloseKey(hkeyHandler);
  2640. }
  2641. return TRUE;
  2642. }
  2643. //+---------------------------------------------------------------------------
  2644. //
  2645. // Member: RegRegisterForEvents, private
  2646. //
  2647. // Synopsis: Registers/UnRegisters for appropriate SENS and WinLogon Events.
  2648. // and any other per machine registration we need to do
  2649. //
  2650. // Arguments: [fUninstall] - set to true by uninstall to force us to unregister
  2651. // regardless of current machine state.
  2652. //
  2653. // Returns: Appropriate status code
  2654. //
  2655. // Modifies:
  2656. //
  2657. // History: 05-Nov-97 rogerg Created.
  2658. //
  2659. //----------------------------------------------------------------------------
  2660. // !!!Warning - Assumes toplevel key information is up to date.
  2661. STDAPI RegRegisterForEvents(BOOL fUninstall)
  2662. {
  2663. HRESULT hr = NOERROR;
  2664. BOOL fLogon = FALSE;
  2665. BOOL fLogoff = FALSE;
  2666. BOOL fIdle = FALSE;
  2667. #ifdef _SENS
  2668. IEventSystem *pEventSystem;
  2669. #endif // _SENS
  2670. CCriticalSection cCritSect(&g_DllCriticalSection,GetCurrentThreadId());
  2671. cCritSect.Enter();
  2672. if (!fUninstall)
  2673. {
  2674. FILETIME ftHandlerReg;
  2675. DWORD dwSyncMgrUsersRegisterFlags; // or'ing of all Users settings
  2676. DWORD dwSyncMgrHandlerRegisterFlags; // or'ing of all handler settings.
  2677. DWORD dwCombinedFlags; // or together user and handler.
  2678. // if not an uninstall determine the true machine state
  2679. // if Logon set for handler or user set or if handler
  2680. // wants an idle set.
  2681. // If Logoff set we register for Logoff.
  2682. RegGetCombinedUserRegFlags(&dwSyncMgrUsersRegisterFlags);
  2683. RegGetCombinedHandlerRegFlags(&dwSyncMgrHandlerRegisterFlags,&ftHandlerReg);
  2684. dwCombinedFlags = dwSyncMgrUsersRegisterFlags | dwSyncMgrHandlerRegisterFlags;
  2685. if ( (dwCombinedFlags & SYNCMGRREGISTERFLAG_CONNECT)
  2686. || (dwSyncMgrHandlerRegisterFlags & SYNCMGRREGISTERFLAG_IDLE) )
  2687. {
  2688. fLogon = TRUE;
  2689. }
  2690. if ( (dwCombinedFlags & SYNCMGRREGISTERFLAG_PENDINGDISCONNECT) )
  2691. {
  2692. fLogoff = TRUE;
  2693. }
  2694. }
  2695. // update Registry entries for SENS to lookup
  2696. RegWriteEvents(fLogon,fLogoff);
  2697. #ifdef _SENS
  2698. // we were able to load ole automation so reg/unreg with the event system.
  2699. hr = CoCreateInstance(CLSID_CEventSystem,NULL,CLSCTX_SERVER,IID_IEventSystem,
  2700. (LPVOID *) &pEventSystem);
  2701. if (SUCCEEDED(hr))
  2702. {
  2703. IEventSubscription *pIEventSubscription;
  2704. WCHAR szGuid[GUID_SIZE+1];
  2705. BSTR bstrSubscriberID = NULL;
  2706. BSTR bstrPROGID_EventSubscription = NULL;
  2707. bstrPROGID_EventSubscription = SysAllocString(PROGID_EventSubscription);
  2708. StringFromGUID2(GUID_SENSSUBSCRIBER_SYNCMGRP,szGuid, ARRAYSIZE(szGuid));
  2709. bstrSubscriberID = SysAllocString(szGuid);
  2710. if (bstrSubscriberID && bstrPROGID_EventSubscription)
  2711. {
  2712. // register for RasConnect
  2713. hr = CoCreateInstance(
  2714. CLSID_CEventSubscription,
  2715. NULL,
  2716. CLSCTX_SERVER,
  2717. IID_IEventSubscription,
  2718. (LPVOID *) &pIEventSubscription
  2719. );
  2720. }
  2721. else
  2722. {
  2723. hr = E_OUTOFMEMORY;
  2724. }
  2725. if (SUCCEEDED(hr))
  2726. {
  2727. BSTR bstrPublisherID = NULL;
  2728. BSTR bstrSubscriptionID = NULL;
  2729. BSTR bstrSubscriptionName = NULL;
  2730. BSTR bstrSubscriberCLSID = NULL;
  2731. BSTR bstrEventID = NULL;
  2732. BSTR bstrEventClassID = NULL;
  2733. BSTR bstrIID = NULL;
  2734. // if there are any events, register with ens for messages.
  2735. if (fLogon)
  2736. {
  2737. StringFromGUID2(GUID_SENSLOGONSUBSCRIPTION_SYNCMGRP,szGuid, ARRAYSIZE(szGuid));
  2738. bstrSubscriptionID = SysAllocString(szGuid);
  2739. if (bstrSubscriptionID && SUCCEEDED(hr))
  2740. {
  2741. hr = pIEventSubscription->put_SubscriptionID(bstrSubscriptionID);
  2742. }
  2743. StringFromGUID2(CLSID_SyncMgrp,szGuid, ARRAYSIZE(szGuid));
  2744. bstrSubscriberCLSID = SysAllocString(szGuid);
  2745. if (bstrSubscriberCLSID && SUCCEEDED(hr))
  2746. {
  2747. hr = pIEventSubscription->put_SubscriberCLSID(bstrSubscriberCLSID);
  2748. }
  2749. StringFromGUID2(SENSGUID_PUBLISHER,szGuid, ARRAYSIZE(szGuid));
  2750. bstrPublisherID = SysAllocString(szGuid);
  2751. if (bstrPublisherID && SUCCEEDED(hr))
  2752. {
  2753. hr = pIEventSubscription->put_PublisherID(bstrPublisherID);
  2754. }
  2755. bstrSubscriptionName = SysAllocString(SZ_SYNCMGRNAME);
  2756. if (bstrSubscriptionName && SUCCEEDED(hr))
  2757. {
  2758. hr = pIEventSubscription->put_SubscriptionName(bstrSubscriptionName);
  2759. }
  2760. bstrEventID = SysAllocString(L"ConnectionMade");
  2761. if (bstrEventID && SUCCEEDED(hr))
  2762. {
  2763. hr = pIEventSubscription->put_MethodName(bstrEventID);
  2764. }
  2765. StringFromGUID2(SENSGUID_EVENTCLASS_NETWORK,szGuid,ARRAYSIZE(szGuid));
  2766. bstrEventClassID = SysAllocString(szGuid);
  2767. if (bstrEventClassID && SUCCEEDED(hr))
  2768. {
  2769. hr = pIEventSubscription->put_EventClassID(bstrEventClassID);
  2770. }
  2771. // set this up for roaming
  2772. if (SUCCEEDED(hr))
  2773. {
  2774. // hr = pIEventSubscription->put_PerUser(TRUE); // don't register PerUser for Nw
  2775. }
  2776. StringFromGUID2(IID_ISensNetwork,szGuid,ARRAYSIZE(szGuid));
  2777. bstrIID = SysAllocString(szGuid);
  2778. if (bstrIID && SUCCEEDED(hr))
  2779. {
  2780. hr = pIEventSubscription->put_InterfaceID(bstrIID);
  2781. }
  2782. if (SUCCEEDED(hr))
  2783. {
  2784. hr = pEventSystem->Store(bstrPROGID_EventSubscription,pIEventSubscription);
  2785. }
  2786. if (bstrIID)
  2787. {
  2788. SysFreeString(bstrIID);
  2789. }
  2790. if (bstrPublisherID)
  2791. SysFreeString(bstrPublisherID);
  2792. if (bstrSubscriberCLSID)
  2793. SysFreeString(bstrSubscriberCLSID);
  2794. if (bstrEventClassID)
  2795. SysFreeString(bstrEventClassID);
  2796. if (bstrEventID)
  2797. SysFreeString(bstrEventID);
  2798. if (bstrSubscriptionID)
  2799. SysFreeString(bstrSubscriptionID);
  2800. if (bstrSubscriptionName)
  2801. SysFreeString(bstrSubscriptionName);
  2802. }
  2803. else // don't need to be registered, remove.
  2804. {
  2805. if (NOERROR == hr)
  2806. {
  2807. int errorIndex;
  2808. bstrSubscriptionID = SysAllocString(L"SubscriptionID={6295df30-35ee-11d1-8707-00C04FD93327}");
  2809. if (bstrSubscriptionID)
  2810. {
  2811. hr = pEventSystem->Remove(bstrPROGID_EventSubscription,bstrSubscriptionID /* QUERY */,&errorIndex);
  2812. SysFreeString(bstrSubscriptionID);
  2813. }
  2814. }
  2815. }
  2816. pIEventSubscription->Release();
  2817. }
  2818. if (bstrSubscriberID)
  2819. {
  2820. SysFreeString(bstrSubscriberID);
  2821. }
  2822. if (bstrPROGID_EventSubscription)
  2823. {
  2824. SysFreeString(bstrPROGID_EventSubscription);
  2825. }
  2826. pEventSystem->Release();
  2827. }
  2828. #endif // _SENS
  2829. cCritSect.Leave();
  2830. return hr;
  2831. }
  2832. // helper functions for handler registration
  2833. STDAPI_(BOOL) RegGetTimeStamp(HKEY hKey, FILETIME *pft)
  2834. {
  2835. FILETIME ft;
  2836. LONG lr;
  2837. DWORD cbSize = sizeof(ft);
  2838. Assert(pft);
  2839. lr = SHRegGetValue( hKey,NULL,
  2840. SZ_REGISTRATIONTIMESTAMPKEY,
  2841. SRRF_RT_REG_BINARY,
  2842. NULL,
  2843. (BYTE *)&ft,
  2844. &cbSize );
  2845. if ( lr == ERROR_SUCCESS )
  2846. {
  2847. Assert(cbSize == sizeof(FILETIME));
  2848. *pft = ft;
  2849. }
  2850. else
  2851. {
  2852. // set the filetime to way back when to
  2853. // any compares will just say older instead
  2854. // of having to check success code
  2855. (*pft).dwLowDateTime = 0;
  2856. (*pft).dwHighDateTime = 0;
  2857. }
  2858. return TRUE;
  2859. }
  2860. STDAPI_(BOOL) RegWriteTimeStamp(HKEY hkey)
  2861. {
  2862. SYSTEMTIME sysTime;
  2863. FILETIME ft;
  2864. LONG lr = -1;
  2865. GetSystemTime(&sysTime); // void can't check for errors
  2866. if (SystemTimeToFileTime(&sysTime,&ft) )
  2867. {
  2868. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  2869. CMutexRegistry.Enter();
  2870. // write out the UpdateTime
  2871. lr = RegSetValueEx( hkey,
  2872. SZ_REGISTRATIONTIMESTAMPKEY,
  2873. NULL,
  2874. REG_BINARY,
  2875. (BYTE *)&ft,
  2876. sizeof(ft) );
  2877. CMutexRegistry.Leave();
  2878. }
  2879. return (ERROR_SUCCESS == lr) ? TRUE : FALSE;
  2880. }
  2881. //+---------------------------------------------------------------------------
  2882. //
  2883. // Function: UpdateHandlerKeyInformation
  2884. //
  2885. // Synopsis: Updates the top-level handler key information
  2886. //
  2887. // Arguments:
  2888. //
  2889. // Returns: void
  2890. //
  2891. // Modifies: enumerates the handlers underneath the given key
  2892. // updating the registrationFlags which is an || or
  2893. // all registered handler flags and then updates the
  2894. // timestamp on this key
  2895. //
  2896. // History: 05-Nov-97 rogerg Created.
  2897. //
  2898. //----------------------------------------------------------------------------
  2899. void UpdateHandlerKeyInformation(HKEY hKeyHandler)
  2900. {
  2901. DWORD dwSyncMgrTopLevelRegisterFlags = 0;
  2902. DWORD dwIndex = 0;
  2903. TCHAR lpName[MAX_PATH];
  2904. while ( ERROR_SUCCESS == RegEnumKey(hKeyHandler,dwIndex,
  2905. lpName,ARRAYSIZE(lpName)) )
  2906. {
  2907. DWORD dwHandlerRegFlags;
  2908. DWORD cbDataSize = sizeof(dwHandlerRegFlags);
  2909. LONG lRet;
  2910. HKEY hKeyClsid;
  2911. lRet = RegOpenKeyEx( hKeyHandler,
  2912. lpName,
  2913. NULL,
  2914. KEY_READ,
  2915. &hKeyClsid );
  2916. if (ERROR_SUCCESS == lRet)
  2917. {
  2918. if (ERROR_SUCCESS == SHRegGetValue(hKeyClsid,NULL,SZ_REGISTRATIONFLAGSKEY,SRRF_RT_REG_DWORD, NULL,
  2919. (LPBYTE) &dwHandlerRegFlags,
  2920. &cbDataSize) )
  2921. {
  2922. dwSyncMgrTopLevelRegisterFlags |= dwHandlerRegFlags;
  2923. }
  2924. RegCloseKey(hKeyClsid);
  2925. }
  2926. dwIndex++;
  2927. }
  2928. // not much we can do if RegFlags are messed up other than assert and mask out
  2929. Assert(dwSyncMgrTopLevelRegisterFlags <= SYNCMGRREGISTERFLAGS_MASK);
  2930. dwSyncMgrTopLevelRegisterFlags &= SYNCMGRREGISTERFLAGS_MASK;
  2931. // write out new flags even if errors occured. work thing that happens is
  2932. // we don't set up someones autosync automatically.
  2933. RegSetValueEx(hKeyHandler,SZ_REGISTRATIONFLAGSKEY,NULL, REG_DWORD,
  2934. (LPBYTE) &(dwSyncMgrTopLevelRegisterFlags), sizeof(dwSyncMgrTopLevelRegisterFlags));
  2935. RegWriteTimeStamp(hKeyHandler);
  2936. }
  2937. //+---------------------------------------------------------------------------
  2938. //
  2939. // Function: RegUpdateTopLevelKeys
  2940. //
  2941. // Synopsis: Looks at toplevel AutoSync,Idle, etc. keys and determines
  2942. // if they need to be updated and if so goes for it.
  2943. //
  2944. // Arguments:
  2945. //
  2946. // Returns: Appropriate status code
  2947. //
  2948. // Modifies: set pfFirstRegistration out param to true if this is
  2949. // the first handler that has registered so we can setup defaults.
  2950. //
  2951. // History: 24-Aug-98 rogerg Created.
  2952. //
  2953. //----------------------------------------------------------------------------
  2954. STDAPI_(void) RegUpdateTopLevelKeys()
  2955. {
  2956. HKEY hkey;
  2957. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  2958. CMutexRegistry.Enter();
  2959. // update the AutoSync Key
  2960. hkey = RegGetSyncTypeKey(SYNCTYPE_AUTOSYNC,KEY_READ | KEY_WRITE,TRUE);
  2961. if (hkey)
  2962. {
  2963. DWORD dwUserLogonLogoff;
  2964. DWORD cbDataSize = sizeof(dwUserLogonLogoff);
  2965. // see if has a logon value and if it is either newly created or
  2966. // old format. Call Update to setthings up
  2967. if (ERROR_SUCCESS != SHRegGetValue(hkey,NULL,TEXT("Logon"),SRRF_RT_REG_DWORD, NULL,
  2968. (LPBYTE) &dwUserLogonLogoff,
  2969. &cbDataSize) )
  2970. {
  2971. RegUpdateAutoSyncKeyValue(hkey,-1,-1);
  2972. }
  2973. RegCloseKey(hkey);
  2974. }
  2975. // update the Idle Key
  2976. hkey = RegGetSyncTypeKey(SYNCTYPE_IDLE,KEY_READ | KEY_WRITE,TRUE);
  2977. if (hkey)
  2978. {
  2979. DWORD dwIdleEnabled;
  2980. DWORD cbDataSize = sizeof(dwIdleEnabled);
  2981. // see if has a Idle value and if it is either newly created or
  2982. // old format. Call Update to setthings up
  2983. if (ERROR_SUCCESS != SHRegGetValue(hkey,NULL,TEXT("IdleEnabled"),SRRF_RT_REG_DWORD, NULL,
  2984. (LPBYTE) &dwIdleEnabled, &cbDataSize) )
  2985. {
  2986. RegUpdateIdleKeyValue(hkey,-1);
  2987. }
  2988. RegCloseKey(hkey);
  2989. }
  2990. CMutexRegistry.Leave();
  2991. }
  2992. //+---------------------------------------------------------------------------
  2993. //
  2994. // Function: RegRegisterHandler
  2995. //
  2996. // Synopsis: Registers Handlers with SyncMgr.
  2997. //
  2998. // Arguments:
  2999. //
  3000. // Returns: Appropriate status code
  3001. //
  3002. // Modifies: set pfFirstRegistration out param to true if this is
  3003. // the first handler that has registered so we can setup defaults.
  3004. //
  3005. // History: 05-Nov-97 rogerg Created.
  3006. //
  3007. //----------------------------------------------------------------------------
  3008. STDAPI_(BOOL) RegRegisterHandler(REFCLSID rclsidHandler,
  3009. WCHAR const* pwszDescription,
  3010. DWORD dwSyncMgrRegisterFlags,
  3011. BOOL *pfFirstRegistration)
  3012. {
  3013. LONG lRet;
  3014. RegUpdateTopLevelKeys(); // make sure other top-level keys are up to date.
  3015. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  3016. CMutexRegistry.Enter();
  3017. *pfFirstRegistration = FALSE;
  3018. HKEY hKeyHandler;
  3019. hKeyHandler = RegGetHandlerTopLevelKey(KEY_READ | KEY_WRITE);
  3020. if (NULL == hKeyHandler)
  3021. {
  3022. CMutexRegistry.Leave();
  3023. return FALSE;
  3024. }
  3025. //
  3026. // Check if this is the first handler being registerd
  3027. //
  3028. TCHAR szGuid[GUID_SIZE+1];
  3029. DWORD cchGuid = ARRAYSIZE(szGuid);
  3030. lRet = RegEnumKeyEx( hKeyHandler,
  3031. 0,
  3032. szGuid,
  3033. &cchGuid,
  3034. NULL,
  3035. NULL,
  3036. NULL,
  3037. NULL );
  3038. if ( lRet != ERROR_SUCCESS )
  3039. *pfFirstRegistration = TRUE;
  3040. //
  3041. // Convert guid and description to TCHAR
  3042. //
  3043. TCHAR *pszDesc;
  3044. BOOL fOk = FALSE;
  3045. StringFromGUID2( rclsidHandler, szGuid, ARRAYSIZE(szGuid));
  3046. pszDesc = (TCHAR *)pwszDescription;
  3047. // write out the registration flags. If fail go ahead
  3048. // and succed registration anyways.
  3049. if (hKeyHandler)
  3050. {
  3051. HKEY hKeyClsid;
  3052. hKeyClsid = RegGetHandlerKey(hKeyHandler,szGuid,KEY_WRITE | KEY_READ,TRUE);
  3053. if (hKeyClsid)
  3054. {
  3055. fOk = TRUE; // if make handle key say registered okay
  3056. if (pszDesc)
  3057. {
  3058. RegSetValueEx(hKeyClsid,NULL,NULL, REG_SZ,
  3059. (LPBYTE) pszDesc,
  3060. (lstrlen(pszDesc) +1)*sizeof(TCHAR));
  3061. }
  3062. RegSetValueEx(hKeyClsid,SZ_REGISTRATIONFLAGSKEY,NULL, REG_DWORD,
  3063. (LPBYTE) &(dwSyncMgrRegisterFlags), sizeof(dwSyncMgrRegisterFlags));
  3064. // update the TimeStamp on the handler clsid
  3065. RegWriteTimeStamp(hKeyClsid);
  3066. RegCloseKey( hKeyClsid );
  3067. // update the toplevel key
  3068. UpdateHandlerKeyInformation(hKeyHandler);
  3069. }
  3070. }
  3071. // update the user information.
  3072. RegSetUserDefaults();
  3073. RegRegisterForEvents(FALSE /* fUninstall */);
  3074. CMutexRegistry.Leave();
  3075. RegCloseKey(hKeyHandler);
  3076. return fOk;
  3077. }
  3078. //+---------------------------------------------------------------------------
  3079. //
  3080. // Function: RegRegRemoveHandler
  3081. //
  3082. // Synopsis: UnRegisters Handlers with SyncMgr.
  3083. //
  3084. // Arguments:
  3085. //
  3086. // Returns: Appropriate status code
  3087. //
  3088. // Modifies: set pfAllHandlerUnRegistered out param to true if this is
  3089. // the last handler that needs to be unregistered before
  3090. // turning off our defaults..
  3091. //
  3092. // History: 05-Nov-97 rogerg Created.
  3093. //
  3094. //----------------------------------------------------------------------------
  3095. STDAPI_(BOOL) RegRegRemoveHandler(REFCLSID rclsidHandler)
  3096. {
  3097. HKEY hKeyHandler;
  3098. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  3099. CMutexRegistry.Enter();
  3100. hKeyHandler = RegGetHandlerTopLevelKey(KEY_WRITE | KEY_READ);
  3101. if (NULL == hKeyHandler)
  3102. {
  3103. //
  3104. // Non-existent key, so nothing to remove
  3105. //
  3106. CMutexRegistry.Leave();
  3107. return TRUE;
  3108. }
  3109. TCHAR szGuid[GUID_SIZE+1];
  3110. StringFromGUID2( rclsidHandler, szGuid, ARRAYSIZE(szGuid) );
  3111. HKEY hKeyClsid;
  3112. hKeyClsid = RegGetHandlerKey(hKeyHandler,szGuid,KEY_WRITE | KEY_READ,FALSE);
  3113. if (hKeyClsid)
  3114. {
  3115. RegCloseKey( hKeyClsid );
  3116. RegDeleteKey( hKeyHandler, szGuid );
  3117. // update the toplevel key
  3118. UpdateHandlerKeyInformation(hKeyHandler);
  3119. }
  3120. else
  3121. {
  3122. //
  3123. // Non-existent key, so nothing to remove
  3124. //
  3125. }
  3126. RegRegisterForEvents(FALSE /* fUninstall */); // UPDATE EVENT REGISTRATION.
  3127. CMutexRegistry.Leave();
  3128. RegCloseKey(hKeyHandler);
  3129. return TRUE;
  3130. }
  3131. //+---------------------------------------------------------------------------
  3132. //
  3133. // Function: RegGetHandlerRegistrationInfo
  3134. //
  3135. // Synopsis: Gets Information of the specified handler.
  3136. //
  3137. // Arguments:
  3138. //
  3139. // Returns: Appropriate status code
  3140. //
  3141. // Modifies: pdwSyncMgrRegisterFlags
  3142. //
  3143. // History: 20-Aug-98 rogerg Created.
  3144. //
  3145. //----------------------------------------------------------------------------
  3146. STDAPI_(BOOL) RegGetHandlerRegistrationInfo(REFCLSID rclsidHandler,LPDWORD pdwSyncMgrRegisterFlags)
  3147. {
  3148. HKEY hKeyHandler;
  3149. *pdwSyncMgrRegisterFlags = 0;
  3150. hKeyHandler = RegGetHandlerTopLevelKey(KEY_READ);
  3151. if (NULL == hKeyHandler)
  3152. {
  3153. //
  3154. // Non-existent key
  3155. //
  3156. return FALSE;
  3157. }
  3158. TCHAR szGuid[GUID_SIZE+1];
  3159. StringFromGUID2( rclsidHandler, szGuid, ARRAYSIZE(szGuid));
  3160. HKEY hKeyClsid;
  3161. BOOL fResult = FALSE;
  3162. hKeyClsid = RegGetHandlerKey(hKeyHandler,szGuid,KEY_READ,FALSE);
  3163. if (hKeyClsid)
  3164. {
  3165. DWORD cbDataSize = sizeof(*pdwSyncMgrRegisterFlags);
  3166. LONG lRet;
  3167. lRet = SHRegGetValue(hKeyClsid,NULL,SZ_REGISTRATIONFLAGSKEY,SRRF_RT_REG_DWORD, NULL,
  3168. (LPBYTE) pdwSyncMgrRegisterFlags,
  3169. &cbDataSize);
  3170. RegCloseKey( hKeyClsid );
  3171. fResult = (ERROR_SUCCESS == lRet) ? TRUE : FALSE;
  3172. }
  3173. else
  3174. {
  3175. //
  3176. // Non-existent key, so nothing to remove
  3177. //
  3178. }
  3179. RegCloseKey(hKeyHandler);
  3180. return fResult;
  3181. }
  3182. //+---------------------------------------------------------------------------
  3183. //
  3184. // Function: RegSetUserDefaults
  3185. //
  3186. // Synopsis: Registers default values for auto and idle sync
  3187. //
  3188. // Setup based on Handler and UserPreferences
  3189. //
  3190. // History: 20-May-98 SitaramR Created
  3191. //
  3192. //----------------------------------------------------------------------------
  3193. STDAPI_(void) RegSetUserDefaults()
  3194. {
  3195. HKEY hKeyUser = NULL;
  3196. FILETIME ftHandlerReg;
  3197. DWORD dwHandlerRegistrationFlags;
  3198. CMutex CMutexRegistry(NULL, FALSE,SZ_REGISTRYMUTEXNAME);
  3199. CMutexRegistry.Enter();
  3200. // get the combined handler registration toplevel flags and timeStamp
  3201. // to see if should bother enumerating the rest.
  3202. if (!RegGetCombinedHandlerRegFlags(&dwHandlerRegistrationFlags,&ftHandlerReg))
  3203. {
  3204. CMutexRegistry.Leave();
  3205. return;
  3206. }
  3207. if (0 != (dwHandlerRegistrationFlags &
  3208. (SYNCMGRREGISTERFLAG_CONNECT | SYNCMGRREGISTERFLAG_PENDINGDISCONNECT) ) )
  3209. {
  3210. // See if AutoSync key needs to be updated
  3211. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_AUTOSYNC,KEY_WRITE | KEY_READ,TRUE);
  3212. if (hKeyUser)
  3213. {
  3214. FILETIME ftUserAutoSync;
  3215. // if got the User get the timestamp and see if it is older than the handlers
  3216. // If this is a new user filetime will be 0
  3217. RegGetTimeStamp(hKeyUser,&ftUserAutoSync);
  3218. if (CompareFileTime(&ftUserAutoSync,&ftHandlerReg) < 0)
  3219. {
  3220. DWORD dwHandlerChangedFlags;
  3221. // need to walk through handlers and update what we need to set based
  3222. // on each handlers timestamp since we don't want a handler that registered
  3223. // for idle to cause us to turn AutoSync back on and vis-a-versa
  3224. if (RegGetChangedHandlerFlags(&ftUserAutoSync,&dwHandlerChangedFlags))
  3225. {
  3226. BOOL fLogon = (dwHandlerChangedFlags & SYNCMGRREGISTERFLAG_CONNECT) ? TRUE : FALSE;
  3227. BOOL fLogoff = (dwHandlerChangedFlags & SYNCMGRREGISTERFLAG_PENDINGDISCONNECT) ? TRUE : FALSE;
  3228. RegSetAutoSyncDefaults(fLogon,fLogoff);
  3229. }
  3230. }
  3231. RegCloseKey(hKeyUser);
  3232. hKeyUser = NULL;
  3233. }
  3234. }
  3235. if (0 != (dwHandlerRegistrationFlags & SYNCMGRREGISTERFLAG_IDLE ) )
  3236. {
  3237. // now check for Idle same logic as above could probably combine
  3238. // into a function
  3239. // See if AutoSync key needs to be updated
  3240. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_IDLE, KEY_WRITE | KEY_READ,TRUE);
  3241. if (hKeyUser)
  3242. {
  3243. FILETIME ftUserIdleSync;
  3244. // if got the User get the timestamp and see if it is older than the handlers
  3245. // If this is a new user filetime will be 0
  3246. RegGetTimeStamp(hKeyUser,&ftUserIdleSync);
  3247. if (CompareFileTime(&ftUserIdleSync,&ftHandlerReg) < 0)
  3248. {
  3249. DWORD dwHandlerChangedFlags;
  3250. // need to walk through handlers and update what we need to set based
  3251. // on each handlers timestamp since we don't want a handler that registered
  3252. // for AutoSync to cause us to turn Idle back on and vis-a-versa
  3253. if (RegGetChangedHandlerFlags(&ftUserIdleSync,&dwHandlerChangedFlags))
  3254. {
  3255. if (dwHandlerChangedFlags & SYNCMGRREGISTERFLAG_IDLE)
  3256. {
  3257. RegSetIdleSyncDefaults(TRUE);
  3258. }
  3259. }
  3260. }
  3261. RegCloseKey(hKeyUser);
  3262. hKeyUser = NULL;
  3263. }
  3264. }
  3265. CMutexRegistry.Leave();
  3266. }
  3267. //+---------------------------------------------------------------------------
  3268. //
  3269. // Function: RegSetAutoSyncDefaults
  3270. //
  3271. // Synopsis: Registers default values for auto sync
  3272. //
  3273. // History: 20-May-98 SitaramR Created
  3274. //
  3275. //----------------------------------------------------------------------------
  3276. STDAPI_(void) RegSetAutoSyncDefaults(BOOL fLogon,BOOL fLogoff)
  3277. {
  3278. CONNECTIONSETTINGS *pConnection = (LPCONNECTIONSETTINGS)
  3279. ALLOC(sizeof(*pConnection));
  3280. if ( pConnection == 0 )
  3281. return;
  3282. INT iRet = LoadString(g_hmodThisDll,
  3283. IDS_LAN_CONNECTION,
  3284. pConnection->pszConnectionName,
  3285. ARRAYSIZE(pConnection->pszConnectionName) );
  3286. Assert( iRet != 0 );
  3287. // -1 values are ignored by RegSetAutoSyncSettings.
  3288. // if not turning on leave the User Preferences alone,
  3289. pConnection->dwConnType = 0;
  3290. pConnection->dwLogon = fLogon ? TRUE : -1;
  3291. pConnection->dwLogoff = fLogoff ? TRUE : -1;
  3292. pConnection->dwPromptMeFirst = -1;
  3293. pConnection->dwMakeConnection = -1;
  3294. pConnection->dwIdleEnabled = -1;
  3295. // since this bases settings on what is already set no need to
  3296. // do a cleanreg or update the machine state
  3297. RegSetAutoSyncSettings(pConnection, 1, 0,
  3298. FALSE /* fCleanReg */,
  3299. FALSE /* fSetMachineState */,
  3300. FALSE /* fPerUser */);
  3301. FREE(pConnection);
  3302. }
  3303. //+---------------------------------------------------------------------------
  3304. //
  3305. // Function: RegSetUserAutoSyncDefaults
  3306. //
  3307. // Synopsis: Registers user default values for auto sync
  3308. //
  3309. // History: 39-March-99 rogerg Created
  3310. //
  3311. //----------------------------------------------------------------------------
  3312. STDAPI RegSetUserAutoSyncDefaults(DWORD dwSyncMgrRegisterMask,
  3313. DWORD dwSyncMgrRegisterFlags)
  3314. {
  3315. // if not changing either logon or logoff just return
  3316. if (!(dwSyncMgrRegisterMask & SYNCMGRREGISTERFLAG_CONNECT)
  3317. && !(dwSyncMgrRegisterMask & SYNCMGRREGISTERFLAG_PENDINGDISCONNECT) )
  3318. {
  3319. return NOERROR;
  3320. }
  3321. CONNECTIONSETTINGS *pConnection = (LPCONNECTIONSETTINGS)
  3322. ALLOC(sizeof(*pConnection));
  3323. if ( pConnection == 0 )
  3324. return E_OUTOFMEMORY;
  3325. INT iRet = LoadString(g_hmodThisDll,
  3326. IDS_LAN_CONNECTION,
  3327. pConnection->pszConnectionName,
  3328. ARRAYSIZE(pConnection->pszConnectionName) );
  3329. Assert( iRet != 0 );
  3330. // -1 values are ignored by RegSetAutoSyncSettings.
  3331. // if not turning on leave the User Preferences alone,
  3332. pConnection->dwConnType = 0;
  3333. pConnection->dwLogon = -1;
  3334. pConnection->dwLogoff = -1;
  3335. pConnection->dwPromptMeFirst = -1;
  3336. pConnection->dwMakeConnection = -1;
  3337. pConnection->dwIdleEnabled = -1;
  3338. if (dwSyncMgrRegisterMask & SYNCMGRREGISTERFLAG_CONNECT)
  3339. {
  3340. pConnection->dwLogon = (dwSyncMgrRegisterFlags & SYNCMGRREGISTERFLAG_CONNECT)
  3341. ? TRUE : FALSE;
  3342. }
  3343. if (dwSyncMgrRegisterMask & SYNCMGRREGISTERFLAG_PENDINGDISCONNECT)
  3344. {
  3345. pConnection->dwLogoff = (dwSyncMgrRegisterFlags & SYNCMGRREGISTERFLAG_PENDINGDISCONNECT)
  3346. ? TRUE : FALSE;
  3347. }
  3348. // since this bases settings on what is already set no need to
  3349. // do a cleanreg or update the machine state
  3350. RegSetAutoSyncSettings(pConnection, 1, 0,
  3351. FALSE /* fCleanReg */,
  3352. TRUE /* fSetMachineState */,
  3353. TRUE /* fPerUser */);
  3354. FREE(pConnection);
  3355. return NOERROR;
  3356. }
  3357. //+---------------------------------------------------------------------------
  3358. //
  3359. // Function: RegSetIdleSyncDefaults
  3360. //
  3361. // Synopsis: Registers default values for idle sync
  3362. //
  3363. // History: 20-May-98 SitaramR Created
  3364. //
  3365. //----------------------------------------------------------------------------
  3366. STDAPI_(void) RegSetIdleSyncDefaults(BOOL fIdle)
  3367. {
  3368. Assert(fIdle); // for now this should only be called when true;
  3369. if (!fIdle)
  3370. {
  3371. return;
  3372. }
  3373. CONNECTIONSETTINGS *pConnection = (LPCONNECTIONSETTINGS)
  3374. ALLOC(sizeof(*pConnection));
  3375. if ( pConnection == 0 )
  3376. return;
  3377. INT iRet = LoadString(g_hmodThisDll,
  3378. IDS_LAN_CONNECTION,
  3379. pConnection->pszConnectionName,
  3380. ARRAYSIZE(pConnection->pszConnectionName) );
  3381. Assert( iRet != 0 );
  3382. pConnection->dwConnType = 0;
  3383. pConnection->dwLogon = -1;
  3384. pConnection->dwLogoff = -1;
  3385. pConnection->dwPromptMeFirst = -1;
  3386. pConnection->dwMakeConnection = -1;
  3387. pConnection->dwIdleEnabled = TRUE;
  3388. // set all userLevel items to -1 so user gets the defaults if new
  3389. // but keep their settings if have already tweaked them.
  3390. pConnection->ulIdleRetryMinutes = -1;
  3391. pConnection->ulDelayIdleShutDownTime = -1;
  3392. pConnection->dwRepeatSynchronization = -1;
  3393. pConnection->ulIdleWaitMinutes = -1;
  3394. pConnection->dwRunOnBatteries = -1;
  3395. RegSetIdleSyncSettings(pConnection, 1, 0,
  3396. FALSE /* fCleanReg */,
  3397. FALSE /* fPerUser */);
  3398. FREE(pConnection);
  3399. }
  3400. //+---------------------------------------------------------------------------
  3401. //
  3402. // Function: RegSetIdleSyncDefaults
  3403. //
  3404. // Synopsis: Registers default values for idle sync
  3405. //
  3406. // History: 30-March-99 ROGERG Created
  3407. //
  3408. //----------------------------------------------------------------------------
  3409. STDAPI RegSetUserIdleSyncDefaults(DWORD dwSyncMgrRegisterMask,
  3410. DWORD dwSyncMgrRegisterFlags)
  3411. {
  3412. // RegSetIdleSyncSettings doesn't handle idle enabled of -1 so only
  3413. // call if Idle actually is set in the flags, if not just return
  3414. if (!(dwSyncMgrRegisterMask & SYNCMGRREGISTERFLAG_IDLE))
  3415. {
  3416. return NOERROR;
  3417. }
  3418. CONNECTIONSETTINGS *pConnection = (LPCONNECTIONSETTINGS)
  3419. ALLOC(sizeof(*pConnection));
  3420. if ( pConnection == 0 )
  3421. return E_OUTOFMEMORY;
  3422. INT iRet = LoadString(g_hmodThisDll,
  3423. IDS_LAN_CONNECTION,
  3424. pConnection->pszConnectionName,
  3425. ARRAYSIZE(pConnection->pszConnectionName) );
  3426. Assert( iRet != 0 );
  3427. pConnection->dwConnType = 0;
  3428. pConnection->dwLogon = -1;
  3429. pConnection->dwLogoff = -1;
  3430. pConnection->dwPromptMeFirst = -1;
  3431. pConnection->dwMakeConnection = -1;
  3432. pConnection->dwIdleEnabled = (SYNCMGRREGISTERFLAG_IDLE & dwSyncMgrRegisterFlags)
  3433. ? TRUE : FALSE;
  3434. // set all userLevel items to -1 so user gets the defaults if new
  3435. // but keep their settings if have already tweaked them.
  3436. pConnection->ulIdleRetryMinutes = -1;
  3437. pConnection->ulDelayIdleShutDownTime = -1;
  3438. pConnection->dwRepeatSynchronization = -1;
  3439. pConnection->ulIdleWaitMinutes = -1;
  3440. pConnection->dwRunOnBatteries = -1;
  3441. RegSetIdleSyncSettings(pConnection, 1, 0,
  3442. FALSE /* fCleanReg */,
  3443. TRUE /* fPerUser */);
  3444. FREE(pConnection);
  3445. return NOERROR;
  3446. }
  3447. //+---------------------------------------------------------------------------
  3448. //
  3449. // Function: RegGetUserRegisterFlags
  3450. //
  3451. // Synopsis: returns current registration flags for the User.
  3452. //
  3453. // History: 30-March-99 ROGERG Created
  3454. //
  3455. //----------------------------------------------------------------------------
  3456. STDAPI RegGetUserRegisterFlags(LPDWORD pdwSyncMgrRegisterFlags)
  3457. {
  3458. CONNECTIONSETTINGS connectSettings;
  3459. *pdwSyncMgrRegisterFlags = 0;
  3460. INT iRet = LoadString(g_hmodThisDll,
  3461. IDS_LAN_CONNECTION,
  3462. connectSettings.pszConnectionName,
  3463. ARRAYSIZE(connectSettings.pszConnectionName) );
  3464. if (0 == iRet)
  3465. {
  3466. Assert( iRet != 0 );
  3467. return E_UNEXPECTED;
  3468. }
  3469. RegGetSyncSettings(SYNCTYPE_AUTOSYNC,&connectSettings);
  3470. if (connectSettings.dwLogon)
  3471. {
  3472. *pdwSyncMgrRegisterFlags |= (SYNCMGRREGISTERFLAG_CONNECT);
  3473. }
  3474. if (connectSettings.dwLogoff)
  3475. {
  3476. *pdwSyncMgrRegisterFlags |= (SYNCMGRREGISTERFLAG_PENDINGDISCONNECT);
  3477. }
  3478. RegGetSyncSettings(SYNCTYPE_IDLE,&connectSettings);
  3479. if (connectSettings.dwIdleEnabled)
  3480. {
  3481. *pdwSyncMgrRegisterFlags |= (SYNCMGRREGISTERFLAG_IDLE);
  3482. }
  3483. return NOERROR;
  3484. }
  3485. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  3486. Function: BOOL RegSchedHandlerItemsChecked(TCHAR *pszHandlerName,
  3487. TCHAR *pszConnectionName,
  3488. TCHAR *pszScheduleName)
  3489. Summary: Determine if any items are checked on this handler for this schedule
  3490. Returns: Returns TRUE if one or more are checked, FALSE otherwise
  3491. ------------------------------------------------------------------------F-F*/
  3492. BOOL RegSchedHandlerItemsChecked(TCHAR *pszHandlerName,
  3493. TCHAR *pszConnectionName,
  3494. TCHAR *pszScheduleName)
  3495. {
  3496. SCODE sc;
  3497. HKEY hKeyUser,
  3498. hkeySchedName,
  3499. hkeyConnection,
  3500. hkeyHandler,
  3501. hkeyItem;
  3502. DWORD cbName = MAX_PATH,
  3503. dwIndex = 0,
  3504. dwCheckState = 0;
  3505. BOOL fItemsChecked = FALSE;
  3506. TCHAR lpName[MAX_PATH + 1];
  3507. hKeyUser = RegGetCurrentUserKey(SYNCTYPE_SCHEDULED,KEY_READ,FALSE);
  3508. if (NULL == hKeyUser)
  3509. {
  3510. return FALSE;
  3511. }
  3512. smChkTo(EH_Err3,RegOpenKeyEx (hKeyUser,
  3513. pszScheduleName,0,KEY_READ,
  3514. &hkeySchedName));
  3515. smChkTo(EH_Err4,RegOpenKeyEx (hkeySchedName,
  3516. pszConnectionName,
  3517. 0,KEY_READ,
  3518. &hkeyConnection));
  3519. smChkTo(EH_Err5,RegOpenKeyEx (hkeyConnection,
  3520. pszHandlerName,
  3521. 0,KEY_READ,
  3522. &hkeyHandler));
  3523. // need to enum handler items.
  3524. while ( ERROR_SUCCESS == RegEnumKey(hkeyHandler,dwIndex,
  3525. lpName,cbName) )
  3526. {
  3527. LONG lRet;
  3528. lRet = RegOpenKeyEx( hkeyHandler,
  3529. lpName,
  3530. NULL,
  3531. KEY_READ,
  3532. &hkeyItem);
  3533. if (ERROR_SUCCESS == lRet)
  3534. {
  3535. DWORD cbDataSize = sizeof(dwCheckState);
  3536. SHRegGetValue(hkeyItem,NULL,TEXT("CheckState"), SRRF_RT_REG_DWORD, NULL,
  3537. (LPBYTE) &dwCheckState, &cbDataSize);
  3538. RegCloseKey(hkeyItem);
  3539. }
  3540. else
  3541. {
  3542. goto EH_Err5;
  3543. }
  3544. if (dwCheckState)
  3545. {
  3546. fItemsChecked = TRUE;
  3547. break;
  3548. }
  3549. dwIndex++;
  3550. }
  3551. RegCloseKey(hkeyHandler);
  3552. RegCloseKey(hkeyConnection);
  3553. RegCloseKey(hkeySchedName);
  3554. RegCloseKey(hKeyUser);
  3555. return fItemsChecked;
  3556. EH_Err5:
  3557. RegCloseKey(hkeyConnection);
  3558. EH_Err4:
  3559. RegCloseKey(hkeySchedName);
  3560. EH_Err3:
  3561. RegCloseKey(hKeyUser);
  3562. return fItemsChecked;
  3563. }