⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 catalogwrapper.cpp

📁 中间件编写示例 COM与.NET组件服务
💻 CPP
字号:
#include "stdafx.h"
#include <comadmin.h>
#include "adminwrap.h"
#include "CatalogWrapper.h"

 
CCatalogWrapper::CCatalogWrapper()
{
}
CCatalogWrapper::~CCatalogWrapper()
{
}
HRESULT CCatalogWrapper::FinalConstruct()
{
   return S_OK;
}
void CCatalogWrapper::FinalRelease()
{
}

STDMETHODIMP CCatalogWrapper::Add(LPCWSTR pwzSubName,CLSID clsidEventClass,REFIID iidInterface,IUnknown *pSink)
{
   if (wcslen(pwzSubName) == 0 || pSink == NULL)
      return E_INVALIDARG;

   HRESULT hres = S_OK;
   //CApp is based on PSDK class, with my modifications. You can use that or write raw calls like everywhere else in this class
   hres = CApp::AddTransientSubscription(pwzSubName,clsidEventClass,iidInterface,pSink);
   return hres;
}
STDMETHODIMP CCatalogWrapper::RemoveFilter(LPCWSTR pwzSubName)
{
   return AddFilter(pwzSubName,L"");
}
STDMETHODIMP CCatalogWrapper::AddFilter(LPCWSTR pwzSubName,LPCWSTR pwzCriteria)
{
   HRESULT  hres = S_OK;
   LONG lRetVal = 0;

   //Empty criteria?
   if(!wcscmp(pwzSubName,L""))
   {
      return E_INVALIDARG;
   }

   ICOMAdminCatalog* pCatalog = NULL;
   ICatalogCollection* pTransientSubscriptionCollection = NULL;
   ICatalogObject* pTransientSubscription = NULL;

   LONG lIndex = 0;
   hres = ::CoCreateInstance(__uuidof(COMAdminCatalog), NULL, CLSCTX_SERVER, __uuidof(ICOMAdminCatalog),(void**) &pCatalog);
   EXIT(hres);

   hres = GetCollection(pCatalog,_bstr_t("TransientSubscriptions"), &pTransientSubscriptionCollection);
   EXIT(hres);

   hres = GetNamedObjectFromCollection(pTransientSubscriptionCollection,_bstr_t(pwzSubName),&pTransientSubscription,&lIndex,TRANSIENTSUB);
   EXIT(hres);

   hres = SetStringProperty(pTransientSubscription,_bstr_t("FilterCriteria"),(WCHAR*)pwzCriteria);
   EXIT(hres);

   hres = pTransientSubscriptionCollection->SaveChanges(&lRetVal);
   EXIT(hres);

exit:
   SAFERELEASE(pTransientSubscriptionCollection);
   SAFERELEASE(pCatalog);
   SAFERELEASE(pTransientSubscription);
   return hres;
}

STDMETHODIMP CCatalogWrapper::Remove(LPCWSTR pwzName)
{
   HRESULT  hres = S_OK;
   hres = CApp::RemoveTransientSubscription(pwzName);
   return hres;
}
STDMETHODIMP CCatalogWrapper::Install(CLSID clsidEventClass,CLSID clsidFilter)
{
   HRESULT hres = S_OK;

   IDispatch* pDispTemp = NULL;
   ICOMAdminCatalog* pCatalog    = NULL;
   ICatalogObject* pApplication  = NULL;
   ICatalogObject* pComponent    = NULL;
   ICatalogCollection* pApplicationCollection = NULL;
   ICatalogCollection* pComponentCollection   = NULL;


   long nApplicationCount = 0;
   long nComponentCount = 0;
   long nChanges = 0;
   int i = 0;//Application index
   int j = 0;//Component index
   
   WCHAR pwszFilterCLSID[130];
   ::StringFromGUID2(clsidFilter,pwszFilterCLSID,129);
   _variant_t varFilterCLSID(pwszFilterCLSID);

   WCHAR* pwszProgID = NULL;
   hres = ::ProgIDFromCLSID(clsidEventClass,&pwszProgID);
   if(FAILED(hres))
      return E_INVALIDARG;

   _bstr_t bstrProgID(pwszProgID);
   ::CoTaskMemFree(pwszProgID);


   hres = ::CoCreateInstance(__uuidof(COMAdminCatalog), NULL, CLSCTX_SERVER, __uuidof(ICOMAdminCatalog),(void**) &pCatalog);
   EXIT(hres);


   //Get the application collection, to find the application containing the event class
   hres = GetCollection(pCatalog, _bstr_t("Applications"),&pApplicationCollection);
   EXIT(hres);

   
   hres = pApplicationCollection->get_Count(&nApplicationCount);
   EXIT(hres);

   ATLASSERT(nApplicationCount > 0);


   //First we need to find the event class. 
   //Two nested loops: The outer all the applications, and the inner on the components collection inside

   while(i<nApplicationCount)
   {
      //Get the current application
      hres = pApplicationCollection->get_Item(i,&pDispTemp);
      EXIT(hres);

      hres = pDispTemp->QueryInterface(IID_ICatalogObject,(void**)&pApplication); 
      EXIT(hres);
      RELEASE(pDispTemp);

      hres = GetCollection(pApplicationCollection,pApplication,_bstr_t("Components"),&pComponentCollection);
      EXIT(hres);

      RELEASE(pApplication);

      hres = pComponentCollection->get_Count(&nComponentCount);
      EXIT(hres);

      j=0;
      while(j<nComponentCount)
      {
         hres = pComponentCollection->get_Item(j,&pDispTemp);
         EXIT(hres);


         hres = pDispTemp->QueryInterface(IID_ICatalogObject,(void**)&pComponent); 
         EXIT(hres);
         RELEASE(pDispTemp);

         _variant_t varName;
         hres = pComponent->get_Name(&varName);
         ATLASSERT(SUCCEEDED(hres));

         _bstr_t bstrName(varName);
         if(bstrProgID == bstrName)
         {
            break;
         }
         j++;
         RELEASE(pComponent);
      }
      if(pComponent)
      {
         break;
      }
      i++;
      RELEASE(pComponentCollection);
   }

   if(pComponent == NULL)
   {
      exit(E_INVALIDARG);
   }
   //pComponent now points to our beloved event class
   hres = pComponent->put_Value(_bstr_t("MultiInterfacePublisherFilterCLSID"),varFilterCLSID);
   EXIT(hres);
   
   hres = pComponentCollection->SaveChanges(&nChanges);
   EXIT(hres);

//Clean up block:
   exit(hres);
exit:
   SAFERELEASE(pDispTemp);
   SAFERELEASE(pCatalog);
   SAFERELEASE(pApplication);
   SAFERELEASE(pComponent);
   SAFERELEASE(pApplicationCollection);
   SAFERELEASE(pComponentCollection);

   return hres;
}

STDMETHODIMP CCatalogWrapper::Remove(CLSID clsidEventClass)
{
   HRESULT  hres = S_OK;
   return Install(clsidEventClass,CLSID_NULL);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -