Selecting ltmmCapture Object Devices for C
The following example shows how to enumerate and select ltmmCapture object devices.
// define helper macros for using interfaces under C
#define COBJMACROS
#include "ltmm.h"
#include "resource.h"
#include "tchar.h"
#include "stdio.h"
HINSTANCE g_hInstance; // application instance handle
IltmmCapture* 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(IltmmDevices* pDevices, 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
IltmmDevices_get_Count(pDevices, &count);
for(i = 0; i < count; i++)
{
IltmmDevice* pDevice;
BSTR bstr;
TCHAR sz[256];
int index;
VARIANT_BOOL f;
// retrieve collection item
IltmmDevices_Item(pDevices, i, &pDevice);
// get displayable name
IltmmDevice_get_FriendlyName(pDevice, &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);
// remember this listbox item, if it is selected in the collection
IltmmDevice_get_Selected(pDevice, &f);
if(f)
selected = i;
// free the collection item
IUnknown_Release(pDevice);
}
// 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;
}
}
}
//
// DeviceSelectionChanged
// called to reflect changes in the listbox selection
//
// pDevices = device collection's interface pointer
// hwndListBox = listbox window handle
//
void DeviceSelectionChanged(IltmmDevices* pDevices, 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
IltmmDevices_put_Selection(pDevices, -1);
}
else
{
// get the collection item index
item = (long) SendMessage(hwndListBox, LB__getITEMDATA, index, 0);
// select the item
#ifdef _DEBUG
{
IltmmDevice* pDevice;
IltmmDevices_Item(pDevices, item, &pDevice);
hr = IltmmDevice_put_Selected(pDevice, VARIANT_TRUE);
IUnknown_Release(pDevice);
}
#else
hr = IltmmDevices_put_Selection(pDevices, item);
#endif
// if new selection failed, we need to force the listbox to match the actual selection
if(FAILED(hr))
{
// get the real selection
IltmmDevices_get_Selection(pDevices, &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;
}
}
}
}
}
//
// RefreshDeviceList
// called when to completely rebuild the collection
//
// pDevices = device collection's interface pointer
// hwndListBox = listbox window handle
//
void RefreshDeviceList(IltmmDevices* pDevices, HWND hwndListBox)
{
IltmmDevice* pDevice;
long item;
BSTR bstr;
// get the currently selected item
IltmmDevices_get_Selection(pDevices, &item);
if(item >= 0)
{
// get it's unique name
IltmmDevices_Item(pDevices, item, &pDevice);
IltmmDevice_get_Name(pDevice, &bstr);
IUnknown_Release(pDevice);
}
else
{
bstr = NULL;
}
// rebuild the collection
IltmmDevices_Refresh(pDevices);
if(bstr)
{
// find the previously selected item
IltmmDevices_Find (pDevices, bstr, &item);
// make it the current selection
if(item >= 0)
IltmmDevices_put_Selection (pDevices, item);
SysFreeString(bstr);
}
// rebuild the listbox
BuildDeviceList(pDevices, hwndListBox);
}
//
// DeviceDlgProc
// selects object devices
//
BOOL CALLBACK DeviceDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
IltmmDevices* pDevices;
switch (msg)
{
case WM_INITDIALOG:
// initialize the audio device listbox
IltmmCapture_get_AudioDevices(g_pCapture, &pDevices);
BuildDeviceList(pDevices, GetDlgItem(hwnd, IDC_AUDIODEVICES));
IUnknown_Release(pDevices);
// initialize the video device listbox
IltmmCapture_get_VideoDevices(g_pCapture, &pDevices);
BuildDeviceList(pDevices, GetDlgItem(hwnd, IDC_VIDEODEVICES));
IUnknown_Release(pDevices);
return TRUE;
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_REFRESHAUDIODEVICES:
// refresh the audio device listbox
IltmmCapture_get_AudioDevices(g_pCapture, &pDevices);
RefreshDeviceList(pDevices, GetDlgItem(hwnd, IDC_AUDIODEVICES));
IUnknown_Release(pDevices);
return TRUE;
break;
case IDC_AUDIODEVICES:
if(HIWORD(wParam) == LBN_SELCHANGE)
{
// select the audio device
IltmmCapture_get_AudioDevices(g_pCapture, &pDevices);
DeviceSelectionChanged(pDevices, GetDlgItem(hwnd, IDC_AUDIODEVICES));
IUnknown_Release(pDevices);
}
break;
case IDC_REFRESHVIDEODEVICES:
// initialize the video device listbox
IltmmCapture_get_VideoDevices(g_pCapture, &pDevices);
RefreshDeviceList(pDevices, GetDlgItem(hwnd, IDC_VIDEODEVICES));
IUnknown_Release(pDevices);
return TRUE;
break;
case IDC_VIDEODEVICES:
if(HIWORD(wParam) == LBN_SELCHANGE)
{
// select the video device
IltmmCapture_get_VideoDevices(g_pCapture, &pDevices);
DeviceSelectionChanged(pDevices, GetDlgItem(hwnd, IDC_VIDEODEVICES));
IUnknown_Release(pDevices);
}
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 convert object
hr = CoCreateInstance(&CLSID_ltmmCapture, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmCapture, (void**) &g_pCapture);
if(FAILED(hr))
goto error;
DialogBox(g_hInstance, (LPCTSTR)IDD_DEVICEDLG, NULL, DeviceDlgProc);
error:
// cleanup
if(g_pCapture)
IUnknown_Release(g_pCapture);
CoUninitialize();
return 0;
}
The following example shows how to enumerate and select ltmmCapture object devices.
// 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 handle IltmmCapture* 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(IltmmDevices* pDevices, 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 IltmmDevices_get_Count(pDevices, &count); for(i = 0; i < count; i++) { IltmmDevice* pDevice; BSTR bstr; TCHAR sz[256]; int index; VARIANT_BOOL f; // retrieve collection item IltmmDevices_Item(pDevices, i, &pDevice); // get displayable name IltmmDevice_get_FriendlyName(pDevice, &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); // remember this listbox item, if it is selected in the collection IltmmDevice_get_Selected(pDevice, &f); if(f) selected = i; // free the collection item IUnknown_Release(pDevice); } // 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; } } } // // DeviceSelectionChanged // called to reflect changes in the listbox selection // // pDevices = device collection's interface pointer // hwndListBox = listbox window handle // void DeviceSelectionChanged(IltmmDevices* pDevices, 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 IltmmDevices_put_Selection(pDevices, -1); } else { // get the collection item index item = (long) SendMessage(hwndListBox, LB_GETITEMDATA, index, 0); // select the item #ifdef _DEBUG { IltmmDevice* pDevice; IltmmDevices_Item(pDevices, item, &pDevice); hr = IltmmDevice_put_Selected(pDevice, VARIANT_TRUE); IUnknown_Release(pDevice); } #else hr = IltmmDevices_put_Selection(pDevices, item); #endif // if new selection failed, we need to force the listbox to match the actual selection if(FAILED(hr)) { // get the real selection IltmmDevices_get_Selection(pDevices, &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; } } } } } // // RefreshDeviceList // called when to completely rebuild the collection // // pDevices = device collection's interface pointer // hwndListBox = listbox window handle // void RefreshDeviceList(IltmmDevices* pDevices, HWND hwndListBox) { IltmmDevice* pDevice; long item; BSTR bstr; // get the currently selected item IltmmDevices_get_Selection(pDevices, &item); if(item >= 0) { // get it's unique name IltmmDevices_Item(pDevices, item, &pDevice); IltmmDevice_get_Name(pDevice, &bstr); IUnknown_Release(pDevice); } else { bstr = NULL; } // rebuild the collection IltmmDevices_Refresh(pDevices); if(bstr) { // find the previously selected item IltmmDevices_Find (pDevices, bstr, &item); // make it the current selection if(item >= 0) IltmmDevices_put_Selection (pDevices, item); SysFreeString(bstr); } // rebuild the listbox BuildDeviceList(pDevices, hwndListBox); } // // DeviceDlgProc // selects object devices // BOOL CALLBACK DeviceDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { IltmmDevices* pDevices; wParam = wParam ; lParam = lParam ; switch (msg) { case WM_INITDIALOG: // initialize the audio device listbox IltmmCapture_get_AudioDevices(g_pCapture, &pDevices); BuildDeviceList(pDevices, GetDlgItem(hwnd, IDC_AUDIODEVICES)); IUnknown_Release(pDevices); // initialize the video device listbox IltmmCapture_get_VideoDevices(g_pCapture, &pDevices); BuildDeviceList(pDevices, GetDlgItem(hwnd, IDC_VIDEODEVICES)); IUnknown_Release(pDevices); return TRUE; break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_REFRESHAUDIODEVICES: // refresh the audio device listbox IltmmCapture_get_AudioDevices(g_pCapture, &pDevices); RefreshDeviceList(pDevices, GetDlgItem(hwnd, IDC_AUDIODEVICES)); IUnknown_Release(pDevices); return TRUE; break; case IDC_AUDIODEVICES: if(HIWORD(wParam) == LBN_SELCHANGE) { // select the audio device IltmmCapture_get_AudioDevices(g_pCapture, &pDevices); DeviceSelectionChanged(pDevices, GetDlgItem(hwnd, IDC_AUDIODEVICES)); IUnknown_Release(pDevices); } break; case IDC_REFRESHVIDEODEVICES: // initialize the video device listbox IltmmCapture_get_VideoDevices(g_pCapture, &pDevices); RefreshDeviceList(pDevices, GetDlgItem(hwnd, IDC_VIDEODEVICES)); IUnknown_Release(pDevices); return TRUE; break; case IDC_VIDEODEVICES: if(HIWORD(wParam) == LBN_SELCHANGE) { // select the video device IltmmCapture_get_VideoDevices(g_pCapture, &pDevices); DeviceSelectionChanged(pDevices, GetDlgItem(hwnd, IDC_VIDEODEVICES)); IUnknown_Release(pDevices); } break; case IDOK: EndDialog(hwnd, IDOK); return TRUE; break; } break; } return FALSE; } L_MULTIMEDIATEX_API void SelectingltmmCaptureObjectDevices_Example ( HINSTANCE hInstance /*application instance handle*/ ) { HRESULT hr; g_hInstance = hInstance; // initialize COM library hr = CoInitialize(NULL); if(FAILED(hr)) goto error; // create the convert object hr = CoCreateInstance(&CLSID_ltmmCapture, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmCapture, (void**) &g_pCapture); if(FAILED(hr)) goto error; DialogBox(g_hInstance, (LPCTSTR)IDD_DEVICEDLG, NULL, DeviceDlgProc); error: // cleanup if(g_pCapture) IUnknown_Release(g_pCapture); CoUninitialize(); }