OBJECT.CPP
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:7k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4.     Object.cpp
  5. Abstract:
  6.     ObjectTypeList class
  7.     
  8. Author:
  9. Environment:
  10.     User mode
  11. Revision History :
  12. --*/
  13. #include "adssmp.h"
  14. #pragma hdrstop
  15. ObjectTypeList::ObjectTypeList()
  16. {
  17.     _pObjList = NULL;
  18.     _dwCurrentIndex = 0;
  19.     _dwMaxElements = 0;
  20.     _dwUBound  = 0;
  21.     _dwLBound = 0;
  22. }
  23. HRESULT
  24. ObjectTypeList::CreateObjectTypeList(
  25.     VARIANT vFilter,
  26.     ObjectTypeList ** ppObjectTypeList
  27. ){
  28.     ObjectTypeList * pObjectTypeList = NULL;
  29.     HRESULT hr = S_OK;
  30.     pObjectTypeList = new ObjectTypeList;
  31.     if (!pObjectTypeList) {
  32.         hr = E_OUTOFMEMORY;
  33.         BAIL_ON_FAILURE(hr);
  34.     }
  35.     hr = BuildObjectArray(
  36.             vFilter,
  37.             &pObjectTypeList->_pObjList,
  38.             &pObjectTypeList->_dwMaxElements
  39.             );
  40.     if (FAILED(hr)) {
  41.         hr = BuildDefaultObjectArray(
  42.                 gpFilters,
  43.                 gdwMaxFilters,
  44.                 &pObjectTypeList->_pObjList,
  45.                 &pObjectTypeList->_dwMaxElements
  46.                 );
  47.         BAIL_ON_FAILURE(hr);
  48.     }
  49.     hr = SafeArrayGetUBound(
  50.                 pObjectTypeList->_pObjList,
  51.                 1,
  52.                 (long FAR *)&pObjectTypeList->_dwUBound
  53.                 );
  54.     BAIL_ON_FAILURE(hr);
  55.     hr = SafeArrayGetLBound(
  56.                 pObjectTypeList->_pObjList,
  57.                 1,
  58.                 (long FAR *)&pObjectTypeList->_dwLBound
  59.                 );
  60.     BAIL_ON_FAILURE(hr);
  61.     pObjectTypeList->_dwCurrentIndex = pObjectTypeList->_dwLBound;
  62.     *ppObjectTypeList = pObjectTypeList;
  63.     RRETURN(S_OK);
  64. error:
  65.     if (pObjectTypeList) {
  66.         delete pObjectTypeList;
  67.     }
  68.     RRETURN(hr);
  69. }
  70. ObjectTypeList::~ObjectTypeList()
  71. {
  72.     HRESULT hr = S_OK;
  73.     if (_pObjList) {
  74.         hr = SafeArrayDestroy(_pObjList);
  75.     }
  76. }
  77. HRESULT
  78. ObjectTypeList::GetCurrentObject(PDWORD pdwObject)
  79. {
  80.     HRESULT hr = S_OK;
  81.     if (_dwCurrentIndex > _dwUBound) {
  82.         return(E_FAIL);
  83.     }
  84.     hr = SafeArrayGetElement(
  85.                     _pObjList,
  86.                     (long FAR *)&_dwCurrentIndex,
  87.                     (void *)pdwObject
  88.                     );
  89.     RRETURN(hr);
  90. }
  91. HRESULT
  92. ObjectTypeList::Next()
  93. {
  94.     HRESULT hr = S_OK;
  95.     _dwCurrentIndex++;
  96.     if (_dwCurrentIndex > _dwUBound) {
  97.         return(E_FAIL);
  98.     }
  99.     return(hr);
  100. }
  101. HRESULT
  102. ObjectTypeList::Reset()
  103. {
  104.     HRESULT hr = S_OK;
  105.     return(hr);
  106. }
  107. HRESULT
  108. IsValidFilter(
  109.     LPWSTR ObjectName,
  110.     DWORD *pdwFilterId,
  111.     PFILTERS pFilters,
  112.     DWORD dwMaxFilters
  113. ){
  114.     DWORD i = 0;
  115.     for (i = 0; i < dwMaxFilters; i++) {
  116.         if (!_wcsicmp(ObjectName, (pFilters + i)->szObjectName)) {
  117.             *pdwFilterId = (pFilters + i)->dwFilterId;
  118.             RRETURN(S_OK);
  119.         }
  120.     }
  121.     *pdwFilterId = 0;
  122.     RRETURN(E_FAIL);
  123. }
  124. HRESULT
  125. BuildDefaultObjectArray(
  126.     PFILTERS  pFilters,
  127.     DWORD dwMaxFilters,
  128.     SAFEARRAY ** ppFilter,
  129.     DWORD * pdwNumElements
  130. ){
  131.     DWORD i;
  132.     HRESULT hr = S_OK;
  133.     SAFEARRAYBOUND sabNewArray;
  134.     SAFEARRAY * pFilter = NULL;
  135.     sabNewArray.cElements = dwMaxFilters;
  136.     sabNewArray.lLbound =  0;
  137.     pFilter =   SafeArrayCreate(
  138.                         VT_I4,
  139.                         1,
  140.                         &sabNewArray
  141.                         );
  142.     if (!pFilter){
  143.         hr = E_OUTOFMEMORY;
  144.         BAIL_ON_FAILURE(hr);
  145.     }
  146.     for (i = 0; i < dwMaxFilters; i++) {
  147.         hr = SafeArrayPutElement(
  148.                 pFilter,
  149.                 (long *)&i,
  150.                 (void *)&((pFilters + i)->dwFilterId)
  151.             );
  152.         BAIL_ON_FAILURE(hr);
  153.     }
  154.     *ppFilter = pFilter;
  155.     *pdwNumElements = dwMaxFilters;
  156.     RRETURN(S_OK);
  157. error:
  158.     if (pFilter) {
  159.         SafeArrayDestroy(pFilter);
  160.     }
  161.     *ppFilter = NULL;
  162.     *pdwNumElements = 0;
  163.     RRETURN(hr);
  164. }
  165. HRESULT
  166. BuildObjectArray(
  167.     VARIANT var,
  168.     SAFEARRAY ** ppFilter,
  169.     DWORD * pdwNumElements
  170. ){
  171.     LONG uDestCount = 0;
  172.     LONG dwSLBound = 0;
  173.     LONG dwSUBound = 0;
  174.     VARIANT v;
  175.     LONG i;
  176.     HRESULT hr = S_OK;
  177.     SAFEARRAYBOUND sabNewArray;
  178.     DWORD dwFilterId;
  179.     SAFEARRAY * pFilter = NULL;
  180.     if(!((V_VT(&var) &  VT_VARIANT) &&  V_ISARRAY(&var))) {
  181.         RRETURN(E_FAIL);
  182.     }
  183.     //
  184.     // Check that there is only one dimension in this array
  185.     //
  186.     if ((V_ARRAY(&var))->cDims != 1) {
  187.         hr = E_FAIL;
  188.         BAIL_ON_FAILURE(hr);
  189.     }
  190.     //
  191.     // Check that there is atleast one element in this array
  192.     //
  193.     if ((V_ARRAY(&var))->rgsabound[0].cElements == 0){
  194.         hr = E_FAIL;
  195.         BAIL_ON_FAILURE(hr);
  196.     }
  197.     //
  198.     // We know that this is a valid single dimension array
  199.     //
  200.     hr = SafeArrayGetLBound(V_ARRAY(&var),
  201.                             1,
  202.                             (long FAR *)&dwSLBound
  203.                             );
  204.     BAIL_ON_FAILURE(hr);
  205.     hr = SafeArrayGetUBound(V_ARRAY(&var),
  206.                             1,
  207.                             (long FAR *)&dwSUBound
  208.                             );
  209.     BAIL_ON_FAILURE(hr);
  210.     sabNewArray.cElements = dwSUBound - dwSLBound + 1;
  211.     sabNewArray.lLbound = dwSLBound;
  212.     pFilter = SafeArrayCreate(
  213.                     VT_I4,
  214.                     1,
  215.                     &sabNewArray
  216.                     );
  217.     if (!pFilter) {
  218.         hr = E_OUTOFMEMORY;
  219.         BAIL_ON_FAILURE(hr);
  220.     }
  221.     for (i = dwSLBound; i <= dwSUBound; i++) {
  222.         VariantInit(&v);
  223.         hr = SafeArrayGetElement(V_ARRAY(&var),
  224.                                 (long FAR *)&i,
  225.                                 &v
  226.                                 );
  227.         if (FAILED(hr)) {
  228.             continue;
  229.         }
  230.         hr = IsValidFilter(
  231.                 V_BSTR(&v),
  232.                 &dwFilterId,
  233.                 gpFilters,
  234.                 gdwMaxFilters
  235.                 );
  236.         if (FAILED(hr)) {
  237.             continue;
  238.         }
  239.         hr = SafeArrayPutElement(
  240.                 pFilter,
  241.                 (long*)&uDestCount,
  242.                 (void *)&dwFilterId
  243.                 );
  244.         if(FAILED(hr)){
  245.             continue;
  246.         }
  247.         uDestCount++;
  248.     }
  249.     //
  250.     // There was nothing of value that could be retrieved from the
  251.     // filter.
  252.     //
  253.     if (!uDestCount ) {
  254.         hr = E_FAIL;
  255.         BAIL_ON_FAILURE(hr);
  256.     }
  257.     *pdwNumElements  = uDestCount;
  258.     *ppFilter = pFilter;
  259.     RRETURN(S_OK);
  260. error:
  261.     if (pFilter) {
  262.         SafeArrayDestroy(pFilter);
  263.     }
  264.     *ppFilter = NULL;
  265.     *pdwNumElements = 0;
  266.     RRETURN(hr);
  267. }