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.

262 lines
5.7 KiB

  1. //
  2. // computil.cpp
  3. //
  4. #include "private.h"
  5. #include "gcomp.h"
  6. #include "helpers.h"
  7. extern "C" HRESULT WINAPI TF_GetGlobalCompartment(ITfCompartmentMgr **pCompMgr);
  8. //+---------------------------------------------------------------------------
  9. //
  10. // GetCompartment
  11. //
  12. //----------------------------------------------------------------------------
  13. HRESULT GetGlobalCompartment(REFGUID rguidComp, ITfCompartment **ppComp)
  14. {
  15. HRESULT hr = E_FAIL;
  16. ITfCompartmentMgr *pCompMgr = NULL;
  17. *ppComp = NULL;
  18. if (FAILED(hr = TF_GetGlobalCompartment(&pCompMgr)))
  19. {
  20. Assert(0);
  21. goto Exit;
  22. }
  23. if (SUCCEEDED(hr) && pCompMgr)
  24. {
  25. hr = pCompMgr->GetCompartment(rguidComp, ppComp);
  26. pCompMgr->Release();
  27. }
  28. else
  29. hr = E_FAIL;
  30. Exit:
  31. return hr;
  32. }
  33. //+---------------------------------------------------------------------------
  34. //
  35. // SetCompartmentDWORD
  36. //
  37. //----------------------------------------------------------------------------
  38. HRESULT SetGlobalCompartmentDWORD(REFGUID rguidComp, DWORD dw)
  39. {
  40. HRESULT hr;
  41. ITfCompartment *pComp;
  42. VARIANT var;
  43. if (SUCCEEDED(hr = GetGlobalCompartment(rguidComp, &pComp)))
  44. {
  45. var.vt = VT_I4;
  46. var.lVal = dw;
  47. hr = pComp->SetValue(0, &var);
  48. pComp->Release();
  49. }
  50. return hr;
  51. }
  52. //+---------------------------------------------------------------------------
  53. //
  54. // GetGlobalCompartmentDWORD
  55. //
  56. //----------------------------------------------------------------------------
  57. HRESULT GetGlobalCompartmentDWORD(REFGUID rguidComp, DWORD *pdw)
  58. {
  59. HRESULT hr;
  60. ITfCompartment *pComp;
  61. VARIANT var;
  62. *pdw = 0;
  63. if (SUCCEEDED(hr = GetGlobalCompartment(rguidComp, &pComp)))
  64. {
  65. if ((hr = pComp->GetValue(&var)) == S_OK)
  66. {
  67. Assert(var.vt == VT_I4);
  68. *pdw = var.lVal;
  69. }
  70. pComp->Release();
  71. }
  72. return hr;
  73. }
  74. //////////////////////////////////////////////////////////////////////////////
  75. //
  76. // CGlobalCompartmentEventSink
  77. //
  78. //////////////////////////////////////////////////////////////////////////////
  79. //+---------------------------------------------------------------------------
  80. //
  81. // IUnknown
  82. //
  83. //----------------------------------------------------------------------------
  84. STDAPI CGlobalCompartmentEventSink::QueryInterface(REFIID riid, void **ppvObj)
  85. {
  86. *ppvObj = NULL;
  87. if (IsEqualIID(riid, IID_IUnknown) ||
  88. IsEqualIID(riid, IID_ITfCompartmentEventSink))
  89. {
  90. *ppvObj = SAFECAST(this, ITfCompartmentEventSink *);
  91. }
  92. if (*ppvObj)
  93. {
  94. AddRef();
  95. return S_OK;
  96. }
  97. Assert(0);
  98. return E_NOINTERFACE;
  99. }
  100. STDAPI_(ULONG) CGlobalCompartmentEventSink::AddRef()
  101. {
  102. return ++_cRef;
  103. }
  104. STDAPI_(ULONG) CGlobalCompartmentEventSink::Release()
  105. {
  106. long cr;
  107. cr = --_cRef;
  108. Assert(cr >= 0);
  109. if (cr == 0)
  110. {
  111. delete this;
  112. }
  113. return cr;
  114. }
  115. //+---------------------------------------------------------------------------
  116. //
  117. // ctor
  118. //
  119. //----------------------------------------------------------------------------
  120. CGlobalCompartmentEventSink::CGlobalCompartmentEventSink(CESCALLBACK pfnCallback, void *pv)
  121. {
  122. Dbg_MemSetThisName(TEXT("CGlobalCompartmentEventSink"));
  123. _cRef = 1;
  124. _pfnCallback = pfnCallback;
  125. _pv = pv;
  126. }
  127. //+---------------------------------------------------------------------------
  128. //
  129. // OnChange
  130. //
  131. //----------------------------------------------------------------------------
  132. STDAPI CGlobalCompartmentEventSink::OnChange(REFGUID rguid)
  133. {
  134. return _pfnCallback(_pv, rguid);
  135. }
  136. //+---------------------------------------------------------------------------
  137. //
  138. // CGlobalCompartmentEventSink::Advise
  139. //
  140. //----------------------------------------------------------------------------
  141. HRESULT CGlobalCompartmentEventSink::_Advise(REFGUID rguidComp)
  142. {
  143. HRESULT hr;
  144. ITfSource *pSource = NULL;
  145. int nCnt;
  146. CESMAP *pcesmap;
  147. nCnt = _rgcesmap.Count();
  148. if (!_rgcesmap.Insert(nCnt, 1))
  149. return E_OUTOFMEMORY;
  150. pcesmap = _rgcesmap.GetPtr(nCnt);
  151. memset(pcesmap, 0, sizeof(CESMAP));
  152. hr = E_FAIL;
  153. if (FAILED(hr = GetGlobalCompartment(rguidComp, &pcesmap->pComp)))
  154. {
  155. Assert(0);
  156. goto Exit;
  157. }
  158. if (FAILED(hr = pcesmap->pComp->QueryInterface(IID_ITfSource, (void **)&pSource)))
  159. {
  160. Assert(0);
  161. goto Exit;
  162. }
  163. if (FAILED(hr = pSource->AdviseSink(IID_ITfCompartmentEventSink, (ITfCompartmentEventSink *)this, &pcesmap->dwCookie)))
  164. {
  165. Assert(0);
  166. goto Exit;
  167. }
  168. hr = S_OK;
  169. Exit:
  170. if (FAILED(hr))
  171. {
  172. Assert(0);
  173. SafeReleaseClear(pcesmap->pComp);
  174. _rgcesmap.Remove(nCnt, 1);
  175. }
  176. SafeRelease(pSource);
  177. return hr;
  178. }
  179. //+---------------------------------------------------------------------------
  180. //
  181. // CGlobalCompartmentEventSink::Unadvise
  182. //
  183. //----------------------------------------------------------------------------
  184. HRESULT CGlobalCompartmentEventSink::_Unadvise()
  185. {
  186. HRESULT hr;
  187. int nCnt;
  188. CESMAP *pcesmap;
  189. hr = E_FAIL;
  190. nCnt = _rgcesmap.Count();
  191. pcesmap = _rgcesmap.GetPtr(0);
  192. while (nCnt)
  193. {
  194. ITfSource *pSource = NULL;
  195. if (FAILED(pcesmap->pComp->QueryInterface(IID_ITfSource, (void **)&pSource)))
  196. goto Next;
  197. if (FAILED(pSource->UnadviseSink(pcesmap->dwCookie)))
  198. goto Next;
  199. Next:
  200. SafeReleaseClear(pcesmap->pComp);
  201. SafeRelease(pSource);
  202. nCnt--;
  203. pcesmap++;
  204. }
  205. _rgcesmap.Clear();
  206. hr = S_OK;
  207. return hr;
  208. }