Selecting ltmmCapture Object Target Formats for C

The following code demonstrates how to enumerate, add, and select ltmmCapture object target formats:

#ifndef COBJMACROS
   #define COBJMACROS
#endif

#include "ltmm.h"
#include "resource.h"
#include "tchar.h"
#include "stdio.h"

HINSTANCE g_hInstance; // application instance handle
IltmmCapture* g_pCapture; // capture object's interface pointer

//
// BuildTargetFormatList
// fills a listbox to match the contents of the collection
//
// pTargetFormats = format collection's interface pointer
// hwndListBox = listbox window handle
//
void BuildTargetFormatList(IltmmTargetFormats* pTargetFormats, HWND hwndListBox) 
{
   long i; 
   long count; 
   int selected = -1; 

   // reset the listbox contents
   SendMessage(hwndListBox, LB_RESETCONTENT, 0, 0); 

   // get the collection's item count
   IltmmTargetFormats_get_Count (pTargetFormats, &count); 

   for(i = 0; i < count; i++)
   {
      IltmmTargetFormat* pTargetFormat; 
      BSTR bstr; 
      TCHAR sz[256]; 
      int index; 
      VARIANT_BOOL f; 

      // retrieve collection item
      IltmmTargetFormats_Item (pTargetFormats, i, &pTargetFormat); 

      // get displayable name
      IltmmTargetFormat_get_Name(pTargetFormat, &bstr); 

      // capture from unicode
      _stprintf(sz, _T("%ls"), bstr); 

      // free name
      SysFreeString(bstr); 

      // add the name to the listbox
      index = SendMessage(hwndListBox, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) sz); 

      // associate the listbox item with the collection item index
      SendMessage(hwndListBox, LB_SETITEMDATA, index, i); 

      // remember this listbox item, if it is selected in the collection
      IltmmTargetFormat_get_Selected(pTargetFormat, &f); 
      if(f) 
         selected = i; 

      // free the collection item
      IUnknown_Release(pTargetFormat); 

   }

   // select the appropriate listbox item
   for(i = 0; i < count; i++)
   {
      if(selected == (long) SendMessage(hwndListBox, LB_GETITEMDATA, i, 0)) 
      {
         SendMessage(hwndListBox, LB_SETCURSEL, i, 0); 
         break; 
      }
   }

}

//
// TargetFormatSelectionChanged
// called to reflect changes in the listbox selection
//
// pTargetFormats = format collection's interface pointer
// hwndListBox = listbox window handle
//
void TargetFormatSelectionChanged(IltmmTargetFormats* pTargetFormats, HWND hwndListBox) 
{
   int index; 
   long item; 
   int i; 
   int count; 
   HRESULT hr; 

   // get the index of current listbox selection
   index = (int) SendMessage(hwndListBox, LB_GETCURSEL, 0, 0); 

   if(index < 0) 
   {
      // clear selection
      IltmmTargetFormats_put_Selection(pTargetFormats, -1); 
   }
   else
   {

      // get the collection item index
      item = (long) SendMessage(hwndListBox, LB_GETITEMDATA, index, 0); 

      // select the item
#ifdef _DEBUG
      {
         IltmmTargetFormat* pTargetFormat; 
         IltmmTargetFormats_Item (pTargetFormats, item, &pTargetFormat); 
         hr = IltmmTargetFormat_put_Selected(pTargetFormat, VARIANT_TRUE); 
         IUnknown_Release(pTargetFormat); 
      }
#else
      hr = IltmmTargetFormats_put_Selection(pTargetFormats, item); 
#endif
      // if new selection failed, we need to force the listbox to match the actual selection
      if(FAILED(hr)) 
      {
         // get the real selection
         IltmmTargetFormats_get_Selection (pTargetFormats, &item);   

         // find the matching listbox item
         count = (int) SendMessage(hwndListBox, LB_GETCOUNT, 0, 0); 

         for(i = 0; i < count; i++)
         {
            if(item == (long) SendMessage(hwndListBox, LB_GETITEMDATA, i, 0)) 
            {
               SendMessage(hwndListBox, LB_SETCURSEL, i, 0); 
               break; 
            }
         }

      }

   }
#ifdef _DEBUG
   {
      long item; 
      IltmmTargetFormats_get_Selection (pTargetFormats, &item); 
      if(item >= 0) 
      {
         IltmmTargetFormat* pTargetFormat; 
         TCHAR sz[2048]; 
         LPTSTR p; 
         VARIANT_BOOL b; 
         BSTR bstr; 

         sz[0] = _T('\0'); 
         p = sz; 
         IltmmTargetFormats_Item (pTargetFormats, item, &pTargetFormat); 


         IltmmTargetFormat_get_AudioCompressor(pTargetFormat, &bstr); 
         p += _stprintf(p, _T("AComp = %ls"), bstr); 
         SysFreeString(bstr); 

         IltmmTargetFormat_get_VideoCompressor(pTargetFormat, &bstr); 
         p += _stprintf(p, _T(", VComp = %ls"), bstr); 
         SysFreeString(bstr); 

         IltmmTargetFormat_get_AVMux(pTargetFormat, &bstr); 
         p += _stprintf(p, _T(", AVMux = %ls"), bstr); 
         SysFreeString(bstr); 

         IltmmTargetFormat_get_Mux(pTargetFormat, &bstr); 
         p += _stprintf(p, _T(", Mux = %ls"), bstr); 
         SysFreeString(bstr); 

         IltmmTargetFormat_get_SinkSubType (pTargetFormat, &bstr); 
         p += _stprintf(p, _T(", SubType = %ls"), bstr); 
         SysFreeString(bstr); 

         IltmmTargetFormat_get_Sink(pTargetFormat, &bstr); 
         p += _stprintf(p, _T(", Sink = %ls"), bstr); 
         SysFreeString(bstr); 


         IltmmTargetFormat_get_ReadOnly(pTargetFormat, &b); 
         p += _stprintf(p, _T(", ReadOnly = %s"), b != 0 ? _T("Yes") : _T("No"));


         IUnknown_Release(pTargetFormat); 

         MessageBox(NULL, sz, _T("Target Format"), MB_OK); 
      }
   }

#endif
}

