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.

246 lines
7.3 KiB

  1. // CompCat.cpp : implementation of the CComponentCategory class
  2. //
  3. //+-------------------------------------------------------------------------
  4. //
  5. // Microsoft Windows
  6. // Copyright (C) Microsoft Corporation, 1992 - 1999
  7. //
  8. // File: CompCat.cpp
  9. //
  10. // Contents: Enumerates the component categories
  11. //
  12. // History: 01-Aug-96 WayneSc Created
  13. //
  14. //--------------------------------------------------------------------------
  15. #include "stdafx.h" //precompiled header
  16. #include <comcat.h> // COM Component Categoories Manager
  17. #include "compcat.h"
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. DEBUG_DECLARE_INSTANCE_COUNTER(CComponentCategory);
  23. CComponentCategory::CComponentCategory()
  24. {
  25. BOOL const created = m_iml.Create( IDB_IMAGELIST, 16 /*cx*/, 4 /*cGrow*/, RGB(0,255,0) /*RGBLTGREEN*/ );
  26. ASSERT(created);
  27. DEBUG_INCREMENT_INSTANCE_COUNTER(CComponentCategory);
  28. }
  29. CComponentCategory::~CComponentCategory()
  30. {
  31. // delete all memory allocated for categories
  32. for (int i=0; i <= m_arpCategoryInfo.GetUpperBound(); i++)
  33. delete m_arpCategoryInfo.GetAt(i);
  34. m_arpCategoryInfo.RemoveAll();
  35. // delete all memory allocated for components
  36. for (i=0; i <= m_arpComponentInfo.GetUpperBound(); i++)
  37. delete m_arpComponentInfo.GetAt(i);
  38. m_arpComponentInfo.RemoveAll();
  39. m_iml.Destroy();
  40. DEBUG_DECREMENT_INSTANCE_COUNTER(CComponentCategory);
  41. }
  42. void CComponentCategory::EnumComponentCategories(void)
  43. {
  44. ICatInformation* pci = NULL;
  45. HRESULT hr;
  46. hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, CLSCTX_INPROC, IID_ICatInformation, (void**)&pci);
  47. if (SUCCEEDED(hr))
  48. {
  49. IEnumCATEGORYINFO* penum = NULL;
  50. if (SUCCEEDED(hr = pci->EnumCategories(GetUserDefaultLCID(), &penum)))
  51. {
  52. CATEGORYINFO* pCatInfo = new CATEGORYINFO;
  53. while (penum->Next(1, pCatInfo, NULL) == S_OK)
  54. {
  55. // skip unnamed categories
  56. if ( pCatInfo->szDescription[0] && !IsEqualCATID(pCatInfo->catid, CATID_Control))
  57. {
  58. m_arpCategoryInfo.Add(pCatInfo);
  59. pCatInfo = new CATEGORYINFO;
  60. }
  61. }
  62. delete pCatInfo;
  63. penum->Release();
  64. }
  65. pci->Release();
  66. }
  67. }
  68. void CComponentCategory::EnumComponents()
  69. {
  70. ICatInformation* pci;
  71. HRESULT hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, MMC_CLSCTX_INPROC,
  72. IID_ICatInformation, (void**)&pci);
  73. if (SUCCEEDED(hr))
  74. {
  75. IEnumCLSID* penumClass;
  76. hr = pci->EnumClassesOfCategories(1, const_cast<GUID*>(&CATID_Control), 0, NULL, &penumClass);
  77. if (SUCCEEDED(hr))
  78. {
  79. CLSID clsid;
  80. while (penumClass->Next(1, &clsid, NULL) == S_OK)
  81. {
  82. TCHAR szCLSID [40];
  83. #ifdef _UNICODE
  84. StringFromGUID2(clsid, szCLSID, countof(szCLSID));
  85. #else
  86. WCHAR wszCLSID[40];
  87. StringFromGUID2(clsid, wszCLSID, countof(wszCLSID));
  88. WideCharToMultiByte(CP_ACP, 0, wszCLSID, -1, szCLSID, sizeof(szCLSID), NULL, NULL);
  89. #endif // _UNICODE
  90. COMPONENTINFO* pComponentInfo = new COMPONENTINFO;
  91. TCHAR szName[MAX_PATH];
  92. szName[0] = _T('\0');
  93. long cb = sizeof(szName)/sizeof(TCHAR);
  94. // Get control class name
  95. RegQueryValue(HKEY_CLASSES_ROOT, CStr("CLSID\\") + szCLSID, szName, &cb);
  96. if (szName[0] != _T('\0'))
  97. pComponentInfo->m_strName = szName;
  98. else
  99. pComponentInfo->m_strName = szCLSID;
  100. // set the remainder attributes
  101. pComponentInfo->m_clsid = clsid;
  102. pComponentInfo->m_uiBitmap=0; // (WayneSc) need to open up exe
  103. pComponentInfo->m_bSelected = TRUE;
  104. // Add component to array
  105. m_arpComponentInfo.Add(pComponentInfo);
  106. }
  107. penumClass->Release();
  108. }
  109. pci->Release();
  110. }
  111. }
  112. void CComponentCategory::FilterComponents(CATEGORYINFO* pCatInfo)
  113. {
  114. ICatInformation* pci;
  115. HRESULT hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr, NULL, MMC_CLSCTX_INPROC,
  116. IID_ICatInformation, (void**)&pci);
  117. if (SUCCEEDED(hr))
  118. {
  119. for (int i=0; i <= m_arpComponentInfo.GetUpperBound(); i++)
  120. {
  121. COMPONENTINFO* pCompInfo = m_arpComponentInfo.GetAt(i);
  122. // if NULL categories, select all conponents
  123. if (pCatInfo == NULL)
  124. {
  125. pCompInfo->m_bSelected = TRUE;
  126. }
  127. else
  128. {
  129. // Query if component implements the category
  130. pCompInfo->m_bSelected =
  131. (pci->IsClassOfCategories(pCompInfo->m_clsid, 1, &pCatInfo->catid, 0, NULL) == S_OK);
  132. }
  133. }
  134. pci->Release();
  135. }
  136. }
  137. // Helper function to create a component category and associated description
  138. HRESULT CComponentCategory::CreateComponentCategory(CATID catid, WCHAR* catDescription)
  139. {
  140. ICatRegister* pcr = NULL ;
  141. HRESULT hr = S_OK ;
  142. hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
  143. NULL, MMC_CLSCTX_INPROC, IID_ICatRegister, (void**)&pcr);
  144. if (FAILED(hr))
  145. return hr;
  146. // Make sure the HKCR\Component Categories\{..catid...}
  147. // key is registered
  148. CATEGORYINFO catinfo;
  149. catinfo.catid = catid;
  150. catinfo.lcid = 0x0409 ; // english
  151. // Make sure the provided description is not too long.
  152. // Only copy the first 127 characters if it is
  153. int len = wcslen(catDescription);
  154. if (len>127)
  155. len = 127;
  156. wcsncpy(catinfo.szDescription, catDescription, len);
  157. // Make sure the description is null terminated
  158. catinfo.szDescription[len] = '\0';
  159. hr = pcr->RegisterCategories(1, &catinfo);
  160. pcr->Release();
  161. return hr;
  162. }
  163. // Helper function to register a CLSID as belonging to a component category
  164. HRESULT CComponentCategory::RegisterCLSIDInCategory(REFCLSID clsid, CATID catid)
  165. {
  166. // Register your component categories information.
  167. ICatRegister* pcr = NULL ;
  168. HRESULT hr = S_OK ;
  169. hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
  170. NULL, MMC_CLSCTX_INPROC, IID_ICatRegister, (void**)&pcr);
  171. if (SUCCEEDED(hr))
  172. {
  173. // Register this category as being "implemented" by
  174. // the class.
  175. CATID rgcatid[1] ;
  176. rgcatid[0] = catid;
  177. hr = pcr->RegisterClassImplCategories(clsid, 1, rgcatid);
  178. }
  179. if (pcr != NULL)
  180. pcr->Release();
  181. return hr;
  182. }
  183. // Helper function to unregister a CLSID as belonging to a component category
  184. HRESULT CComponentCategory::UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid)
  185. {
  186. ICatRegister* pcr = NULL ;
  187. HRESULT hr = S_OK ;
  188. hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
  189. NULL, MMC_CLSCTX_INPROC, IID_ICatRegister, (void**)&pcr);
  190. if (SUCCEEDED(hr))
  191. {
  192. // Unregister this category as being "implemented" by
  193. // the class.
  194. CATID rgcatid[1] ;
  195. rgcatid[0] = catid;
  196. hr = pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid);
  197. }
  198. if (pcr != NULL)
  199. pcr->Release();
  200. return hr;
  201. }