IltmmCapture::get_TargetStream Example for C++

// This function will write the contents of the target
// data for the passed capture object to a disk file
void WriteTargetMemory(LPCWSTR lpwszFileName, IltmmCapture *pCapture)
{
   HANDLE hFile = NULL;
   
   // open file for output
   hFile = CreateFile(lpwszFileName, // open file name
      GENERIC_WRITE,              // open for writing
      FILE_SHARE_READ,            // share for reading
      NULL,                       // no security
      CREATE_ALWAYS,              // re-create the file
      FILE_ATTRIBUTE_NORMAL,      // normal file
      NULL);                      // no attr. template

   if (hFile == INVALID_HANDLE_VALUE)
      return;

   long fl = 0;
   unsigned long lBytesWritten;
   IltmmMemory *pTarget = NULL;
   
   // try to get the target stream
   pCapture->get_TargetStream((IUnknown**)&pTarget);
   if(!pTarget)
      return;
   
   VARIANT varData;
   unsigned char *pBuffer = NULL;
   SAFEARRAY sa;

   // get the buffer size and initialize a variant
   pTarget->get_BufferSize(&fl);
   VariantInit(&varData);
   
   // setup the safe array
   memset(&sa, 0, sizeof(sa));
   sa.cbElements = sizeof(unsigned char);
   sa.cDims = 1;
   sa.fFeatures = (FADF_AUTO | FADF_FIXEDSIZE);
   sa.pvData = new UCHAR[fl];
   sa.rgsabound[0].cElements = fl;

   // associate the safe array with our variant
   V_VT(&varData) = (VT_ARRAY | VT_UI1);
   V_ARRAY(&varData) = &sa;

   // copy the target data to the variant safe array and to our buffer
   pTarget->CopyData(0, fl, &varData);
   SafeArrayAccessData(V_ARRAY(&varData), (void**)&pBuffer);

   // write the buffer out to our file
   WriteFile(hFile, pBuffer, fl, &lBytesWritten, NULL);

   // clean up the variant
   SafeArrayUnaccessData(V_ARRAY(&varData));
   VariantClear(&varData);

   // close the output file
   CloseHandle(hFile);
   
   // release the target object
   pTarget->Release();
}