Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
5.2 KiB

  1. #include "pch.h"
  2. #pragma hdrstop
  3. #include <mobsyncp.h>
  4. //*************************************************************
  5. //
  6. // RegisterSyncMgrHandler
  7. //
  8. // Purpose: Register/unregister CSC Update handler with SyncMgr
  9. //
  10. // Parameters: bRegister - TRUE to register, FALSE to unregister
  11. // punkSyncMgr - (optional) instance of SyncMgr to use
  12. //
  13. // Return: HRESULT
  14. //
  15. //*************************************************************
  16. HRESULT
  17. RegisterSyncMgrHandler(
  18. BOOL bRegister,
  19. LPUNKNOWN punkSyncMgr
  20. )
  21. {
  22. HRESULT hr;
  23. HRESULT hrComInit = E_FAIL;
  24. ISyncMgrRegister *pSyncRegister = NULL;
  25. const DWORD dwRegFlags = SYNCMGRREGISTERFLAG_CONNECT | SYNCMGRREGISTERFLAG_PENDINGDISCONNECT;
  26. TraceEnter(TRACE_UPDATE, "CscRegisterHandler");
  27. if (punkSyncMgr)
  28. {
  29. hr = punkSyncMgr->QueryInterface(IID_ISyncMgrRegister, (LPVOID*)&pSyncRegister);
  30. }
  31. else
  32. {
  33. hrComInit = CoInitialize(NULL);
  34. hr = CoCreateInstance(CLSID_SyncMgr,
  35. NULL,
  36. CLSCTX_SERVER,
  37. IID_ISyncMgrRegister,
  38. (LPVOID*)&pSyncRegister);
  39. }
  40. FailGracefully(hr, "Unable to get ISyncMgrRegister interface");
  41. if (bRegister)
  42. hr = pSyncRegister->RegisterSyncMgrHandler(CLSID_CscUpdateHandler, NULL, dwRegFlags);
  43. else
  44. hr = pSyncRegister->UnregisterSyncMgrHandler(CLSID_CscUpdateHandler, dwRegFlags);
  45. exit_gracefully:
  46. DoRelease(pSyncRegister);
  47. if (SUCCEEDED(hrComInit))
  48. CoUninitialize();
  49. TraceLeaveResult(hr);
  50. }
  51. //
  52. // Set/Clear the sync-at-logon-logoff flags for our SyncMgr handler.
  53. // When set, SyncMgr will include Offline Files in any sync activity
  54. // at logon and/or logoff.
  55. //
  56. // dwFlagsRequested - Value of flags bits. 1 == set, 0 == clear.
  57. // dwMask - Mask describing which flags bits to use.
  58. //
  59. // Both dwMask and dwFlagsRequested may be one of the following:
  60. //
  61. // 0
  62. // SYNCMGRREGISTER_CONNECT
  63. // SYNCMGRREGISTER_PENDINGDISCONNECT
  64. // SYNCMGRREGISTER_CONNECT | SYNCMGRREGISTER_PENDINGDISCONNECT
  65. //
  66. HRESULT
  67. RegisterForSyncAtLogonAndLogoff(
  68. DWORD dwMask,
  69. DWORD dwFlagsRequested
  70. )
  71. {
  72. CCoInit coinit;
  73. HRESULT hr = coinit.Result();
  74. if (SUCCEEDED(hr))
  75. {
  76. ISyncMgrRegisterCSC *pSyncRegister = NULL;
  77. hr = CoCreateInstance(CLSID_SyncMgr,
  78. NULL,
  79. CLSCTX_SERVER,
  80. IID_ISyncMgrRegisterCSC,
  81. (LPVOID*)&pSyncRegister);
  82. if (SUCCEEDED(hr))
  83. {
  84. //
  85. // Re-register the sync mgr handler with the "connect" and "disconnect"
  86. // flags set. Other existing flags are left unmodified.
  87. //
  88. DWORD dwFlagsActual;
  89. hr = pSyncRegister->GetUserRegisterFlags(&dwFlagsActual);
  90. if (SUCCEEDED(hr))
  91. {
  92. const DWORD LOGON = SYNCMGRREGISTERFLAG_CONNECT;
  93. const DWORD LOGOFF = SYNCMGRREGISTERFLAG_PENDINGDISCONNECT;
  94. if (dwMask & LOGON)
  95. {
  96. if (dwFlagsRequested & LOGON)
  97. dwFlagsActual |= LOGON;
  98. else
  99. dwFlagsActual &= ~LOGON;
  100. }
  101. if (dwMask & LOGOFF)
  102. {
  103. if (dwFlagsRequested & LOGOFF)
  104. dwFlagsActual |= LOGOFF;
  105. else
  106. dwFlagsActual &= ~LOGOFF;
  107. }
  108. hr = pSyncRegister->SetUserRegisterFlags(dwMask & (LOGON | LOGOFF),
  109. dwFlagsActual);
  110. }
  111. pSyncRegister->Release();
  112. }
  113. }
  114. return hr;
  115. }
  116. //
  117. // Determine if we're registered for sync at logon/logoff.
  118. // Returns:
  119. // S_OK = We're registered. Query *pbLogon and *pbLogoff to
  120. // determine specifics if you're interested.
  121. // S_FALSE = We're not registered.
  122. // Other = Couldn't determine because of some error.
  123. //
  124. HRESULT
  125. IsRegisteredForSyncAtLogonAndLogoff(
  126. bool *pbLogon,
  127. bool *pbLogoff
  128. )
  129. {
  130. bool bLogon = false;
  131. bool bLogoff = false;
  132. CCoInit coinit;
  133. HRESULT hr = coinit.Result();
  134. if (SUCCEEDED(hr))
  135. {
  136. ISyncMgrRegisterCSC *pSyncRegister = NULL;
  137. hr = CoCreateInstance(CLSID_SyncMgr,
  138. NULL,
  139. CLSCTX_SERVER,
  140. IID_ISyncMgrRegisterCSC,
  141. (LPVOID*)&pSyncRegister);
  142. if (SUCCEEDED(hr))
  143. {
  144. DWORD dwFlags;
  145. hr = pSyncRegister->GetUserRegisterFlags(&dwFlags);
  146. if (SUCCEEDED(hr))
  147. {
  148. hr = S_FALSE;
  149. bLogon = (0 != (SYNCMGRREGISTERFLAG_CONNECT & dwFlags));
  150. bLogoff = (0 != (SYNCMGRREGISTERFLAG_PENDINGDISCONNECT & dwFlags));
  151. if (bLogon || bLogoff)
  152. hr = S_OK;
  153. }
  154. pSyncRegister->Release();
  155. }
  156. }
  157. if (NULL != pbLogon)
  158. *pbLogon = bLogon;
  159. if (NULL != pbLogoff)
  160. *pbLogoff = bLogoff;
  161. return hr;
  162. }