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

📄 adminwrap.cpp

📁 中间件编写示例 COM与.NET组件服务
💻 CPP
📖 第 1 页 / 共 3 页
字号:
         if (!varname.bstrVal)
            exit(hr=E_OUTOFMEMORY);
         varname.vt = VT_BSTR;

         hr = pProp->put_Value(g_bNAME, varname);
         exit(hr);

         bname = g_bVALUE;
         pFinal = pProp;
         pFinals = pProps;
      }
      else
         exit(hr);
      pFinal = pProp;
      pFinals = pProps;
      break;

   default:
      exit(hr= E_INVALIDARG);
   }

   hr = pFinal->put_Value(bname, var);
   exit(hr);

   hr = pFinals->SaveChanges(&lret);

exit:
   SAFERELEASE(pDisp);
   SAFERELEASE(pProp);
   SAFERELEASE(pProps);
   SAFERELEASE(pSubs);
   SAFERELEASE(pSub);
   SAFERELEASE(pApp);
   SAFERELEASE(pApps);
   return hr;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Name        : GetCollection
// Purppose    : Gets a collection from the Catalogue
// Parameters  :
//             : ICOMAdminCatalog*    pCat      - Admin Catalog object
//             : BSTR                 szCollName - Collection Name
//             : ICatalogCollection** ppCol     - Collection returned
// Return Value : HRESULT
////////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT GetCollection(ICOMAdminCatalog* pCat, BSTR szCollName, ICatalogCollection** ppCol)
{
   HRESULT hr = E_INVALIDARG;
   CComBSTR bstr;
   IDispatch* pDisp = NULL;
   if (!pCat || !szCollName || !ppCol) 
      goto exit;
   bstr = szCollName;
   hr = pCat->GetCollection(bstr.m_str, &pDisp);
   exit(hr);
   hr = pDisp->QueryInterface(__uuidof(ICatalogCollection), (LPVOID*)ppCol);
   exit(hr);
   hr = (*ppCol)->Populate();
exit:
   SAFERELEASE(pDisp);
   return hr;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Name        : GetCollection
// Purppose    : Gets a collection from the Catalogue
// Parameters  :
//             : ICatalogCollection*  pParentColl - Parent Collection
//             : ICatalogObject*      pCatObj     - Parent Object
//             : BSTR                 szCollName   - Collection Name
//             : ICatalogCollection** ppCol       - Collection returned
// Return Value : HRESULT
////////////////////////////////////////////////////////////////////////////////////////////////////  
HRESULT GetCollection(ICatalogCollection* pParentColl, ICatalogObject* pCatObj, BSTR szCollName, ICatalogCollection** ppCol)
{
   HRESULT hr = E_INVALIDARG;
   CComBSTR bstr;
   VARIANT var;
   IDispatch* pDisp = NULL;
   VariantInit(&var);
   if (!pParentColl || !pCatObj || !szCollName || !ppCol)
      goto exit;
   hr = pCatObj->get_Key(&var);
   exit(hr);
   bstr = szCollName;
   hr = pParentColl->GetCollection(bstr.m_str, var, &pDisp);
   exit(hr);
   hr = pDisp->QueryInterface(__uuidof(ICatalogCollection), (LPVOID*)ppCol);
   exit(hr);
   hr = (*ppCol)->Populate();
exit:
   VariantClear(&var);
   if(pDisp)
      pDisp->Release();
   return hr;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Name        : GetObjectAndCollection
// Purppose    : Gets a collection from the Catalogue
// Parameters  :
//             : ICOMAdminCatalog*    pCat        - Admin Catalog object
//             : BSTR                 szCollName   - Collection Name
//             : BSTR                 szObjectName - Object name
//             : ADMINTYPE            type         - Type of the object
//             : ICatalogCollection** ppCol       - Collection returned
//             : ICatalogObject*      ppObj       - Object returned
// Return Value : HRESULT
////////////////////////////////////////////////////////////////////////////////////////////////////  
HRESULT GetObjectAndCollection(ICOMAdminCatalog* pCat, BSTR szCollName, BSTR szObjectName, ADMINTYPE type, ICatalogCollection** ppCol, ICatalogObject** ppObj)
{
   HRESULT hr = E_FAIL;
   hr = GetCollection(pCat, szCollName, ppCol);
   exit(hr);
   hr = GetNamedObjectFromCollection(*ppCol, szObjectName, ppObj, NULL, type);
exit:
   return hr;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Name        : GetObjectAndCollection
// Purppose    : Gets a collection from the Catalogue
// Parameters  :
//             : ICatalogCollection*  pParentColl   - Parent Collection
//             : ICatalogObject*      pParentObject - Parent Object
//             : BSTR                 szCollName     - Collection Name
//             : BSTR                 szObjectName   - Object name
//             : ADMINTYPE            type           - Type of the object
//             : ICatalogCollection** ppCol         - Collection returned
//             : ICatalogObject*      ppObj         - Object returned
// Return Value : HRESULT
////////////////////////////////////////////////////////////////////////////////////////////////////  
HRESULT GetObjectAndCollection(ICatalogCollection* pParentColl, ICatalogObject* pParentObject, BSTR szCollName, BSTR szObjectName, ADMINTYPE type, ICatalogCollection** ppCol, ICatalogObject** ppObj)
{
   HRESULT hr = E_FAIL;
   hr = GetCollection(pParentColl, pParentObject, szCollName, ppCol);   
   exit(hr);
   hr = GetNamedObjectFromCollection(*ppCol, szObjectName, ppObj, NULL, type);
exit:
   return hr;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Name        : RemoveNamedObjectFromCollection
// Purppose    : Removes an object from a collection
// Parameters  :
//             : ICatalogCollection* pParentColl - Collection from the object has to be removed
//             : BSTR                szObjectName - Object name
//             : ADMINTYPE           type         - type of the object
// Return Value : HRESULT
////////////////////////////////////////////////////////////////////////////////////////////////////  
HRESULT RemoveNamedObjectFromCollection(ICatalogCollection* pCol, BSTR szObjectName, ADMINTYPE type)
{
   HRESULT hr = E_FAIL;
   ICatalogObject* pCatObj = NULL;
   LONG i, lTotal;

   if (wcscmp(szObjectName, SysAllocString(L"*"))) 
   {
      hr = GetNamedObjectFromCollection(pCol, szObjectName, &pCatObj, &i, type);
      exit(hr);
      hr = pCol->Remove(i);
      exit(hr);
      hr = pCol->SaveChanges(&i);
   }
   else
   {
      hr = pCol->get_Count(&lTotal);
      exit(hr);
      for (i=lTotal-1; i >= 0; i--)
      {
         hr = pCol->Remove(i);
         exit(hr);
         hr = pCol->SaveChanges(&i);
         exit(hr);
      }
   }

exit:
   if(pCatObj)
      pCatObj->Release();
   return hr;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Name        : GetNamedObjectFromCollection
// Purppose    : Gets an object from a collection
// Parameters  :
//             : ICatalogCollection* pParentColl - Collection from the object has to be retrieved
//             : BSTR                bstrObjName  - Object name
//             : ADMINTYPE           type         - type of the object
//             : ICatalogObject**    ppObj        - return Object
//             : LONG*               plIndex      - Index of the object (-1, if not found)
//             : ADMINTYPE           type         - type of the object
//             : BSTR                bstrPropname - name of the property
// Return Value : HRESULT
////////////////////////////////////////////////////////////////////////////////////////////////////  
HRESULT GetNamedObjectFromCollection(ICatalogCollection* pCol, BSTR bstrObjName, ICatalogObject** ppObj, LONG* plIndex, ADMINTYPE type, BSTR bstrPropname)
{
   HRESULT hr = E_FAIL;
   LONG lCount = 0;
   LONG i = 0;
   ICatalogObject* pObj = NULL;
   BSTR bstrName;
   VARIANT varProp;
   BOOL  fFound = FALSE;

   VariantInit(&varProp);
   if (!pCol || !bstrObjName || !ppObj)   
      return E_INVALIDARG;
   
   if (!bstrPropname)
   {
      hr = GetObjectNameProperty(type, &bstrName);
      exit(hr);
   }
   else
      bstrName = bstrPropname;

   *ppObj = NULL;

   hr = pCol->Populate();
   exit(hr);

   hr = pCol->get_Count(&lCount);
   exit(hr);

   if (lCount == 0)
   {
      *plIndex = -1; // To indicate that the object is not found in the collection
      exit(hr=E_FAIL);
   }
   
   // Loop through 
   for (i = 0; i < lCount; i++)
   {
      VariantClear(&varProp);

      // Get the next item
      hr = pCol->get_Item(i, (IDispatch**)&pObj);
      exit(hr);
      
      // Retrieve it's name property
      hr = pObj->get_Value((BSTR)bstrName, &varProp);
      exit(hr);

      // Check it's name property for a match
      if (wcscmp(bstrObjName, varProp.bstrVal) == 0)
      {
         // Found it!
         *ppObj = pObj;
         pObj->AddRef();
         VariantClear(&varProp);
         // does user want index of the just-found object?
         if (plIndex)
            *plIndex = i;
         fFound = TRUE;
         hr = S_OK;
         goto exit;
      }
       SAFERELEASE(pObj);
   }
   if (plIndex)
      *plIndex = -1; // To indicate that the object is not found in the collection
exit:
   SysFreeString(bstrName);
   // If we got here then we didn't find it
   SAFERELEASE(pObj);
   return hr;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Name        : GetObjectNameProperty
// Purppose    : Gets the property name of the object
// Parameters  :
//             : ADMINTYPE type  - type of the object
//             : BSTR*     pbstr - name
// Return Value : HRESULT
////////////////////////////////////////////////////////////////////////////////////////////////////  
HRESULT GetObjectNameProperty(ADMINTYPE type, BSTR* pbstr)
{  
   if (!pbstr)
      return E_INVALIDARG;

   switch (type)
   {  
   case APPLICATION:
   case SUBSCRIPTION:
   case TRANSIENTSUB:
      *pbstr = SysAllocString(g_wczNAME);
      break;
   case COMPONENT:
      *pbstr = SysAllocString(g_wczProgIDProp);
      break;
   default:
      assert(FALSE);
      break;
   }
   return S_OK;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Name        : SetStringProperty
// Purppose    : Sets the named string property of the object
// Parameters  :
//             : ICatalogObject* pcat  -  Pointer to the object
//             : BSTR            bPROP -  Name of the property
//             : WCHAR*          pval  -  Value of the property
// Return Value : HRESULT
////////////////////////////////////////////////////////////////////////////////////////////////////  
HRESULT SetStringProperty(ICatalogObject *pcat, BSTR bPROP, WCHAR* pval)
{
   HRESULT hr;
   VARIANT var;
   if (!pcat)
      return E_INVALIDARG;

   if (!pval)
      return S_FALSE;

   VariantClear(&var);

   var.bstrVal = SysAllocString(pval);
   if (!var.bstrVal)
      exit(hr=E_OUTOFMEMORY);
   var.vt = VT_BSTR;

   hr = pcat->put_Value(bPROP, var);
   exit(hr);

exit:
   VariantClear(&var);
   return hr;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Name        : SetIUnknownProperty
// Purppose    : Sets the named IUnknown property of the object
// Parameters  :
//             : ICatalogObject *pcat  -  Pointer to the object
//             : BSTR            bPROP -  Name of the property
//             : IUnknown*       punk  -  Value of the property
// Return Value : HRESULT
////////////////////////////////////////////////////////////////////////////////////////////////////  
HRESULT SetIUnknownProperty(ICatalogObject *pcat, BSTR bPROP, IUnknown* punk)
{
   HRESULT hr;
   VARIANT var;
   if (!punk || !pcat)
      return E_INVALIDARG;

   VariantClear(&var);

   var.punkVal = punk;
   var.vt = VT_UNKNOWN;
   punk->AddRef();   // We'll be caling VariantClear

   hr = pcat->put_Value(bPROP, var);
   exit(hr);

exit:
   VariantClear(&var);
   return hr;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Name        : SetBOOLProperty
// Purppose    : Sets the named BOOL property of the object
// Parameters  :
//             : ICatalogObject *pcat -  Pointer to the object
//             : BSTR bPROP           -  Name of the property
//             : BOOL fFlag           -  Value of the property
// Return Value : HRESULT
////////////////////////////////////////////////////////////////////////////////////////////////////  
HRESULT SetBoolProperty(ICatalogObject *pcat, BSTR bPROP, BOOL fFlag)
{
   HRESULT  hr;
   VARIANT var;

   if (!pcat)
      return E_INVALIDARG;

   VariantClear(&var);

   var.boolVal = (short) fFlag;
   var.vt = VT_BOOL;

   hr = pcat->put_Value(bPROP, var);
   exit(hr);

exit:
   VariantClear(&var);
   return hr;
}

⌨️ 快捷键说明

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