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.

241 lines
7.5 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); // this is a weird API - it takes a TSTR but asks for the length in cb, not cch
  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. DECLARE_SC(sc, TEXT("CComponentCategory::CreateComponentCategory"));
  141. CComPtr<ICatRegister> pcr;
  142. sc = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
  143. NULL, MMC_CLSCTX_INPROC, IID_ICatRegister, (void**)&pcr);
  144. if (sc)
  145. return sc.ToHr();
  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. sc = StringCchCopyW(catinfo.szDescription, countof(catinfo.szDescription), catDescription);
  152. if(sc)
  153. return sc.ToHr();
  154. sc = pcr->RegisterCategories(1, &catinfo);
  155. if(sc)
  156. return sc.ToHr();
  157. return sc.ToHr();
  158. }
  159. // Helper function to register a CLSID as belonging to a component category
  160. HRESULT CComponentCategory::RegisterCLSIDInCategory(REFCLSID clsid, CATID catid)
  161. {
  162. // Register your component categories information.
  163. ICatRegister* pcr = NULL ;
  164. HRESULT hr = S_OK ;
  165. hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
  166. NULL, MMC_CLSCTX_INPROC, IID_ICatRegister, (void**)&pcr);
  167. if (SUCCEEDED(hr))
  168. {
  169. // Register this category as being "implemented" by
  170. // the class.
  171. CATID rgcatid[1] ;
  172. rgcatid[0] = catid;
  173. hr = pcr->RegisterClassImplCategories(clsid, 1, rgcatid);
  174. }
  175. if (pcr != NULL)
  176. pcr->Release();
  177. return hr;
  178. }
  179. // Helper function to unregister a CLSID as belonging to a component category
  180. HRESULT CComponentCategory::UnRegisterCLSIDInCategory(REFCLSID clsid, CATID catid)
  181. {
  182. ICatRegister* pcr = NULL ;
  183. HRESULT hr = S_OK ;
  184. hr = CoCreateInstance(CLSID_StdComponentCategoriesMgr,
  185. NULL, MMC_CLSCTX_INPROC, IID_ICatRegister, (void**)&pcr);
  186. if (SUCCEEDED(hr))
  187. {
  188. // Unregister this category as being "implemented" by
  189. // the class.
  190. CATID rgcatid[1] ;
  191. rgcatid[0] = catid;
  192. hr = pcr->UnRegisterClassImplCategories(clsid, 1, rgcatid);
  193. }
  194. if (pcr != NULL)
  195. pcr->Release();
  196. return hr;
  197. }