Selecting ltmmConvert Object Target Devices for C
The following example shows how to enumerate and select ltmmConvert object target 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
IltmmConvert* g_pConvert; // convert object's interface pointer
//
// BuildTargetDeviceList
// fills a listbox to match the contents of the collection
//
// pTargetDevices = device collection's interface pointer
// hwndListBox = listbox window handle
//
void BuildTargetDeviceList(IltmmTargetDevices* pTargetDevices, 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
IltmmTargetDevices_get_Count (pTargetDevices, &count);
for(i = 0; i < count; i++)
{
IltmmTargetDevice* pTargetDevice;
BSTR bstr;
TCHAR sz[256];
int index;
VARIANT_BOOL f;
// retrieve collection item
IltmmTargetDevices_Item(pTargetDevices, i, &pTargetDevice);
// get displayable name
IltmmTargetDevice_get_FriendlyName(pTargetDevice, &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
IltmmTargetDevice_get_Selected(pTargetDevice, &f);
if(f)
selected = i;
// free the collection item
IUnknown_Release(pTargetDevice);
}
// 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;
}
}
}
//
// TargetDeviceSelectionChanged
// called to reflect changes in the listbox selection
//
// pTargetDevices = device collection's interface pointer
// hwndListBox = listbox window handle
//
void TargetDeviceSelectionChanged(IltmmTargetDevices* pTargetDevices, 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
IltmmTargetDevices_put_Selection(pTargetDevices, -1);
}
else
{
// get the collection item index
item = (long) SendMessage(hwndListBox, LB__getITEMDATA, index, 0);
// select the item
#ifdef _DEBUG
{
IltmmTargetDevice* pTargetDevice;
IltmmTargetDevices_Item(pTargetDevices, item, &pTargetDevice);
hr = IltmmTargetDevice_put_Selected(pTargetDevice, VARIANT_TRUE);
IUnknown_Release(pTargetDevice);
}
#else
hr = IltmmTargetDevices_put_Selection(pTargetDevices, item);
#endif
// if new selection failed, we need to force the listbox to match the actual selection
if(FAILED(hr))
{
// get the real selection
IltmmTargetDevices_get_Selection(pTargetDevices, &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;
}
}
}
}
}
//
// RefreshTargetDeviceList
// called when to completely rebuild the collection
//
// pTargetDevices = device collection's interface pointer
// hwndListBox = listbox window handle
//
void RefreshTargetDeviceList(IltmmTargetDevices* pTargetDevices, HWND hwndListBox)
{
IltmmTargetDevice* pTargetDevice;
long item;
BSTR bstr;
// get the currently selected item
IltmmTargetDevices_get_Selection(pTargetDevices, &item);
if(item >= 0)
{
// get it's unique name
IltmmTargetDevices_Item(pTargetDevices, item, &pTargetDevice);
IltmmTargetDevice_get_Name(pTargetDevice, &bstr);
IUnknown_Release(pTargetDevice);
}
else
{
bstr = NULL;
}
// rebuild the collection
IltmmTargetDevices_Refresh(pTargetDevices);
if(bstr)
{
// find the previously selected item
IltmmTargetDevices_Find(pTargetDevices, bstr, &item);
// make it the current selection
if(item >= 0)
IltmmTargetDevices_put_Selection(pTargetDevices, item);
SysFreeString(bstr);
}
// rebuild the listbox
BuildTargetDeviceList(pTargetDevices, hwndListBox);
}
//
// TargetDeviceDlgProc
// selects object devices
//
BOOL CALLBACK TargetDeviceDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
IltmmTargetDevices* pTargetDevices;
switch (msg)
{
case WM_INITDIALOG:
// initialize the target device listbox
IltmmConvert__get_TargetDevices(g_pConvert, &pTargetDevices);
BuildTargetDeviceList(pTargetDevices, GetDlgItem(hwnd, IDC_TARGETDEVICES));
IUnknown_Release(pTargetDevices);
return TRUE;
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_REFRESHTARGETDEVICES:
// initialize the target device listbox
IltmmConvert__get_TargetDevices(g_pConvert, &pTargetDevices);
RefreshTargetDeviceList(pTargetDevices, GetDlgItem(hwnd, IDC_TARGETDEVICES));
IUnknown_Release(pTargetDevices);
return TRUE;
break;
case IDC_TARGETDEVICES:
if(HIWORD(wParam) == LBN_SELCHANGE)
{
// select the target device
IltmmConvert__get_TargetDevices(g_pConvert, &pTargetDevices);
TargetDeviceSelectionChanged(pTargetDevices, GetDlgItem(hwnd, IDC_TARGETDEVICES));
IUnknown_Release(pTargetDevices);
}
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_ltmmConvert, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmConvert, (void**) &g_pConvert);
if(FAILED(hr))
goto error;
DialogBox(g_hInstance, (LPCTSTR)IDD_TARGETDEVICEDLG, NULL, TargetDeviceDlgProc);
error:
// cleanup
if(g_pConvert)
IUnknown_Release(g_pConvert);
CoUninitialize();
return 0;
}
The following example shows how to enumerate and select ltmmConvert object target 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 IltmmConvert* g_pConvert; // convert object's interface pointer // // BuildTargetDeviceList // fills a listbox to match the contents of the collection // // pTargetDevices = device collection's interface pointer // hwndListBox = listbox window handle // void BuildTargetDeviceList(IltmmTargetDevices* pTargetDevices, 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 IltmmTargetDevices_get_Count (pTargetDevices, &count); for(i = 0; i < count; i++) { IltmmTargetDevice* pTargetDevice; BSTR bstr; TCHAR sz[256]; int index; VARIANT_BOOL f; // retrieve collection item IltmmTargetDevices_Item(pTargetDevices, i, &pTargetDevice); // get displayable name IltmmTargetDevice_get_FriendlyName(pTargetDevice, &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 IltmmTargetDevice_get_Selected(pTargetDevice, &f); if(f) selected = i; // free the collection item IUnknown_Release(pTargetDevice); } // 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; } } } // // TargetDeviceSelectionChanged // called to reflect changes in the listbox selection // // pTargetDevices = device collection's interface pointer // hwndListBox = listbox window handle // void TargetDeviceSelectionChanged(IltmmTargetDevices* pTargetDevices, 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 IltmmTargetDevices_put_Selection(pTargetDevices, -1); } else { // get the collection item index item = (long) SendMessage(hwndListBox, LB_GETITEMDATA, index, 0); // select the item #ifdef _DEBUG { IltmmTargetDevice* pTargetDevice; IltmmTargetDevices_Item(pTargetDevices, item, &pTargetDevice); hr = IltmmTargetDevice_put_Selected(pTargetDevice, VARIANT_TRUE); IUnknown_Release(pTargetDevice); } #else hr = IltmmTargetDevices_put_Selection(pTargetDevices, item); #endif // if new selection failed, we need to force the listbox to match the actual selection if(FAILED(hr)) { // get the real selection IltmmTargetDevices_get_Selection(pTargetDevices, &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; } } } } } // // RefreshTargetDeviceList // called when to completely rebuild the collection // // pTargetDevices = device collection's interface pointer // hwndListBox = listbox window handle // void RefreshTargetDeviceList(IltmmTargetDevices* pTargetDevices, HWND hwndListBox) { IltmmTargetDevice* pTargetDevice; long item; BSTR bstr; // get the currently selected item IltmmTargetDevices_get_Selection(pTargetDevices, &item); if(item >= 0) { // get it's unique name IltmmTargetDevices_Item(pTargetDevices, item, &pTargetDevice); IltmmTargetDevice_get_Name(pTargetDevice, &bstr); IUnknown_Release(pTargetDevice); } else { bstr = NULL; } // rebuild the collection IltmmTargetDevices_Refresh(pTargetDevices); if(bstr) { // find the previously selected item IltmmTargetDevices_Find(pTargetDevices, bstr, &item); // make it the current selection if(item >= 0) IltmmTargetDevices_put_Selection(pTargetDevices, item); SysFreeString(bstr); } // rebuild the listbox BuildTargetDeviceList(pTargetDevices, hwndListBox); } // // TargetDeviceDlgProc // selects object devices // BOOL CALLBACK TargetDeviceDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { IltmmTargetDevices* pTargetDevices; lParam = lParam ; switch (msg) { case WM_INITDIALOG: // initialize the target device listbox IltmmConvert_get_TargetDevices(g_pConvert, &pTargetDevices); BuildTargetDeviceList(pTargetDevices, GetDlgItem(hwnd, IDC_TARGETDEVICES)); IUnknown_Release(pTargetDevices); return TRUE; break; case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_REFRESHTARGETDEVICES: // initialize the target device listbox IltmmConvert_get_TargetDevices(g_pConvert, &pTargetDevices); RefreshTargetDeviceList(pTargetDevices, GetDlgItem(hwnd, IDC_TARGETDEVICES)); IUnknown_Release(pTargetDevices); return TRUE; break; case IDC_TARGETDEVICES: if(HIWORD(wParam) == LBN_SELCHANGE) { // select the target device IltmmConvert_get_TargetDevices(g_pConvert, &pTargetDevices); TargetDeviceSelectionChanged(pTargetDevices, GetDlgItem(hwnd, IDC_TARGETDEVICES)); IUnknown_Release(pTargetDevices); } break; case IDOK: EndDialog(hwnd, IDOK); return TRUE; break; } break; } return FALSE; } L_MULTIMEDIATEX_API void SelectingltmmConvertObjectTargetDevices_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_ltmmConvert, NULL, CLSCTX_INPROC_SERVER, &IID_IltmmConvert, (void**) &g_pConvert); if(FAILED(hr)) goto error; DialogBox(g_hInstance, (LPCTSTR)IDD_TARGETDEVICEDLG, NULL, TargetDeviceDlgProc); error: // cleanup if(g_pConvert) IUnknown_Release(g_pConvert); CoUninitialize(); }