Selecting ltmmPlay Object Renderers for C
The following example shows how to enumerate and select ltmmPlay object renderers.
// 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
IltmmPlay* g_pPlay; // convert object's interface pointer
//
// BuildRendererList
// fills a listbox to match the contents of the collection
//
// pRenderers = renderer collection's interface pointer
// hwndListBox = listbox window handle
//
void BuildRendererList(IltmmRenderers* pRenderers, 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
IltmmRenderers__get_Count(pRenderers, &count);
for(i = 0; i < count; i++)
{
IltmmRenderer* pRenderer;
BSTR bstr;
TCHAR sz[256];
int index;
VARIANT_BOOL f;
// retrieve collection item
IltmmRenderers_Item(pRenderers, i, &pRenderer);
// get displayable name
IltmmRenderer__get_FriendlyName(pRenderer, &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
IltmmRenderer__get_Selected(pRenderer, &f);
if(f)
selected = i;
// free the collection item
IUnknown_Release(pRenderer);
}
// 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;
}
}
}
//
// RendererSelectionChanged
// called to reflect changes in the listbox selection
//
// pRenderers = renderer collection's interface pointer
// hwndListBox = listbox window handle
//
void RendererSelectionChanged(IltmmRenderers* pRenderers, 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
IltmmRenderers__put_Selection(pRenderers, -1);
}
else
{
// get the collection item index
item = (long) SendMessage(hwndListBox, LB__getITEMDATA, index, 0);
// select the item
#ifdef _DEBUG
{
IltmmRenderer* pRenderer;
IltmmRenderers_Item(pRenderers, item, &pRenderer);
hr = IltmmRenderer__put_Selected(pRenderer, VARIANT_TRUE);
IUnknown_Release(pRenderer);
}
#else
hr = IltmmRenderers__put_Selection(pRenderers, item);
#endif
// if new selection failed, we need to force the listbox to match the actual selection
if(FAILED(hr))
{
// get the real selection
IltmmRenderers__get_Selection(pRenderers, &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;
}
}
}
}
}
//
// RefreshRendererList
// called when to completely rebuild the collection
//
// pRenderers = renderer collection's interface pointer
// hwndListBox = listbox window handle
//
void RefreshRendererList(IltmmRenderers* pRenderers, HWND hwndListBox)
{
IltmmRenderer* pRenderer;
long item;
BSTR bstr;
// get the currently selected item
IltmmRenderers__get_Selection(pRenderers, &item);
if(item >= 0)
{
// get it's unique name
IltmmRenderers_Item(pRenderers, item, &pRenderer);
IltmmRenderer__get_Name(pRenderer, &bstr);
IUnknown_Release(pRenderer);
}
else
{
bstr = NULL;
}
// rebuild the collection
IltmmRenderers_Refresh(pRenderers);
if(bstr)
{
// find the previously selected item
IltmmRenderers_Find(pRenderers, bstr, &item);
// make it the current selection
if(item >= 0)
IltmmRenderers__put_Selection(pRenderers, item);
SysFreeString(bstr);
}
// rebuild the listbox
BuildRendererList(pRenderers, hwndListBox);
}
//
// RendererDlgProc
// selects object renderers
//
BOOL CALLBACK RendererDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
IltmmRenderers* pRenderers;
switch (msg)
{
case WM_INITDIALOG:
// initialize the audio renderer listbox
IltmmPlay__get_AudioRenderers(g_pPlay, &pRenderers);
BuildRendererList(pRenderers, GetDlgItem(hwnd, IDC_AUDIODEVICES));
IUnknown_Release(pRenderers);
return TRUE;
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_REFRESHAUDIODEVICES:
// refresh the audio renderer listbox
IltmmPlay__get_AudioRenderers(g_pPlay, &pRenderers);
RefreshRendererList(pRenderers, GetDlgItem(hwnd, IDC_AUDIODEVICES));
IUnknown_Release(pRenderers);
return TRUE;
break;
case IDC_AUDIODEVICES:
if(HIWORD(wParam) == LBN_SELCHANGE)
{
// select the audio renderer
IltmmPlay__get_AudioRenderers(g_pPlay, &pRenderers);
RendererSelectionChanged(pRenderers, GetDlgItem(hwnd, IDC_AUDIODEVICES));
IUnknown_Release(pRenderers);
}
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 play object
hr = CoCreateInstance(&CLSID_ltmmPlay, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmPlay, (void**) &g_pPlay);
if(FAILED(hr))
goto error;
DialogBox(g_hInstance, (LPCTSTR)IDD_DEVICEDLG, NULL, RendererDlgProc);
error:
// cleanup
if(g_pPlay)
IUnknown_Release(g_pPlay);
CoUninitialize();
return 0;
}