Selecting ltmmConvert Object Target Formats for C++

The following code demonstrates how to enumerate, add, and select ltmmConvert object target formats:

#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 
// 
// BuildTargetFormatList 
// fills a listbox to match the contents of the collection 
// 
// pTargetFormats = format collection's interface pointer 
// hwndListBox = listbox window handle 
// 
void BuildTargetFormatList(IltmmTargetFormats* pTargetFormats, 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 
   pTargetFormats->get_Count(&count); 
   for(i = 0; i < count; i++) 
   { 
      IltmmTargetFormat* pTargetFormat; 
      BSTR bstr; 
      TCHAR sz[256]; 
      int index; 
      VARIANT_BOOL f; 
      // retrieve collection item 
      pTargetFormats->Item(i, &pTargetFormat); 
      // get displayable name 
      pTargetFormat->get_Name(&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 
      pTargetFormat->get_Selected(&f); 
      if(f) 
         selected = i; 
      // free the collection item 
      pTargetFormat->Release(); 
   } 
   // 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; 
      } 
   } 
} 
// 
// TargetFormatSelectionChanged 
// called to reflect changes in the listbox selection 
// 
// pTargetFormats = format collection's interface pointer 
// hwndListBox = listbox window handle 
// 
void TargetFormatSelectionChanged(IltmmTargetFormats* pTargetFormats, 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 
      pTargetFormats->put_Selection(-1); 
   } 
   else 
   { 
      // get the collection item index 
      item = (long) SendMessage(hwndListBox, LB_GETITEMDATA, index, 0); 
      // select the item 
#ifdef _DEBUG 
      { 
         IltmmTargetFormat* pTargetFormat; 
         pTargetFormats->Item(item, &pTargetFormat); 
         hr = pTargetFormat->put_Selected(VARIANT_TRUE); 
         pTargetFormat->Release(); 
      } 
#else 
      hr = pTargetFormats->put_Selection(item); 
#endif 
      // if new selection failed, we need to force the listbox to match the actual selection 
      if(FAILED(hr)) 
      { 
         // get the real selection 
         pTargetFormats->get_Selection(&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; 
            } 
         } 
      } 
   } 
#ifdef _DEBUG 
   { 
      long item; 
      pTargetFormats->get_Selection(&item); 
      if(item >= 0) 
      { 
         IltmmTargetFormat* pTargetFormat; 
         TCHAR sz[2048]; 
         LPTSTR p; 
         VARIANT_BOOL b; 
         BSTR bstr; 
         sz[0] = _T('\0'); 
         p = sz; 
         pTargetFormats->Item(item, &pTargetFormat); 
         pTargetFormat->get_AudioCompressor(&bstr); 
         p += _stprintf(p, _T("AComp = %ls"), bstr); 
         SysFreeString(bstr); 
         pTargetFormat->get_VideoCompressor(&bstr); 
         p += _stprintf(p, _T(", VComp = %ls"), bstr); 
         SysFreeString(bstr); 
         pTargetFormat->get_AVMux(&bstr); 
         p += _stprintf(p, _T(", AVMux = %ls"), bstr); 
         SysFreeString(bstr); 
         pTargetFormat->get_Mux(&bstr); 
         p += _stprintf(p, _T(", Mux = %ls"), bstr); 
         SysFreeString(bstr); 
         pTargetFormat->get_SinkSubType(&bstr); 
         p += _stprintf(p, _T(", SubType = %ls"), bstr); 
         SysFreeString(bstr); 
         pTargetFormat->get_Sink(&bstr); 
         p += _stprintf(p, _T(", Sink = %ls"), bstr); 
         SysFreeString(bstr); 
         pTargetFormat->get_ReadOnly(&b); 
         p += _stprintf(p, _T(", ReadOnly = %s"), b != 0 ? _T("Yes") : _T("No")); 
         pTargetFormat->Release(); 
         MessageBox(NULL, sz, _T("Target Format"), MB_OK); 
      } 
   } 
