HRESULT ConfigureVMR9(HWND window)
{
    assert(g_graph != NULL);

    HRESULT hr = S_OK;
    RECT rect;

    SmartPtr<IVMRFilterConfig9> filterConfig;

    // Create the VMR-9.
    FAIL_RET( CoCreateInstance(CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)&g_filter) );

    // Configure the VMR-9.
    // Set the maximum number of video streams, and set windowless mode. 
    FAIL_RET( g_filter->QueryInterface(IID_IVMRFilterConfig9, reinterpret_cast<void**>(&filterConfig)) );

    FAIL_RET( filterConfig->SetNumberOfStreams( MAX_VIDEO_STREAMS ) );
    FAIL_RET( filterConfig->SetRenderingMode( VMR9Mode_Windowless ) );

    FAIL_RET( g_filter->QueryInterface(IID_IVMRWindowlessControl9, reinterpret_cast<void**>(&g_windowlessControl)) );
    FAIL_RET( g_windowlessControl->SetVideoClippingWindow( window ) );

    GetClientRect( window, & rect );
    FAIL_RET( g_windowlessControl->SetVideoPosition(NULL, & rect) );

    // Set the custom compositor on the VMR-9.

    g_compositor.Attach( new CMyCompositor9() );
    FAIL_RET( filterConfig->SetImageCompositor( g_compositor ));

    // Add the VMR-9 to the filter graph.
    FAIL_RET( g_graph->AddFilter(g_filter, L"Video Mixing Renderer 9") );

    return hr;
}
HRESULT StartGraph(HWND window)
{
    // Clear DirectShow interfaces (COM smart pointers)
    CloseGraph(window);

    SmartPtr<IVMRFilterConfig9> filterConfig;


    BSTR path = GetMoviePath();
    if( ! path )
    {
        return E_FAIL;
    }

    HRESULT hr;
    
    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&g_graph);

    if (SUCCEEDED(hr))
    {
        hr = CoCreateInstance(CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)&g_filter);
    }

    if (SUCCEEDED(hr))
    {
        hr = g_filter->QueryInterface(IID_IVMRFilterConfig9, reinterpret_cast<void**>(&filterConfig));
    }

    if (SUCCEEDED(hr))
    {
        hr = filterConfig->SetRenderingMode( VMR9Mode_Renderless );

    }

    if (SUCCEEDED(hr))
    {
        hr = filterConfig->SetNumberOfStreams(2);

    }

    if (SUCCEEDED(hr))
    {
        hr = SetAllocatorPresenter( g_filter, window );
    }

    if (SUCCEEDED(hr))
    {
        hr = g_graph->AddFilter(g_filter, L"Video Mixing Renderer 9");
    }

    if (SUCCEEDED(hr))
    {
        hr = g_graph->QueryInterface(IID_IMediaControl, reinterpret_cast<void**>(&g_mediaControl));
    }

    if (SUCCEEDED(hr))
    {
        hr = g_graph->RenderFile( path, NULL );
    }

    if (SUCCEEDED(hr))
    {
        hr = g_mediaControl->Run();
    }

    SysFreeString(path);

    return hr;
}