The following example shows how to enumerate and select ltmfCapture object devices.
#include "ltmf.h"#include "resource.h"#include "tchar.h"#include "stdio.h"HINSTANCE g_hInstance; // application instance handleIltmfCapture* g_pCapture; // convert object's interface pointer//// BuildDeviceList// fills a listbox to match the contents of the collection//// pDevices = device collection's interface pointer// hwndListBox = listbox window handle//void BuildDeviceList(IltmfDevices* pDevices, HWND hwndListBox){long i;long count;int selected = -1;// reset the listbox contentsSendMessage(hwndListBox, LB_RESETCONTENT, 0, 0);// get the collection's item countpDevices->get_Count (&count);for(i = 0; i < count; i++){IltmfDevice* pDevice;BSTR bstr;TCHAR sz[256];int index;VARIANT_BOOL f;// retrieve collection itempDevices->Item (i, &pDevice);// get displayable namepDevice->get_FriendlyName (&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);// remember this listbox item, if it is selected in the collectionpDevice->get_Selected (&f);if (f)selected = i;// free the collection itempDevice->Release();}// select the appropriate listbox itemfor(i = 0; i < count; i++){if (selected == (long) SendMessage(hwndListBox, LB_GETITEMDATA, i, 0)){SendMessage(hwndListBox, LB_SETCURSEL, i, 0);break;}}}//// DeviceSelectionChanged// called to reflect changes in the listbox selection//// pDevices = device collection's interface pointer// hwndListBox = listbox window handle//void DeviceSelectionChanged(IltmfDevices* pDevices, HWND hwndListBox){int index;long item;int i;int count;HRESULT hr;// get the index of current listbox selectionindex = (int) SendMessage(hwndListBox, LB_GETCURSEL, 0, 0);if (index < 0){// clear selectionpDevices->put_Selection (-1);}else{// get the collection item indexitem = (long) SendMessage(hwndListBox, LB_GETITEMDATA, index, 0);// select the item#ifdef _DEBUG{IltmfDevice* pDevice;pDevices->Item (item, &pDevice);hr = pDevice->put_Selected(VARIANT_TRUE);pDevice->Release();}#elsehr = pDevices->put_Selection (item);#endif// if new selection failed, we need to force the listbox to match the actual selectionif (FAILED(hr)){// get the real selectionpDevices->get_Selection (&item);// find the matching listbox itemcount = (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;}}}}}//// RefreshDeviceList// called when to completely rebuild the collection//// pDevices = device collection's interface pointer// hwndListBox = listbox window handle//void RefreshDeviceList(IltmfDevices* pDevices, HWND hwndListBox){IltmfDevice* pDevice;long item;BSTR bstr;// get the currently selected itempDevices->get_Selection (&item);if (item >= 0){// get it's unique namepDevices->Item (item, &pDevice);pDevice->get_Name (&bstr);pDevice->Release();}else{bstr = NULL;}// rebuild the collectionpDevices->Refresh ();if (bstr){// find the previously selected itempDevices->Find(bstr, &item);// make it the current selectionif (item >= 0)pDevices->put_Selection (item);SysFreeString(bstr);}// rebuild the listboxBuildDeviceList(pDevices, hwndListBox);}//// DeviceDlgProc// selects object devices//BOOL CALLBACK DeviceDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){IltmfDevices* pDevices;switch (msg){case WM_INITDIALOG:// initialize the audio device listboxg_pCapture->get_AudioDevices (&pDevices);BuildDeviceList(pDevices, GetDlgItem(hwnd, IDC_AUDIODEVICES));pDevices->Release();// initialize the video device listboxg_pCapture->get_VideoDevices (&pDevices);BuildDeviceList(pDevices, GetDlgItem(hwnd, IDC_VIDEODEVICES));pDevices->Release();return TRUE;break;case WM_COMMAND:switch(LOWORD(wParam)){case IDC_REFRESHAUDIODEVICES:// refresh the audio device listboxg_pCapture->get_AudioDevices (&pDevices);RefreshDeviceList(pDevices, GetDlgItem(hwnd, IDC_AUDIODEVICES));pDevices->Release();return TRUE;break;case IDC_AUDIODEVICES:if (HIWORD(wParam) == LBN_SELCHANGE){// select the audio deviceg_pCapture->get_AudioDevices (&pDevices);DeviceSelectionChanged(pDevices, GetDlgItem(hwnd, IDC_AUDIODEVICES));pDevices->Release();}break;case IDC_REFRESHVIDEODEVICES:// initialize the video device listboxg_pCapture->get_VideoDevices (&pDevices);RefreshDeviceList(pDevices, GetDlgItem(hwnd, IDC_VIDEODEVICES));pDevices->Release();return TRUE;break;case IDC_VIDEODEVICES:if (HIWORD(wParam) == LBN_SELCHANGE){// select the video deviceg_pCapture->get_VideoDevices (&pDevices);DeviceSelectionChanged(pDevices, GetDlgItem(hwnd, IDC_VIDEODEVICES));pDevices->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 libraryhr = CoInitialize(NULL);if (FAILED(hr))goto error;// create the convert objecthr = CoCreateInstance(CLSID_ltmfCapture, NULL, CLSCTX_INPROC_SERVER, IID_IltmfCapture, (void**) &g_pCapture);if (FAILED(hr))goto error;DialogBox(g_hInstance, (LPCTSTR)IDD_DEVICEDLG, NULL, DeviceDlgProc);error:// cleanupif (g_pCapture)g_pCapture->Release();CoUninitialize();return 0;}