#endif 
} 
// 
// AddCustomTargetFormats 
// called to append custom target formats 
// 
// pTargetFormats = format collection's interface pointer 
// 
void AddCustomTargetFormats(IltmmTargetFormats* pTargetFormats) 
{ 
   BSTR bstr; 
   long index; 
   IltmmTargetFormat* pTargetFormat; 
   // remove all custom formats 
#ifdef _DEBUG 
   pTargetFormats->get_Count(&index); 
   pTargetFormats->Remove(index - 1); 
#else 
   pTargetFormats->RemoveAll(); 
#endif 
   bstr = SysAllocString(L"CUSTOM FORMAT 1"); 
   pTargetFormats->Add(bstr, -1); 
   pTargetFormats->Find(bstr, &index); 
   SysFreeString(bstr); 
   pTargetFormats->Item(index, &pTargetFormat); 
   // assign an optional audio compressor 
   // this case: MP3 
   bstr = SysAllocString(L"@device:cm:{33D9A761-90C8-11D0-BD43-00A0C911CE86}\\85MPEG Layer-3"); 
   pTargetFormat->put_AudioCompressor(bstr); 
   SysFreeString(bstr); 
   // assign an optional video compressor 
   // this case: LEAD MCMP 
   bstr = SysAllocString(L"@device:sw:{33D9A760-90C8-11D0-BD43-00A0C911CE86}\\LEAD MCMP/MJPEG Codec (2.0)"); 
   pTargetFormat->put_VideoCompressor(bstr); 
   SysFreeString(bstr); 
   // assign an optional AVMux compressor 
   // this case: no premux 
   bstr = SysAllocString(L""); 
   pTargetFormat->put_AVMux(bstr); 
   SysFreeString(bstr); 
   // assign the required Mux 
   // this case: AVI 
   bstr = SysAllocString(L"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\\{E2510970-F137-11CE-8B67-00AA00A3F1A6}"); 
   pTargetFormat->put_Mux(bstr); 
   SysFreeString(bstr); 
   // assign the optional SinkSubType 
   // this case: MEDIASUBTYPE_Avi 
   bstr = SysAllocString(L"{E436EB88-524F-11CE-9F53-0020AF0BA770}"); 
   pTargetFormat->put_SinkSubType(bstr); 
   SysFreeString(bstr); 
   // assign an optional Sink 
   // this case: default 
   bstr = SysAllocString(L""); 
   pTargetFormat->put_Sink(bstr); 
   SysFreeString(bstr); 
   pTargetFormat->Release(); 
} 
// 
// TargetFormatDlgProc 
// selects object formats 
// 
BOOL CALLBACK TargetFormatDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
   IltmmTargetFormats* pTargetFormats; 
   switch (msg) 
   { 
   case WM_INITDIALOG: 
      // initialize the target format listbox 
      g_pConvert->get_TargetFormats(&pTargetFormats); 
      AddCustomTargetFormats(pTargetFormats); 
      BuildTargetFormatList(pTargetFormats, GetDlgItem(hwnd, IDC_TARGETFORMATS)); 
      pTargetFormats->Release(); 
      return TRUE; 
      break; 
   case WM_COMMAND: 
      switch(LOWORD(wParam)) 
      { 
   case IDC_TARGETFORMATS: 
         if(HIWORD(wParam) == LBN_SELCHANGE) 
         { 
            // select the target format 
            g_pConvert->get_TargetFormats(&pTargetFormats); 
            TargetFormatSelectionChanged(pTargetFormats, GetDlgItem(hwnd, IDC_TARGETFORMATS)); 
            pTargetFormats->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 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_TARGETFORMATDLG, NULL, TargetFormatDlgProc); 
error: 
   // cleanup 
   if(g_pConvert) 
      g_pConvert->Release(); 
   CoUninitialize(); 
   return 0; 
} 

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Multimedia C API Help