© 2004 Microsoft Corporation. All rights reserved.

Figure 10 Setting a Recycling Limit Programmatically
  //usage: "MyApp" will be recycled after 1000 object activations
//hres = SetRecycleByActivations("MyApp",1000);
HRESULT SetRecycleByActivations(LPCSTR lpcszAppName,DWORD dwActivations)
{
   //Verify app name is valid    
   if(_bstr_t(lpcszAppName) == _bstr_t(""))
   {
      return  E_INVALIDARG;
   }
   HRESULT hres = S_OK;
   ICOMAdminCatalog2* pCatalog = NULL;
   hres = ::CoCreateInstance(CLSID_COMAdminCatalog, NULL,CLSCTX_SERVER,
                             IID_ICOMAdminCatalog2,(void**)&pCatalog);
    ICatalogObject* pApplication  = NULL;
    ICatalogCollection* pApplicationCollection = NULL;
    long nApplicationCount = 0;
    int i = 0;//Application index
   //Get the application collection
   hres = pCatalog->GetCollection(_bstr_t("Applications"),
                      (IDispatch**)&pApplicationCollection);
   pCatalog->Release();
      
   hres = pApplicationCollection->Populate();
  
   hres = pApplicationCollection->get_Count(&nApplicationCount);
   hres = COMADMIN_E_OBJECT_DOES_NOT_EXIST;
   for(i=0;i<nApplicationCount;i++)
   {
      //Get the current application
      hres = pApplicationCollection->get_Item
          (i,(IDispatch**)&pApplication);
      _variant_t varName;
      pApplication->get_Name(&varName);
      _bstr_t bstrName(varName);
      if(bstrName == _bstr_t(lpcszAppName))
      {
         long ret = 0;
         _variant_t varActivationLimit((long)dwActivations);
         hres = pApplication->put_Value(_bstr_t("RecycleActivationLimit"),
                                        varActivationLimit);
         hres = pApplicationCollection->SaveChanges(&ret);
      }
      pApplication->Release();
   }
   pApplicationCollection->Release();
   return hres;
}