Selecting ltmmCapture Object Processors for C++

The following example shows how to enumerate and select ltmmCapture object processors.

#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
//
// BuildProcessorList
// fills the registered processor listbox to match the contents of the collection
//
// pProcessors = processor collection's interface pointer
// hwndListBox = listbox window handle
//
void BuildProcessorList(IltmmProcessors* pProcessors, HWND hwndListBox)
{
   long i;
   long count;
   // reset the listbox contents
   SendMessage(hwndListBox, LB_RESETCONTENT, 0, 0);
   // get the collection's item count
   pProcessors->get_Count(&count);
   for(i = 0; i < count; i++)
   {
      IltmmProcessor* pProcessor;
      BSTR bstr;
      TCHAR sz[256];
      int index;
      // retrieve collection item
      pProcessors->Item(i, &pProcessor);
      // get displayable name
      pProcessor->get_FriendlyName(&bstr);
      // convert 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);
      // free the collection item
      pProcessor->Release();
   }
}
//
// BuildSelectedProcessorList
// fills the selected processor listbox to match the contents of the collection
//
// pSelProcessors = selected processor collection's interface pointer
// hwndSelListBox = selected listbox window handle
//
void BuildSelectedProcessorList(IltmmProcessors* pSelProcessors, HWND hwndSelListBox)
{
   long i;
   long count;
   // reset the listbox contents
   SendMessage(hwndSelListBox, LB_RESETCONTENT, 0, 0);
   // get the collection's item count
   pSelProcessors->get_Count(&count);
   for(i = 0; i < count; i++)
   {  
      IltmmProcessor* pProcessor;
      BSTR bstr;
      TCHAR sz[256];
      // retrieve collection item
      pSelProcessors->Item(i, &pProcessor);
      // get displayable name
      pProcessor->get_FriendlyName(&bstr);
      // convert from unicode
      _stprintf(sz, _T("%ls"), bstr);
      // free name
      SysFreeString(bstr);
      // add the name to the listbox
      SendMessage(hwndSelListBox, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) sz);
      // free the collection item
      pProcessor->Release();
   }
   // add a blank entry at the end of the list and select it
   SendMessage(hwndSelListBox, LB_ADDSTRING, 0, (LPARAM) (LPVOID) _T(""));
   SendMessage(hwndSelListBox, LB_SETCURSEL, count, 0);
}
//
// RemoveSelectedProcessor
// removes the currently selected processor
//
// pSelProcessors = selected processor collection's interface pointer
// hwndSelListBox = selected listbox window handle
//
void RemoveSelectedProcessor(IltmmProcessors* pSelProcessors, HWND hwndSelListBox)
{
   int index;
   int count;
   // get the number of selected processor listbox items
   count = (int) SendMessage(hwndSelListBox, LB_GETCOUNT, 0, 0);
   // get the deletion point from the selected processor listbox
   index = (int) SendMessage(hwndSelListBox, LB_GETCURSEL, 0, 0);
   // if not a valid processor, then return
   if(index < 0 || index >= (count - 1))
      return;
   // remove the processor from the selected processor collection
   pSelProcessors->Remove(index);
   // remove the name from the the selected processor listbox
   SendMessage(hwndSelListBox, LB_DELETESTRING, index, 0);
   // select the next item in the listbox
   SendMessage(hwndSelListBox, LB_SETCURSEL, index, 0);
}
//
// RemoveAllSelectedProcessors
// removes all selected processors
//
// pSelProcessors = selected processor collection's interface pointer
// hwndSelListBox = selected listbox window handle
//
void RemoveAllSelectedProcessors(IltmmProcessors* pSelProcessors, HWND hwndSelListBox)
{
   pSelProcessors->RemoveAll();
   BuildSelectedProcessorList(pSelProcessors, hwndSelListBox);
}
//
// InsertSelectedProcessor
// inserts the currently selected registerd processor into te selected processor collection
//
// pProcessors = registered processor collection's interface pointer
// hwndListBox = registered listbox window handle
// pSelProcessors = selected processor collection's interface pointer
// hwndSelListBox = selected listbox window handle
//
void InsertSelectedProcessor(IltmmProcessors* pProcessors, HWND hwndListBox,
                             IltmmProcessors* pSelProcessors, HWND hwndSelListBox)
{
   int index;
   int selindex;
   long item;
   IltmmProcessor* pProcessor;
   BSTR bstr;
   TCHAR sz[256];
   // get the selection from the registered processor listbox
   index = (int) SendMessage(hwndListBox, LB_GETCURSEL, 0, 0);
   if(index < 0)
      return;
   // get the collection item index
   item = (long) SendMessage(hwndListBox, LB_GETITEMDATA, index, 0);
   // get the actual item
   pProcessors->Item(item, &pProcessor);
   // get the insertion point from the selected processor listbox
   selindex = (int) SendMessage(hwndSelListBox, LB_GETCURSEL, 0, 0);
   // insert the processor into the selected processor collection
   pSelProcessors->Add(pProcessor, selindex);
   // get the displayable name
   pProcessor->get_FriendlyName(&bstr);
   // release the processor
   pProcessor->Release();
   // convert from unicode
   _stprintf(sz, _T("%ls"), bstr);
   // free name
   SysFreeString(bstr);
   // insert the name into the selected processor listbox
   SendMessage(hwndSelListBox, LB_INSERTSTRING, selindex, (LPARAM) (LPCTSTR) sz);
}
//
// RefreshProcessorList
// called to completely rebuild the collection
//
// pProcessors = processor collection's interface pointer
// hwndListBox = listbox window handle
//
void RefreshProcessorList(IltmmProcessors* pProcessors, HWND hwndListBox)
{
   IltmmProcessor* pProcessor;
   long item;
   BSTR bstr;
   long i;
   long count;
   // get the currently selected item
   i = (int) SendMessage(hwndListBox, LB_GETCURSEL, 0, 0);
   if(i >= 0)
   {  
      item = (long) SendMessage(hwndListBox, LB_GETITEMDATA, i, 0);
      // get it's unique name
      pProcessors->Item(item, &pProcessor);
      pProcessor->get_Name(&bstr);
      pProcessor->Release();
   }
   else
   {  
      bstr = NULL;
   }
   // rebuild the collection
   pProcessors->Refresh();
   // rebuild the listbox
   BuildProcessorList(pProcessors, hwndListBox);
   if(bstr)
   {  
      // find the previously selected item
      pProcessors->Find(bstr, &item);
      // make it the current selection
      if(item >= 0)
      {  
         pProcessors->get_Count(&count);
         // select the appropriate listbox item
         for(i = 0; i < count; i++)
         {  
            if(item == (long) SendMessage(hwndListBox, LB_GETITEMDATA, i, 0))
            {  
               SendMessage(hwndListBox, LB_SETCURSEL, i, 0);
               break;
            }  
         }  
      }  
      SysFreeString(bstr);
   }
}
//
// ProcessorSelectionChanged
// called when the registered processor selection has changed
//
// pProcessors = processor collection's interface pointer
// hwndListBox = listbox window handle
// hwndAddBtn = add button window handle
//
void ProcessorSelectionChanged(IltmmProcessors* pProcessors, HWND hwndListBox, HWND hwndAddBtn)
{
   int index;
   // get the currently selected listbox item
   index = (int) SendMessage(hwndListBox, LB_GETCURSEL, 0, 0);
   // enable add, if a filter is selected
   EnableWindow(hwndAddBtn, index >= 0);
}
//
// SelectedProcessorSelectionChanged
// called when the selected processor selection has changed
//
// pSelProcessors = processor collection's interface pointer
// hwndSelListBox = listbox window handle
// hwndRemoveBtn = remove button window handle
// hwndRemoveAllBtn = remove all button window handle
// hwndPropBtn = properties button window handle
//
void SelectedProcessorSelectionChanged(IltmmProcessors* pSelProcessors, HWND hwndSelListBox, HWND hwndRemoveBtn, HWND hwndRemoveAllBtn, HWND hwndPropBtn)
{
   int index;
   int count;
   BOOL fFilter;
   VARIANT_BOOL fHasDialog = VARIANT_FALSE;
   HRESULT hr;
   // get the currently selected listbox item
   index = (int) SendMessage(hwndSelListBox, LB_GETCURSEL, 0, 0);
   // get the total item count
   count = (int) SendMessage(hwndSelListBox, LB_GETCOUNT, 0, 0);
   // is this a filter?
   fFilter = (index >= 0 && index < (count - 1));
   // enable remove all button, if there is a filter in the list
   EnableWindow(hwndRemoveAllBtn, count > 1);
   // enable remove button, if it is a filter
   EnableWindow(hwndRemoveBtn, fFilter);
   if(fFilter)
   {  
      // check for the availablility of a filter property dialog box
      IltmmProcessor* pProcessor;
      hr = pSelProcessors->Item(index, &pProcessor);
      if(SUCCEEDED(hr))
      {  
         pProcessor->HasDialog(ltmmProcessor_Dlg_Properties, &fHasDialog);
         pProcessor->Release();
      }   
   }
   else
   {  
      fHasDialog = VARIANT_FALSE;
   }
   // enable properties button, if dialog box is available
   EnableWindow(hwndPropBtn, fHasDialog != VARIANT_FALSE);
}
//
// InvokeProcessorProperties
// invokes the currently selected processor's properties dialog box
//
// pSelProcessors = processor collection's interface pointer
// hwndSelListBox = listbox window handle
// hwndParent = parent window handle
//
void InvokeProcessorProperties(IltmmProcessors* pSelProcessors, HWND hwndSelListBox, HWND hwndParent)
{
   int index;
   int count;
   IltmmProcessor* pProcessor;
   HRESULT hr;
   // get the currently selected listbox item
   index = (int) SendMessage(hwndSelListBox, LB_GETCURSEL, 0, 0);
   // get the total item count
   count = (int) SendMessage(hwndSelListBox, LB_GETCOUNT, 0, 0);
   // is this a filter?
   if(index >= 0 && index < (count - 1))
   {  
      // invoke the processor's dialog
      hr = pSelProcessors->Item(index, &pProcessor);
      if(SUCCEEDED(hr))
      {  
         pProcessor->ShowDialog(ltmmProcessor_Dlg_Properties, (long) hwndParent);
         pProcessor->Release();
      }
   }
}
//
// ProcessorDlgProc
// selects object processors
//
BOOL CALLBACK ProcessorDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   IltmmProcessors* pProcessors;
   IltmmProcessors* pSelProcessors;
   switch (msg)
   {
   case WM_INITDIALOG:
      // initialize the audio processor listbox
      g_pCapture->get_AudioProcessors(&pProcessors);
      BuildProcessorList(pProcessors, GetDlgItem(hwnd, IDC_AUDIOPROCESSORS));
      ProcessorSelectionChanged(pProcessors, GetDlgItem(hwnd, IDC_AUDIOPROCESSORS),
         GetDlgItem(hwnd, IDC_ADDAUDIOPROCESSOR));
      pProcessors->Release();
      // initialize the selected audio processor listbox
      g_pCapture->get_SelectedAudioProcessors(&pSelProcessors);
      BuildSelectedProcessorList(pSelProcessors, GetDlgItem(hwnd, IDC_SELAUDIOPROCESSORS));
      SelectedProcessorSelectionChanged(pSelProcessors, GetDlgItem(hwnd, IDC_SELAUDIOPROCESSORS),
         GetDlgItem(hwnd, IDC_REMOVEAUDIOPROCESSOR),
         GetDlgItem(hwnd, IDC_REMOVEALLAUDIOPROCESSORS),
         GetDlgItem(hwnd, IDC_AUDIOPROCESSORPROPERTIES));
      pSelProcessors->Release();
      // initialize the video processor listbox
      g_pCapture->get_VideoProcessors(&pProcessors);
      BuildProcessorList(pProcessors, GetDlgItem(hwnd, IDC_VIDEOPROCESSORS));
      ProcessorSelectionChanged(pProcessors, GetDlgItem(hwnd, IDC_VIDEOPROCESSORS),
         GetDlgItem(hwnd, IDC_ADDVIDEOPROCESSOR));
      pProcessors->Release();
      // initialize the selected video processor listbox
      g_pCapture->get_SelectedVideoProcessors(&pSelProcessors);
      BuildSelectedProcessorList(pSelProcessors, GetDlgItem(hwnd, IDC_SELVIDEOPROCESSORS));
      SelectedProcessorSelectionChanged(pSelProcessors, GetDlgItem(hwnd, IDC_SELVIDEOPROCESSORS),
         GetDlgItem(hwnd, IDC_REMOVEVIDEOPROCESSOR),
         GetDlgItem(hwnd, IDC_REMOVEALLVIDEOPROCESSORS),
         GetDlgItem(hwnd, IDC_VIDEOPROCESSORPROPERTIES));
      pSelProcessors->Release();
      return TRUE;
      break;
   case WM_COMMAND:
      switch(LOWORD(wParam))
      {  
      case IDC_ADDAUDIOPROCESSOR:
         // insert processor into selected audio processor collection

         g_pCapture->get_AudioProcessors(&pProcessors);
         g_pCapture->get_SelectedAudioProcessors(&pSelProcessors);
         InsertSelectedProcessor(pProcessors, GetDlgItem(hwnd, IDC_AUDIOPROCESSORS),
            pSelProcessors, GetDlgItem(hwnd, IDC_SELAUDIOPROCESSORS));
         SelectedProcessorSelectionChanged(pSelProcessors, GetDlgItem(hwnd, IDC_SELAUDIOPROCESSORS),
            GetDlgItem(hwnd, IDC_REMOVEAUDIOPROCESSOR),
            GetDlgItem(hwnd, IDC_REMOVEALLAUDIOPROCESSORS),
            GetDlgItem(hwnd, IDC_AUDIOPROCESSORPROPERTIES));
         pSelProcessors->Release();

         pProcessors->Release();

         return TRUE;
         break;
      case IDC_REMOVEAUDIOPROCESSOR:
         // remove processor from the selected audio processor collection

         g_pCapture->get_SelectedAudioProcessors(&pSelProcessors);
         RemoveSelectedProcessor(pSelProcessors, GetDlgItem(hwnd, IDC_SELAUDIOPROCESSORS));
         SelectedProcessorSelectionChanged(pSelProcessors, GetDlgItem(hwnd, IDC_SELAUDIOPROCESSORS),
            GetDlgItem(hwnd, IDC_REMOVEAUDIOPROCESSOR),
            GetDlgItem(hwnd, IDC_REMOVEALLAUDIOPROCESSORS),
            GetDlgItem(hwnd, IDC_AUDIOPROCESSORPROPERTIES));
         pSelProcessors->Release();

         return TRUE;
         break;
      case IDC_REMOVEALLAUDIOPROCESSORS:
         // remove all selected audio processors

         g_pCapture->get_SelectedAudioProcessors(&pSelProcessors);
         RemoveAllSelectedProcessors(pSelProcessors, GetDlgItem(hwnd, IDC_SELAUDIOPROCESSORS));
         SelectedProcessorSelectionChanged(pSelProcessors, GetDlgItem(hwnd, IDC_SELAUDIOPROCESSORS),
            GetDlgItem(hwnd, IDC_REMOVEAUDIOPROCESSOR),
            GetDlgItem(hwnd, IDC_REMOVEALLAUDIOPROCESSORS),
            GetDlgItem(hwnd, IDC_AUDIOPROCESSORPROPERTIES));
         pSelProcessors->Release();

         return TRUE;
         break;
      case IDC_REFRESHAUDIOPROCESSORS:
         // rebuild the registered audio processor collection

         g_pCapture->get_AudioProcessors(&pProcessors);
         RefreshProcessorList(pProcessors, GetDlgItem(hwnd, IDC_AUDIOPROCESSORS));
         ProcessorSelectionChanged(pProcessors, GetDlgItem(hwnd, IDC_AUDIOPROCESSORS),
            GetDlgItem(hwnd, IDC_ADDAUDIOPROCESSOR));
         pProcessors->Release();

         return TRUE;
         break;
      case IDC_AUDIOPROCESSORPROPERTIES:
         // invoke the selected audio processor's properties dialog box

         g_pCapture->get_SelectedAudioProcessors(&pSelProcessors);
         InvokeProcessorProperties(pSelProcessors, GetDlgItem(hwnd, IDC_SELAUDIOPROCESSORS), hwnd);
         pSelProcessors->Release();

         return TRUE;
         break;
      case IDC_SELAUDIOPROCESSORS:
         if(HIWORD(wParam) == LBN_SELCHANGE)
         {  
            // selection changed, update buttons

            g_pCapture->get_SelectedAudioProcessors(&pSelProcessors);
            SelectedProcessorSelectionChanged(pSelProcessors, GetDlgItem(hwnd, IDC_SELAUDIOPROCESSORS),
               GetDlgItem(hwnd, IDC_REMOVEAUDIOPROCESSOR),
               GetDlgItem(hwnd, IDC_REMOVEALLAUDIOPROCESSORS),
               GetDlgItem(hwnd, IDC_AUDIOPROCESSORPROPERTIES));
            pSelProcessors->Release();

         }  
         break;
      case IDC_AUDIOPROCESSORS:
         if(HIWORD(wParam) == LBN_SELCHANGE)
         {  
            // selection changed, update buttons

            g_pCapture->get_AudioProcessors(&pProcessors);
            ProcessorSelectionChanged(pProcessors, GetDlgItem(hwnd, IDC_AUDIOPROCESSORS),
               GetDlgItem(hwnd, IDC_ADDAUDIOPROCESSOR));
            pProcessors->Release();

         }  
         break;
      case IDC_ADDVIDEOPROCESSOR:
         // insert processor into selected video processor collection

         g_pCapture->get_VideoProcessors(&pProcessors);
         g_pCapture->get_SelectedVideoProcessors(&pSelProcessors);
         InsertSelectedProcessor(pProcessors, GetDlgItem(hwnd, IDC_VIDEOPROCESSORS),
            pSelProcessors, GetDlgItem(hwnd, IDC_SELVIDEOPROCESSORS));
         SelectedProcessorSelectionChanged(pSelProcessors, GetDlgItem(hwnd, IDC_SELVIDEOPROCESSORS),
            GetDlgItem(hwnd, IDC_REMOVEVIDEOPROCESSOR),
            GetDlgItem(hwnd, IDC_REMOVEALLVIDEOPROCESSORS),
            GetDlgItem(hwnd, IDC_VIDEOPROCESSORPROPERTIES));
         pSelProcessors->Release();

         pProcessors->Release();

         return TRUE;
         break;
      case IDC_REMOVEVIDEOPROCESSOR:
         // remove processor from the selected video processor collection

         g_pCapture->get_SelectedVideoProcessors(&pSelProcessors);
         RemoveSelectedProcessor(pSelProcessors, GetDlgItem(hwnd, IDC_SELVIDEOPROCESSORS));
         SelectedProcessorSelectionChanged(pSelProcessors, GetDlgItem(hwnd, IDC_SELVIDEOPROCESSORS),
            GetDlgItem(hwnd, IDC_REMOVEVIDEOPROCESSOR),
            GetDlgItem(hwnd, IDC_REMOVEALLVIDEOPROCESSORS),
            GetDlgItem(hwnd, IDC_VIDEOPROCESSORPROPERTIES));
         pSelProcessors->Release();

         return TRUE;
         break;
      case IDC_REMOVEALLVIDEOPROCESSORS:
         // remove all selected video processors

         g_pCapture->get_SelectedVideoProcessors(&pSelProcessors);
         RemoveAllSelectedProcessors(pSelProcessors, GetDlgItem(hwnd, IDC_SELVIDEOPROCESSORS));
         SelectedProcessorSelectionChanged(pSelProcessors, GetDlgItem(hwnd, IDC_SELVIDEOPROCESSORS),
            GetDlgItem(hwnd, IDC_REMOVEVIDEOPROCESSOR),
            GetDlgItem(hwnd, IDC_REMOVEALLVIDEOPROCESSORS),
            GetDlgItem(hwnd, IDC_VIDEOPROCESSORPROPERTIES));
         pSelProcessors->Release();

         return TRUE;
         break;
      case IDC_REFRESHVIDEOPROCESSORS:
         // rebuild the registered video processor collection

         g_pCapture->get_VideoProcessors(&pProcessors);
         RefreshProcessorList(pProcessors, GetDlgItem(hwnd, IDC_VIDEOPROCESSORS));
         ProcessorSelectionChanged(pProcessors, GetDlgItem(hwnd, IDC_VIDEOPROCESSORS),
            GetDlgItem(hwnd, IDC_ADDVIDEOPROCESSOR));
         pProcessors->Release();

         return TRUE;
         break;
      case IDC_VIDEOPROCESSORPROPERTIES:
         // invoke the selected video processor's properties dialog box

         g_pCapture->get_SelectedVideoProcessors(&pSelProcessors);
         InvokeProcessorProperties(pSelProcessors, GetDlgItem(hwnd, IDC_SELVIDEOPROCESSORS), hwnd);
         pSelProcessors->Release();

         return TRUE;
         break;
      case IDC_SELVIDEOPROCESSORS:
         if(HIWORD(wParam) == LBN_SELCHANGE)
         {  
            // selection changed, update buttons

            g_pCapture->get_SelectedVideoProcessors(&pSelProcessors);
            SelectedProcessorSelectionChanged(pSelProcessors, GetDlgItem(hwnd, IDC_SELVIDEOPROCESSORS),
               GetDlgItem(hwnd, IDC_REMOVEVIDEOPROCESSOR),
               GetDlgItem(hwnd, IDC_REMOVEALLVIDEOPROCESSORS),
               GetDlgItem(hwnd, IDC_VIDEOPROCESSORPROPERTIES));
            pSelProcessors->Release();

         }  
         break;
      case IDC_VIDEOPROCESSORS:
         if(HIWORD(wParam) == LBN_SELCHANGE)
         {  
            // selection changed, update buttons

            g_pCapture->get_VideoProcessors(&pProcessors);
            ProcessorSelectionChanged(pProcessors, GetDlgItem(hwnd, IDC_VIDEOPROCESSORS),
               GetDlgItem(hwnd, IDC_ADDVIDEOPROCESSOR));
            pProcessors->Release();

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

      }
      break;
   }
   return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpCmdLine,
                     int nCmdShow)
{
   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_PROCESSORDLG, NULL, ProcessorDlgProc); error:
   // cleanup
   if(g_pCapture)
      g_pCapture->Release();
   CoUninitialize();
   return 0; }