Selecting ltmmCapture Object Inputs for C
The following example shows how to enumerate and select ltmmCapture object inputs.
// 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
//
// BuildCaptureInputList
// fills a listbox to match the contents of the collection
//
// pCaptureInputs = input collection's interface pointer
// hwndListBox = listbox window handle
//
void BuildCaptureInputList(IltmmCaptureInputs* pCaptureInputs, 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
IltmmCaptureInputs_get_Count (pCaptureInputs, &count);
for(i = 0; i < count; i++)
{
IltmmCaptureInput* pCaptureInput;
BSTR bstr;
TCHAR sz[256];
int index;
VARIANT_BOOL f;
// retrieve collection item
IltmmCaptureInputs_Item (pCaptureInputs, i, &pCaptureInput);
// get displayable name
IltmmCaptureInput_get_Name (pCaptureInput, &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
IltmmCaptureInput_get_Selected (pCaptureInput, &f);
if(f)
selected = i;
// free the collection item
IUnknown_Release(pCaptureInput);
}
// 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;
}
}
}
//
// CaptureInputSelectionChanged
// called to reflect changes in the listbox selection
//
// pCaptureInputs = input collection's interface pointer
// hwndListBox = listbox window handle
//
void CaptureInputSelectionChanged(IltmmCaptureInputs* pCaptureInputs, 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
IltmmCaptureInputs_put_Selection (pCaptureInputs, -1);
}
else
{
// get the collection item index
item = (long) SendMessage(hwndListBox, LB__getITEMDATA, index, 0);
// select the item
#ifdef _DEBUG
{
IltmmCaptureInput* pCaptureInput;
IltmmCaptureInputs_Item (pCaptureInputs, item, &pCaptureInput);
hr = IltmmCaptureInput_put_Selected (pCaptureInput, VARIANT_TRUE);
IUnknown_Release(pCaptureInput);
}
#else
hr = IltmmCaptureInputs_put_Selection (pCaptureInputs, item);
#endif
// if new selection failed, we need to force the listbox to match the actual selection
if(FAILED(hr))
{
// get the real selection
IltmmCaptureInputs_get_Selection (pCaptureInputs, &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;
}
}
}
}
}
//
// SelectCompositeVideo
// selects composite video, if available
//
// pCaptureInputs = input collection's interface pointer
// hwndListBox = listbox window handle
//
void SelectCompositeVideo(IltmmCaptureInputs* pCaptureInputs, HWND hwndListBox)
{
long item;
BSTR bstr;
long i;
long count;
// Find the composite video input
bstr = SysAllocString(L"Video Composite");
IltmmCaptureInputs_Find(pCaptureInputs, bstr, &item);
SysFreeString(bstr);
// make it the current selection
if(item >= 0)
{
IltmmCaptureInputs_put_Selection (pCaptureInputs, item);
IltmmCaptureInputs_get_Count (pCaptureInputs, &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;
}
}
}
}
//
// CaptureInputDlgProc
// selects object inputs
//
BOOL CALLBACK CaptureInputDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
IltmmCaptureInputs* pCaptureInputs;
switch (msg)
{
case WM_INITDIALOG:
// initialize the video input listbox
IltmmCapture_get_VideoInputs (g_pCapture, &pCaptureInputs);
BuildCaptureInputList(pCaptureInputs, GetDlgItem(hwnd, IDC_VIDEOINPUTS));
IUnknown_Release(pCaptureInputs);
return TRUE;
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_SELECTCOMPOSITEVIDEO:
// select composite video
IltmmCapture_get_VideoInputs (g_pCapture, &pCaptureInputs);
SelectCompositeVideo(pCaptureInputs, GetDlgItem(hwnd, IDC_VIDEOINPUTS));
IUnknown_Release(pCaptureInputs);
return TRUE;
break;
case IDC_VIDEOINPUTS:
if(HIWORD(wParam) == LBN_SELCHANGE)
{
// select the video input
IltmmCapture_get_VideoInputs (g_pCapture, &pCaptureInputs);
CaptureInputSelectionChanged(pCaptureInputs, GetDlgItem(hwnd, IDC_VIDEOINPUTS));
IUnknown_Release(pCaptureInputs);
}
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;
IltmmDevices* pDevices;
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;
// select the first audio device available
IltmmCapture_get_AudioDevices (g_pCapture, &pDevices);
IltmmDevices_put_Selection (pDevices, 0);
IUnknown_Release(pDevices);
// select the first video device available
IltmmCapture_get_VideoDevices (g_pCapture, &pDevices);
IltmmDevices_put_Selection (pDevices, 0);
IUnknown_Release(pDevices);
DialogBox(g_hInstance, (LPCTSTR)IDD_INPUTDLG, NULL, CaptureInputDlgProc);
error:
// cleanup
if(g_pCapture)
IUnknown_Release(g_pCapture);
CoUninitialize();
return 0;
}
The following example shows how to enumerate and select ltmmCapture object inputs.
// 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 // // BuildCaptureInputList // fills a listbox to match the contents of the collection // // pCaptureInputs = input collection's interface pointer // hwndListBox = listbox window handle // void BuildCaptureInputList(IltmmCaptureInputs* pCaptureInputs, 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 IltmmCaptureInputs_get_Count (pCaptureInputs, &count); for(i = 0; i < count; i++) { IltmmCaptureInput* pCaptureInput; BSTR bstr; TCHAR sz[256]; int index; VARIANT_BOOL f; // retrieve collection item IltmmCaptureInputs_Item (pCaptureInputs, i, &pCaptureInput); // get displayable name IltmmCaptureInput_get_Name (pCaptureInput, &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 IltmmCaptureInput_get_Selected (pCaptureInput, &f); if(f) selected = i; // free the collection item IUnknown_Release(pCaptureInput); } // 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; } } } // // CaptureInputSelectionChanged // called to reflect changes in the listbox selection // // pCaptureInputs = input collection's interface pointer // hwndListBox = listbox window handle // void CaptureInputSelectionChanged(IltmmCaptureInputs* pCaptureInputs, 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 IltmmCaptureInputs_put_Selection (pCaptureInputs, -1); } else { // get the collection item index item = (long) SendMessage(hwndListBox, LB_GETITEMDATA, index, 0); // select the item #ifdef _DEBUG { IltmmCaptureInput* pCaptureInput; IltmmCaptureInputs_Item (pCaptureInputs, item, &pCaptureInput); hr = IltmmCaptureInput_put_Selected (pCaptureInput, VARIANT_TRUE); IUnknown_Release(pCaptureInput); } #else hr = IltmmCaptureInputs_put_Selection (pCaptureInputs, item); #endif // if new selection failed, we need to force the listbox to match the actual selection if(FAILED(hr)) { // get the real selection IltmmCaptureInputs_get_Selection (pCaptureInputs, &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; } } } } } // // SelectCompositeVideo // selects composite video, if available // // pCaptureInputs = input collection's interface pointer // hwndListBox = listbox window handle // void SelectCompositeVideo(IltmmCaptureInputs* pCaptureInputs, HWND hwndListBox) { long item; BSTR bstr; long i; long count; // Find the composite video input bstr = SysAllocString(L"Video Composite"); IltmmCaptureInputs_Find(pCaptureInputs, bstr, &item); SysFreeString(bstr); // make it the current selection if(item >= 0) { IltmmCaptureInputs_put_Selection (pCaptureInputs, item); IltmmCaptureInputs_get_Count (pCaptureInputs, &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; } } } } // // CaptureInputDlgProc // selects object inputs // BOOL CALLBACK CaptureInputDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { IltmmCaptureInputs* pCaptureInputs; wParam = wParam ; lParam = lParam ; switch (msg) { case WM_INITDIALOG: // initialize the video input listbox IltmmCapture_get_VideoInputs (g_pCapture, &pCaptureInputs); BuildCaptureInputList(pCaptureInputs, GetDlgItem(hwnd, IDC_VIDEOINPUTS)); IUnknown_Release(pCaptureInputs); return TRUE; break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_SELECTCOMPOSITEVIDEO: // select composite video IltmmCapture_get_VideoInputs (g_pCapture, &pCaptureInputs); SelectCompositeVideo(pCaptureInputs, GetDlgItem(hwnd, IDC_VIDEOINPUTS)); IUnknown_Release(pCaptureInputs); return TRUE; break; case IDC_VIDEOINPUTS: if(HIWORD(wParam) == LBN_SELCHANGE) { // select the video input IltmmCapture_get_VideoInputs (g_pCapture, &pCaptureInputs); CaptureInputSelectionChanged(pCaptureInputs, GetDlgItem(hwnd, IDC_VIDEOINPUTS)); IUnknown_Release(pCaptureInputs); } break; case IDOK: EndDialog(hwnd, IDOK); return TRUE; break; } break; } return FALSE; } L_MULTIMEDIATEX_API void SelectingltmmCaptureObjectInputs_Example ( HINSTANCE hInstance /*application instance handle*/ ) { HRESULT hr; IltmmDevices* pDevices; 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; // select the first audio device available IltmmCapture_get_AudioDevices (g_pCapture, &pDevices); IltmmDevices_put_Selection (pDevices, 0); IUnknown_Release(pDevices); // select the first video device available IltmmCapture_get_VideoDevices (g_pCapture, &pDevices); IltmmDevices_put_Selection (pDevices, 0); IUnknown_Release(pDevices); DialogBox(g_hInstance, (LPCTSTR)IDD_INPUTDLG, NULL, CaptureInputDlgProc); error: // cleanup if(g_pCapture) IUnknown_Release(g_pCapture); CoUninitialize(); }