The following example shows how to enumerate and select ltmmCapture object processors.
// define helper macros for using interfaces under C#ifndef COBJMACROS#define COBJMACROS#endif#include "ltmm.h"#include "resource.h"#include "tchar.h"#include "stdio.h"HINSTANCE g_hInstance; // application instance handleIltmmCapture* 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 contentsSendMessage(hwndListBox, LB_RESETCONTENT, 0, 0);// get the collection's item countIltmmProcessors_get_Count (pProcessors, &count);for(i = 0; i < count; i++){IltmmProcessor* pProcessor;BSTR bstr;TCHAR sz[256];int index;// retrieve collection itemIltmmProcessors_Item(pProcessors, i, &pProcessor);// get displayable nameIltmmProcessor_get_FriendlyName (pProcessor, &bstr);// convert from unicode_stprintf(sz, _T("%ls"), bstr);// free nameSysFreeString(bstr);// add the name to the listboxindex = SendMessage(hwndListBox, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) sz);// associate the listbox item with the collection item indexSendMessage(hwndListBox, LB_SETITEMDATA, index, i);// free the collection itemIUnknown_Release(pProcessor);}}//// 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 contentsSendMessage(hwndSelListBox, LB_RESETCONTENT, 0, 0);// get the collection's item countIltmmProcessors_get_Count (pSelProcessors, &count);for(i = 0; i < count; i++){IltmmProcessor* pProcessor;BSTR bstr;TCHAR sz[256];// retrieve collection itemIltmmProcessors_Item (pSelProcessors, i, &pProcessor);// get displayable nameIltmmProcessor_get_FriendlyName (pProcessor, &bstr);// convert from unicode_stprintf(sz, _T("%ls"), bstr);// free nameSysFreeString(bstr);// add the name to the listboxSendMessage(hwndSelListBox, LB_ADDSTRING, 0, (LPARAM) (LPCTSTR) sz);// free the collection itemIUnknown_Release(pProcessor);}// add a blank entry at the end of the list and select itSendMessage(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 itemscount = (int) SendMessage(hwndSelListBox, LB_GETCOUNT, 0, 0);// get the deletion point from the selected processor listboxindex = (int) SendMessage(hwndSelListBox, LB_GETCURSEL, 0, 0);// if not a valid processor, then returnif(index < 0 || index >= (count - 1))return;// remove the processor from the selected processor collectionIltmmProcessors_Remove (pSelProcessors, index);// remove the name from the selected processor listboxSendMessage(hwndSelListBox, LB_DELETESTRING, index, 0);// select the next item in the listboxSendMessage(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){IltmmProcessors_RemoveAll (pSelProcessors);BuildSelectedProcessorList(pSelProcessors, hwndSelListBox);}//// InsertSelectedProcessor// inserts the currently selected registerd processor into the 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 listboxindex = (int) SendMessage(hwndListBox, LB_GETCURSEL, 0, 0);if(index < 0)return;// get the collection item indexitem = (long) SendMessage(hwndListBox, LB_GETITEMDATA, index, 0);// get the actual itemIltmmProcessors_Item (pProcessors, item, &pProcessor);// get the insertion point from the selected processor listboxselindex = (int) SendMessage(hwndSelListBox, LB_GETCURSEL, 0, 0);// insert the processor into the selected processor collectionIltmmProcessors_Add (pSelProcessors, pProcessor, selindex);// get the displayable nameIltmmProcessor_get_FriendlyName (pProcessor, &bstr);// release the processorIUnknown_Release(pProcessor);// convert from unicode_stprintf(sz, _T("%ls"), bstr);// free nameSysFreeString(bstr);// insert the name into the selected processor listboxSendMessage(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 itemi = (int) SendMessage(hwndListBox, LB_GETCURSEL, 0, 0);if(i >= 0){item = (long) SendMessage(hwndListBox, LB_GETITEMDATA, i, 0);// get it's unique nameIltmmProcessors_Item (pProcessors, item, &pProcessor);IltmmProcessor_get_Name (pProcessor, &bstr);IUnknown_Release(pProcessor);}else{bstr = NULL;}// rebuild the collectionIltmmProcessors_Refresh (pProcessors);// rebuild the listboxBuildProcessorList(pProcessors, hwndListBox);if(bstr){// find the previously selected itemIltmmProcessors_Find(pProcessors, bstr, &item);// make it the current selectionif(item >= 0){IltmmProcessors_get_Count (pProcessors, &count);// select the appropriate listbox itemfor(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;pProcessors = pProcessors ;// get the currently selected listbox itemindex = (int) SendMessage(hwndListBox, LB_GETCURSEL, 0, 0);// enable add, if a filter is selectedEnableWindow(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 itemindex = (int) SendMessage(hwndSelListBox, LB_GETCURSEL, 0, 0);// get the total item countcount = (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 listEnableWindow(hwndRemoveAllBtn, count > 1);// enable remove button, if it is a filterEnableWindow(hwndRemoveBtn, fFilter);if(fFilter){// check for the availability of a filter property dialog boxIltmmProcessor* pProcessor;hr = IltmmProcessors_Item (pSelProcessors, index, &pProcessor);if(SUCCEEDED(hr)){IltmmProcessor_HasDialog (pProcessor, ltmmProcessor_Dlg_Properties, &fHasDialog);IUnknown_Release(pProcessor);}}else{fHasDialog = VARIANT_FALSE;}// enable properties button, if dialog box is availableEnableWindow(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 itemindex = (int) SendMessage(hwndSelListBox, LB_GETCURSEL, 0, 0);// get the total item countcount = (int) SendMessage(hwndSelListBox, LB_GETCOUNT, 0, 0);// is this a filter?if(index >= 0 && index < (count - 1)){// invoke the processor's dialoghr = IltmmProcessors_Item (pSelProcessors, index, &pProcessor);if(SUCCEEDED(hr)){IltmmProcessor_ShowDialog(pProcessor, ltmmProcessor_Dlg_Properties, (long) hwndParent);IUnknown_Release(pProcessor);}}}//// ProcessorDlgProc// selects object processors//BOOL CALLBACK ProcessorDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){IltmmProcessors* pProcessors;IltmmProcessors* pSelProcessors;wParam = wParam ;lParam = lParam ;switch (msg){case WM_INITDIALOG:// initialize the audio processor listboxIltmmCapture_get_AudioProcessors(g_pCapture, &pProcessors);BuildProcessorList(pProcessors, GetDlgItem(hwnd, IDC_AUDIOPROCESSORS));ProcessorSelectionChanged(pProcessors, GetDlgItem(hwnd, IDC_AUDIOPROCESSORS),GetDlgItem(hwnd, IDC_ADDAUDIOPROCESSOR));IUnknown_Release(pProcessors);// initialize the selected audio processor listboxIltmmCapture_get_SelectedAudioProcessors(g_pCapture, &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));IUnknown_Release(pSelProcessors);// initialize the video processor listboxIltmmCapture_get_VideoProcessors (g_pCapture, &pProcessors);BuildProcessorList(pProcessors, GetDlgItem(hwnd, IDC_VIDEOPROCESSORS));ProcessorSelectionChanged(pProcessors, GetDlgItem(hwnd, IDC_VIDEOPROCESSORS),GetDlgItem(hwnd, IDC_ADDVIDEOPROCESSOR));IUnknown_Release(pProcessors);// initialize the selected video processor listboxIltmmCapture_get_SelectedVideoProcessors(g_pCapture, &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));IUnknown_Release(pSelProcessors);return TRUE;break;case WM_COMMAND:switch(LOWORD(wParam)){case IDC_ADDAUDIOPROCESSOR:// insert processor into selected audio processor collectionIltmmCapture_get_AudioProcessors(g_pCapture, &pProcessors);IltmmCapture_get_SelectedAudioProcessors(g_pCapture, &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));IUnknown_Release(pSelProcessors);IUnknown_Release(pProcessors);return TRUE;break;case IDC_REMOVEAUDIOPROCESSOR:// remove processor from the selected audio processor collectionIltmmCapture_get_SelectedAudioProcessors(g_pCapture, &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));IUnknown_Release(pSelProcessors);return TRUE;break;case IDC_REMOVEALLAUDIOPROCESSORS:// remove all selected audio processorsIltmmCapture_get_SelectedAudioProcessors(g_pCapture, &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));IUnknown_Release(pSelProcessors);return TRUE;break;case IDC_REFRESHAUDIOPROCESSORS:// rebuild the registered audio processor collectionIltmmCapture_get_AudioProcessors(g_pCapture, &pProcessors);RefreshProcessorList(pProcessors, GetDlgItem(hwnd, IDC_AUDIOPROCESSORS));ProcessorSelectionChanged(pProcessors, GetDlgItem(hwnd, IDC_AUDIOPROCESSORS),GetDlgItem(hwnd, IDC_ADDAUDIOPROCESSOR));IUnknown_Release(pProcessors);return TRUE;break;case IDC_AUDIOPROCESSORPROPERTIES:// invoke the selected audio processor's properties dialog boxIltmmCapture_get_SelectedAudioProcessors(g_pCapture, &pSelProcessors);InvokeProcessorProperties(pSelProcessors, GetDlgItem(hwnd, IDC_SELAUDIOPROCESSORS), hwnd);IUnknown_Release(pSelProcessors);return TRUE;break;case IDC_SELAUDIOPROCESSORS:if(HIWORD(wParam) == LBN_SELCHANGE){// selection changed, update buttonsIltmmCapture_get_SelectedAudioProcessors(g_pCapture, &pSelProcessors);SelectedProcessorSelectionChanged(pSelProcessors, GetDlgItem(hwnd, IDC_SELAUDIOPROCESSORS),GetDlgItem(hwnd, IDC_REMOVEAUDIOPROCESSOR),GetDlgItem(hwnd, IDC_REMOVEALLAUDIOPROCESSORS),GetDlgItem(hwnd, IDC_AUDIOPROCESSORPROPERTIES));IUnknown_Release(pSelProcessors);}break;case IDC_AUDIOPROCESSORS:if(HIWORD(wParam) == LBN_SELCHANGE){// selection changed, update buttonsIltmmCapture_get_AudioProcessors(g_pCapture, &pProcessors);ProcessorSelectionChanged(pProcessors, GetDlgItem(hwnd, IDC_AUDIOPROCESSORS),GetDlgItem(hwnd, IDC_ADDAUDIOPROCESSOR));IUnknown_Release(pProcessors);}break;case IDC_ADDVIDEOPROCESSOR:// insert processor into selected video processor collectionIltmmCapture_get_VideoProcessors (g_pCapture, &pProcessors);IltmmCapture_get_SelectedVideoProcessors (g_pCapture, &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));IUnknown_Release(pSelProcessors);IUnknown_Release(pProcessors);return TRUE;break;case IDC_REMOVEVIDEOPROCESSOR:// remove processor from the selected video processor collectionIltmmCapture_get_SelectedVideoProcessors (g_pCapture, &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));IUnknown_Release(pSelProcessors);return TRUE;break;case IDC_REMOVEALLVIDEOPROCESSORS:// remove all selected video processorsIltmmCapture_get_SelectedVideoProcessors (g_pCapture, &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));IUnknown_Release(pSelProcessors);return TRUE;break;case IDC_REFRESHVIDEOPROCESSORS:// rebuild the registered video processor collectionIltmmCapture_get_VideoProcessors (g_pCapture, &pProcessors);RefreshProcessorList(pProcessors, GetDlgItem(hwnd, IDC_VIDEOPROCESSORS));ProcessorSelectionChanged(pProcessors, GetDlgItem(hwnd, IDC_VIDEOPROCESSORS),GetDlgItem(hwnd, IDC_ADDVIDEOPROCESSOR));IUnknown_Release(pProcessors);return TRUE;break;case IDC_VIDEOPROCESSORPROPERTIES:// invoke the selected video processor's properties dialog boxIltmmCapture_get_SelectedVideoProcessors (g_pCapture, &pSelProcessors);InvokeProcessorProperties(pSelProcessors, GetDlgItem(hwnd, IDC_SELVIDEOPROCESSORS), hwnd);IUnknown_Release(pSelProcessors);return TRUE;break;case IDC_SELVIDEOPROCESSORS:if(HIWORD(wParam) == LBN_SELCHANGE){// selection changed, update buttonsIltmmCapture_get_SelectedVideoProcessors (g_pCapture, &pSelProcessors);SelectedProcessorSelectionChanged(pSelProcessors, GetDlgItem(hwnd, IDC_SELVIDEOPROCESSORS),GetDlgItem(hwnd, IDC_REMOVEVIDEOPROCESSOR),GetDlgItem(hwnd, IDC_REMOVEALLVIDEOPROCESSORS),GetDlgItem(hwnd, IDC_VIDEOPROCESSORPROPERTIES));IUnknown_Release(pSelProcessors);}break;case IDC_VIDEOPROCESSORS:if(HIWORD(wParam) == LBN_SELCHANGE){// selection changed, update buttonsIltmmCapture_get_VideoProcessors (g_pCapture, &pProcessors);ProcessorSelectionChanged(pProcessors, GetDlgItem(hwnd, IDC_VIDEOPROCESSORS),GetDlgItem(hwnd, IDC_ADDVIDEOPROCESSOR));IUnknown_Release(pProcessors);}break;case IDOK:EndDialog(hwnd, IDOK);return TRUE;break;}break;}return FALSE;}void SelectingltmmCaptureObjectProcessors_Example ( HINSTANCE hInstance /*application instance handle*/ ){HRESULT hr;g_hInstance = hInstance;// initialize COM libraryhr = CoInitialize(NULL);if(FAILED(hr))goto error;// create the capture objecthr = 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:// cleanupif(g_pCapture)IUnknown_Release(g_pCapture);CoUninitialize();}