Windows Media Encoder SDK banner art
PreviousNext

Using Encoder Automation

Windows Media Encoder exposes a COM IDispatch interface that enables a client application to call methods and properties. Client applications can be written in many programming languages. This software development kit (SDK) focuses on those applications written in Visual Basic. Visual Basic is recommended for most applications because it hides the details of connecting to the IDispatch and other COM interfaces.

Visual C++ can also be used to automate Windows Media Encoder. The following C++ example offers an implementation for connecting to an encoder that is running or starting a new instance of Windows Media Encoder.

First you must declare and specify the GUID for Windows Media Encoder:

static const GUID CLSID_IAsfRTEncoder = 
    { 0x7DEBA670, 0x68AB, 0x11D0, { 0x98, 0xEB, 0x00, 0xaa, 0x00, 0xbb, 0xb5, 0x2c } };

The ConnectToEnc function attempts to connect to an instance of Windows Media Encoder that is running by calling the OLE API function, GetActiveObject, and passing in the GUID of the encoder. If the connection succeeds, the function returns a valid pointer, pIRexUnknown, to the IUknown interface. Then QueryInterface is used to obtain the IDispatch interface:

STDMETHODIMP CMyClass::ConnectToEnc(IDispatch **pIDispatch)
{
   HRESULT hr;
   IUnknown *pIRexUnknown = NULL;
    
   if (NULL == pIDispatch)
        return E_INVALIDARG;

   hr = GetActiveObject(CLSID_IAsfRTEncoder, NULL, &pIRexUnknown);

   if (SUCCEEDED(hr))
      hr = pIRexUnknown->QueryInterface(IID_IDispatch, (LPVOID *)pIDispatch);

   if (pIRexUnknown)
      pIRexUnknown->Release();

    return hr;
}

The StartNewEnc function creates a new instance of Windows Media Encoder by using the COM API function, CoCreateInstance:

STDMETHODIMP CMyClass::StartNewEnc(IDispatch **pIDispatch)
{
   HRESULT hr;
   IUnknown *pIRexUnknown = NULL;
    
    if (NULL == pIDispatch)
        return E_INVALIDARG;

   hr = CoCreateInstance(CLSID_IAsfRTEncoder, 
                         NULL, 
                         CLSCTX_SERVER, 
                         IID_IDispatch, 
                         (LPVOID *)pIDispatch);

    return hr;
}

Once Windows Media encoder is installed, the pIDispatch pointer can be used to invoke the encoder methods and manipulate the encoder properties.

For detailed information on using Visual C++ to create an Automation client application, see the Automation Programmer's Reference from Microsoft Press (ISBN 1-57231-584-9).

PreviousNext

© 1999 Microsoft Corporation. All rights reserved.