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.

426 lines
10 KiB

  1. //
  2. // lbaddin.cpp
  3. //
  4. #include "private.h"
  5. #include "globals.h"
  6. #include "ctflbui.h"
  7. #include "regsvr.h"
  8. #include "cregkey.h"
  9. #include "tim.h"
  10. #include "lbaddin.h"
  11. const TCHAR c_szLangBarAddInKey[] = TEXT("SOFTWARE\\Microsoft\\CTF\\LangBarAddIn\\");
  12. const TCHAR c_szGuid[] = TEXT("GUID");
  13. const WCHAR c_wszFilePath[] = L"FilePath";
  14. typedef HRESULT (STDAPICALLTYPE* LPFNCTFGETLANGBARADDIN)(ITfLangBarAddIn **ppAddIn);
  15. extern CPtrArray<SYSTHREAD> *g_rgSysThread;
  16. //////////////////////////////////////////////////////////////////////////////
  17. //
  18. // LangBar AddIn service
  19. //
  20. //////////////////////////////////////////////////////////////////////////////
  21. //+---------------------------------------------------------------------------
  22. //
  23. // TF_RegisterLangBarAddIn
  24. //
  25. //+---------------------------------------------------------------------------
  26. HRESULT TF_RegisterLangBarAddIn(REFGUID rguidUISrv, const WCHAR *pwszFile, DWORD dwFlags)
  27. {
  28. BOOL fLocalMachine = dwFlags & TF_RLBAI_LOCALMACHINE;
  29. if (IsEqualGUID(GUID_NULL, rguidUISrv))
  30. return E_INVALIDARG;
  31. if (!pwszFile)
  32. return E_INVALIDARG;
  33. CMyRegKey key;
  34. TCHAR szKey[256];
  35. StringCopyArray(szKey, c_szLangBarAddInKey);
  36. CLSIDToStringA(rguidUISrv, szKey + lstrlen(szKey));
  37. if (key.Create(fLocalMachine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, szKey) != S_OK)
  38. return E_FAIL;
  39. if (key.SetValueW(pwszFile, c_wszFilePath) != S_OK)
  40. return E_FAIL;
  41. if (key.SetValue((dwFlags & TF_RLBAI_ENABLE) ? 1 : 0, c_szEnable) != S_OK)
  42. return E_FAIL;
  43. return S_OK;
  44. }
  45. //+---------------------------------------------------------------------------
  46. //
  47. // TF_UnregisterLangBarAddIn
  48. //
  49. //+---------------------------------------------------------------------------
  50. HRESULT TF_UnregisterLangBarAddIn(REFGUID rguidUISrv, DWORD dwFlags)
  51. {
  52. CMyRegKey key;
  53. TCHAR szKey[256];
  54. TCHAR szSubKey[256];
  55. BOOL fLocalMachine = dwFlags & TF_RLBAI_LOCALMACHINE;
  56. if (IsEqualGUID(GUID_NULL, rguidUISrv))
  57. return E_INVALIDARG;
  58. StringCopyArray(szKey, c_szLangBarAddInKey);
  59. if (key.Open(fLocalMachine ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, szKey, KEY_ALL_ACCESS) != S_OK)
  60. return E_FAIL;
  61. CLSIDToStringA(rguidUISrv, szSubKey);
  62. key.RecurseDeleteKey(szSubKey);
  63. return S_OK;
  64. }
  65. //+---------------------------------------------------------------------------
  66. //
  67. // TF_ClearLangBarAddIns
  68. //
  69. //+---------------------------------------------------------------------------
  70. HRESULT TF_ClearLangBarAddIns(REFCLSID rclsid)
  71. {
  72. int i;
  73. CicEnterCriticalSection(g_csInDllMain);
  74. if (g_rgSysThread)
  75. {
  76. int nCnt = g_rgSysThread->Count();
  77. for (i = 0; i < nCnt; i++)
  78. {
  79. SYSTHREAD *psfn = g_rgSysThread->Get(i);
  80. if (psfn)
  81. {
  82. ClearLangBarAddIns(psfn, rclsid);
  83. }
  84. }
  85. }
  86. CicLeaveCriticalSection(g_csInDllMain);
  87. return S_OK;
  88. }
  89. BOOL ClearLangBarAddIns(SYSTHREAD *psfn, REFCLSID rclsid)
  90. {
  91. int i;
  92. int nCnt;
  93. if (!psfn->prgLBAddIn)
  94. return TRUE;
  95. nCnt = psfn->prgLBAddIn->Count();
  96. for (i = nCnt - 1; i >= 0; i--)
  97. {
  98. LANGBARADDIN *pAddIn = psfn->prgLBAddIn->Get(i);
  99. if (pAddIn)
  100. {
  101. if (pAddIn->_plbai)
  102. {
  103. if (IsEqualGUID(rclsid, CLSID_NULL) ||
  104. IsEqualGUID(rclsid, pAddIn->_clsid))
  105. {
  106. _try {
  107. if (pAddIn->_fStarted)
  108. pAddIn->_plbai->OnTerminate();
  109. pAddIn->_plbai->Release();
  110. }
  111. _except(1) {
  112. Assert(0);
  113. }
  114. }
  115. }
  116. delete pAddIn;
  117. }
  118. psfn->prgLBAddIn->Remove(i, 1);
  119. }
  120. if (!psfn->prgLBAddIn->Count())
  121. UninitLangBarAddIns(psfn);
  122. return TRUE;
  123. }
  124. //+---------------------------------------------------------------------------
  125. //
  126. // InitLangBarAddInArray
  127. //
  128. //+---------------------------------------------------------------------------
  129. void InitLangBarAddInArray()
  130. {
  131. CMyRegKey key;
  132. DWORD dwIndex;
  133. char szKey[MAX_PATH];
  134. SYSTHREAD *psfn = GetSYSTHREAD();
  135. BOOL fLocalMachine = FALSE;
  136. if (!psfn)
  137. return;
  138. if (key.Open(HKEY_CURRENT_USER, c_szLangBarAddInKey, KEY_READ) != S_OK)
  139. {
  140. //
  141. // if there is no addin current user, try local machine.
  142. //
  143. goto StartInLocalmachine;
  144. }
  145. TryAgainInLocalMachine:
  146. dwIndex = 0;
  147. while (key.EnumKey(dwIndex, szKey, ARRAYSIZE(szKey)) == S_OK)
  148. {
  149. CMyRegKey subkey;
  150. GUID guid;
  151. WCHAR wszFilePath[MAX_PATH];
  152. LANGBARADDIN **ppAddIn;
  153. if (subkey.Open(key, szKey, KEY_READ) != S_OK)
  154. goto Next;
  155. if (subkey.QueryValueCchW(wszFilePath, c_wszFilePath, ARRAYSIZE(wszFilePath)) != S_OK)
  156. goto Next;
  157. StringAToCLSID(szKey, &guid);
  158. if (psfn->prgLBAddIn)
  159. {
  160. int i;
  161. int nCnt = psfn->prgLBAddIn->Count();
  162. for (i = 0; i < nCnt; i++)
  163. {
  164. LANGBARADDIN *pAddIn = psfn->prgLBAddIn->Get(i);
  165. if (pAddIn && IsEqualGUID(pAddIn->_guid, guid))
  166. goto Next;
  167. }
  168. }
  169. else
  170. {
  171. psfn->prgLBAddIn = new CPtrArray<LANGBARADDIN>;
  172. if (!psfn->prgLBAddIn)
  173. {
  174. return;
  175. }
  176. }
  177. //
  178. // if there is no reg entry for Enable, the default is enabled.
  179. //
  180. DWORD dwRet;
  181. BOOL fEnabled = FALSE;
  182. if (subkey.QueryValue(dwRet, c_szEnable) == S_OK)
  183. {
  184. if (dwRet)
  185. fEnabled = TRUE;
  186. }
  187. ppAddIn = psfn->prgLBAddIn->Append(1);
  188. if (!ppAddIn)
  189. goto Next;
  190. *ppAddIn = new LANGBARADDIN;
  191. if (!*ppAddIn)
  192. goto Next;
  193. (*ppAddIn)->_guid = guid;
  194. (*ppAddIn)->_fEnabled = fEnabled ? TRUE : FALSE;
  195. StringCopyArrayW((*ppAddIn)->_wszFilePath, wszFilePath);
  196. Next:
  197. dwIndex++;
  198. }
  199. key.Close();
  200. if (!fLocalMachine)
  201. {
  202. StartInLocalmachine:
  203. fLocalMachine = TRUE;
  204. if (key.Open(HKEY_LOCAL_MACHINE, c_szLangBarAddInKey, KEY_READ) == S_OK)
  205. {
  206. //
  207. // it is time to try local machine.
  208. //
  209. goto TryAgainInLocalMachine;
  210. }
  211. }
  212. return;
  213. }
  214. //+---------------------------------------------------------------------------
  215. //
  216. // LoadLangBarAddIns
  217. //
  218. //+---------------------------------------------------------------------------
  219. BOOL LoadLangBarAddIns(SYSTHREAD *psfn)
  220. {
  221. int i;
  222. int nCnt;
  223. if (psfn->fLBAddInLoaded)
  224. return TRUE;
  225. psfn->fLBAddInLoaded = TRUE;
  226. if (!psfn->prgLBAddIn)
  227. {
  228. InitLangBarAddInArray();
  229. if (!psfn->prgLBAddIn)
  230. return FALSE;
  231. }
  232. if (!psfn->prgLBAddIn)
  233. return TRUE;
  234. nCnt = psfn->prgLBAddIn->Count();
  235. for (i = 0; i < nCnt; i++)
  236. {
  237. LANGBARADDIN *pAddIn = psfn->prgLBAddIn->Get(i);
  238. LPFNCTFGETLANGBARADDIN pfn;
  239. ITfLangBarAddIn *plbai;
  240. if (!pAddIn)
  241. continue;
  242. if (pAddIn->_plbai)
  243. continue;
  244. if (!pAddIn->_fEnabled)
  245. continue;
  246. if (!pAddIn->_hInst)
  247. {
  248. if (IsOnNT())
  249. pAddIn->_hInst = LoadLibraryW(pAddIn->_wszFilePath);
  250. else
  251. pAddIn->_hInst = LoadLibrary(WtoA(pAddIn->_wszFilePath));
  252. }
  253. pfn = (LPFNCTFGETLANGBARADDIN)GetProcAddress(pAddIn->_hInst,
  254. TEXT("CTFGetLangBarAddIn"));
  255. if (!pfn)
  256. continue;
  257. if (FAILED(pfn(&plbai)))
  258. continue;
  259. pAddIn->_plbai = plbai;
  260. }
  261. return psfn->prgLBAddIn->Count() ? TRUE : FALSE;
  262. }
  263. //+---------------------------------------------------------------------------
  264. //
  265. // UpdateLangBarAddIns
  266. //
  267. //+---------------------------------------------------------------------------
  268. void UpdateLangBarAddIns()
  269. {
  270. int i;
  271. int nCnt;
  272. DWORD dwFlags = 0;
  273. SYSTHREAD *psfn = GetSYSTHREAD();
  274. if (!psfn)
  275. return;
  276. if (!LoadLangBarAddIns(psfn))
  277. return;
  278. if (!psfn->prgLBAddIn)
  279. return;
  280. if (psfn->ptim && psfn->ptim->_GetFocusDocInputMgr())
  281. dwFlags = 1;
  282. nCnt = psfn->prgLBAddIn->Count();
  283. for (i = 0; i < nCnt; i++)
  284. {
  285. LANGBARADDIN *pAddIn = psfn->prgLBAddIn->Get(i);
  286. if (!pAddIn)
  287. continue;
  288. if (!pAddIn->_plbai)
  289. continue;
  290. if (!pAddIn->_fEnabled)
  291. continue;
  292. if (!pAddIn->_fStarted)
  293. {
  294. pAddIn->_fStarted = TRUE;
  295. HRESULT hr = E_FAIL;
  296. _try {
  297. hr = pAddIn->_plbai->OnStart(&pAddIn->_clsid);
  298. }
  299. _except(1) {
  300. Assert(0);
  301. }
  302. if (FAILED(hr))
  303. {
  304. pAddIn->_plbai->Release();
  305. pAddIn->_plbai = NULL;
  306. continue;
  307. }
  308. }
  309. _try {
  310. pAddIn->_plbai->OnUpdate(dwFlags);
  311. }
  312. _except(1) {
  313. Assert(0);
  314. }
  315. }
  316. }
  317. //+---------------------------------------------------------------------------
  318. //
  319. // TerminateLangBarAddIns
  320. //
  321. //+---------------------------------------------------------------------------
  322. void UninitLangBarAddIns(SYSTHREAD *psfn)
  323. {
  324. if (psfn->prgLBAddIn)
  325. {
  326. // Assert(!psfn->prgLBAddIn->Count());
  327. delete psfn->prgLBAddIn;
  328. psfn->prgLBAddIn = NULL;
  329. }
  330. psfn->fLBAddInLoaded = FALSE;
  331. }