IltmmVideoFormats::get_Count Example for C++

HRESULT GetCaptureVideoFormatCount(IltmmCapture *pCapture)
{
   HRESULT        hr;
   long           lIndex, lCount, lSelection, lWidth, lHeight, lCompression, lBitCount;
   VARIANT_BOOL   vSelected;
   BSTR           SubTypeName, SubTypeFriendlyName, Subtype;

   IltmmVideoFormats* pVidCapFormats = NULL;
   IltmmVideoFormat*  pVidFormat = NULL;
   
   // try to get the capture video formats
   hr = pCapture->get_VideoCaptureFormats(&pVidCapFormats);

   if (FAILED(hr))
      return hr;

   // get the count here
   hr = pVidCapFormats->get_Count(&lCount);
   if (FAILED(hr))
   {
      // release the capture formats object
      pVidCapFormats->Release();
      return hr;
   }

   // get the selected format
   hr = pVidCapFormats->get_Selection(&lSelection);
   if (FAILED(hr))
   {
      // release the capture formats object
      pVidCapFormats->Release();
      return hr;
   }

   // if the first format is not selected, select it
   if (lSelection != 0)
   {
      hr = pVidCapFormats->put_Selection(0);
      if (FAILED(hr))
      {
         // release the capture formats object
         pVidCapFormats->Release();
         return hr;
      }
   }

   // get the first format object
   hr = pVidCapFormats->Item(0, &pVidFormat);
   if (FAILED(hr))
   {
      // release the capture formats object
      pVidCapFormats->Release();
      return hr;
   }

   // check it's selected state
   hr = pVidFormat->get_Selected(&vSelected);
   if (FAILED(hr))
   {
      // release the capture formats and selected format object
      pVidCapFormats->Release();
      pVidFormat->Release();
      return hr;
   }

   // if it is not selected, select it
   if (vSelected == VARIANT_FALSE)
   {
      hr = pVidFormat->put_Selected(VARIANT_TRUE);
      if (FAILED(hr))
      {
         // release the capture formats and selected format object
         pVidCapFormats->Release();
         pVidFormat->Release();
         return hr;
      }
   }

   // get the format's subtype name
   hr = pVidFormat->get_SubTypeName(&SubTypeName);
   if (FAILED(hr))
   {
      // release the capture formats and selected format object
      pVidCapFormats->Release();
      pVidFormat->Release();
      return hr;
   }

   // do something with the subtype name bstr and then free it
   SysFreeString(SubTypeName);

   // get the format's friendly name
   hr = pVidFormat->get_SubTypeFriendlyName(&SubTypeFriendlyName);
   if (FAILED(hr))
   {
      // release the capture formats and selected format object
      pVidCapFormats->Release();
      pVidFormat->Release();
      return hr;
   }

   // do something with the friendly name bstr and then free it
   SysFreeString(SubTypeFriendlyName);
   
   // get the format's width
   hr = pVidFormat->get_Width(&lWidth);
   if (FAILED(hr))
   {
      // release the capture formats and selected format object
      pVidCapFormats->Release();
      pVidFormat->Release();
      return hr;
   }

   // get the format's height
   hr = pVidFormat->get_Height(&lHeight);
   if (FAILED(hr))
   {
      // release the capture formats and selected format object
      pVidCapFormats->Release();
      pVidFormat->Release();
      return hr;
   }
   // get the format's compression
   hr = pVidFormat->get_Compression(&lCompression);
   if (FAILED(hr))
   {
      // release the capture formats and selected format object
      pVidCapFormats->Release();
      pVidFormat->Release();
      return hr;
   }
   // get the format's bit count
   hr = pVidFormat->get_BitCount(&lBitCount);
   if (FAILED(hr))
   {
      // release the capture formats and selected format object
      pVidCapFormats->Release();
      pVidFormat->Release();
      return hr;
   }
   
   Subtype = SysAllocString(ltmmMEDIASUBTYPE_RGB24);
   // find a video format with subtype RGB24, 1024x768
   hr = pVidCapFormats->Find(Subtype, 1024, 768, &lIndex);
   SysFreeString(Subtype);
   
   if (FAILED(hr))
   {
      // release the capture formats and selected format object
      pVidCapFormats->Release();
      pVidFormat->Release();
      return hr;
   }

   // release the capture formats and selected format object
   pVidCapFormats->Release();
   pVidFormat->Release();

   return S_OK;
}