//
// AddCustomTargetFormats
// called to append custom target formats
//
// pTargetFormats = format collection's interface pointer
//
void AddCustomTargetFormats(IltmmTargetFormats* pTargetFormats) 
{
   BSTR bstr; 
   long index; 
   IltmmTargetFormat* pTargetFormat; 

   // remove all custom formats
#ifdef _DEBUG
   IltmmTargetFormats_get_Count (pTargetFormats, &index); 
   IltmmTargetFormats_Remove(pTargetFormats, index - 1); 
#else
   IltmmTargetFormats_RemoveAll(pTargetFormats); 
#endif

   bstr = SysAllocString(L"CUSTOM FORMAT 1");
   IltmmTargetFormats_Add(pTargetFormats, bstr, -1); 
   IltmmTargetFormats_Find(pTargetFormats, bstr, &index); 
   SysFreeString(bstr); 

   IltmmTargetFormats_Item(pTargetFormats, index, &pTargetFormat); 

   // assign an optional audio compressor
   // this case: MP3
   bstr = SysAllocString(L"@device:cm:{33D9A761-90C8-11D0-BD43-00A0C911CE86}\\85MPEG Layer-3");
   IltmmTargetFormat_put_AudioCompressor (pTargetFormat, bstr); 
   SysFreeString(bstr); 

   // assign an optional video compressor
   // this case: LEAD MCMP
   bstr = SysAllocString(L"@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\\LEAD MCMP/MJPEG Codec (2.0)");
   IltmmTargetFormat_put_VideoCompressor (pTargetFormat, bstr); 
   SysFreeString(bstr); 

   // assign an optional AVMux compressor
   // this case: no premux
   bstr = SysAllocString(L"");
   IltmmTargetFormat_put_AVMux(pTargetFormat, bstr); 
   SysFreeString(bstr); 

   // assign the required Mux
   // this case: AVI
   bstr = SysAllocString(L"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\\{E2510970-F137-11CE-8B67-00AA00A3F1A6}");
   IltmmTargetFormat_put_Mux(pTargetFormat, bstr); 
   SysFreeString(bstr); 

   // assign the optional SinkSubType
   // this case: MEDIASUBTYPE_Avi
   bstr = SysAllocString(L"{E436EB88-524F-11CE-9F53-0020AF0BA770}");
   IltmmTargetFormat_put_SinkSubType(pTargetFormat, bstr); 
   SysFreeString(bstr); 

   // assign an optional Sink
   // this case: default
   bstr = SysAllocString(L"");
   IltmmTargetFormat_put_Sink(pTargetFormat, bstr); 
   SysFreeString(bstr); 


   IUnknown_Release(pTargetFormat); 
}

//
// TargetFormatDlgProc
// selects object formats
//
BOOL CALLBACK TargetFormatDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{
   IltmmTargetFormats* pTargetFormats; 


   lParam = lParam ;

   switch (msg) 
   {
   case WM_INITDIALOG: 
      // initialize the target format listbox

      IltmmCapture_get_TargetFormats(g_pCapture, &pTargetFormats); 
      AddCustomTargetFormats(pTargetFormats); 
      BuildTargetFormatList(pTargetFormats, GetDlgItem(hwnd, IDC_TARGETFORMATS)); 
      IUnknown_Release(pTargetFormats); 

      return TRUE; 
      break; 

   case WM_COMMAND: 
      switch(LOWORD(wParam)) 
      {
      case IDC_TARGETFORMATS: 
         if(HIWORD(wParam) == LBN_SELCHANGE) 
         {
            // select the target format
            IltmmCapture_get_TargetFormats(g_pCapture, &pTargetFormats); 
            TargetFormatSelectionChanged(pTargetFormats, GetDlgItem(hwnd, IDC_TARGETFORMATS)); 
            IUnknown_Release(pTargetFormats); 
         }
         break; 

      case IDOK: 
         EndDialog(hwnd, IDOK); 
         return TRUE; 
         break; 
      }
      break; 
   }
   return FALSE; 

}

void SelectingltmmCaptureObjectTargetFormats_Example ( HINSTANCE hInstance )
{
   HRESULT hr; 

   g_hInstance = hInstance; 

   // initialize COM library
   hr = CoInitialize(NULL); 
   if(FAILED(hr)) 
      goto error; 

   // create the capture object
   hr = CoCreateInstance(&CLSID_ltmmCapture, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmCapture, (void**) &g_pCapture); 
   if(FAILED(hr)) 
      goto error; 


   DialogBox(g_hInstance, (LPCTSTR)IDD_TARGETFORMATDLG, NULL, TargetFormatDlgProc); 


error: 
   // cleanup

   if(g_pCapture) 
      IUnknown_Release(g_pCapture); 

   CoUninitialize();
}