DirectShow Sample Code

The following code does not include the functions WinMain or WindowProc. See the sample code in Setting the Video Window for these functions.

Note   To make the following code example easier to read, error checking is not included. This code example should not be used in a release configuration unless it has been modified to include secure error handling.

#include <windows.h>
#include <streams.h>

#define WM_GRAPHNOTIFY  WM_APP + 1
#define CLASSNAME "EventNotify"

IGraphBuilder   *pGraph = NULL;
IMediaControl   *pMediaControl = NULL;
IMediaEventEx   *pEvent = NULL;
HWND            g_hwnd;

void PlayFile(void)
{
    // Create the filter graph manager and render the file.
    CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void **)&pGraph);
    // Filenames on Windows CE start with a \\ instead of a drive letter.
    pGraph->RenderFile(L"\\Media\\Boys.avi", NULL);

    // Specify the owner window.
    IVideoWindow    *pVidWin = NULL;
    pGraph->QueryInterface(IID_IVideoWindow, (void **)&pVidWin);
    pVidWin->put_Owner((OAHWND)g_hwnd);
    pVidWin->put_WindowStyle( WS_CHILD | WS_CLIPSIBLINGS);

    // Set the owner window to receive event notices.
    pGraph->QueryInterface(IID_IMediaEventEx, (void **)&pEvent);
    pEvent->SetNotifyWindow((OAHWND)g_hwnd, WM_GRAPHNOTIFY, 0);

    // Run the graph.
    pGraph->QueryInterface(IID_IMediaControl, (void **)&pMediaControl);
    pMediaControl->Run();
}

void CleanUp(void)
{
    IVideoWindow    *pVidWin = NULL;
    pGraph->QueryInterface(IID_IVideoWindow, (void **)&pVidWin);
    pVidWin->put_Visible(OAFALSE);
    pVidWin->put_Owner(NULL);

    // Stop the graph.
    pMediaControl->Stop();

    long evCode;
    pEvent->WaitForCompletion(INFINITE, &evCode);

    pMediaControl->Release();
    pEvent->Release();
    pGraph->Release();
    PostQuitMessage(0);
}

void HandleEvent() 
{
    long evCode, param1, param2;
    HRESULT hr;
    while (hr = pEvent->GetEvent(&evCode, &param1, &param2, 0), SUCCEEDED(hr))
    { 
        hr = pEvent->FreeEventParams(evCode, param1, param2);
        if ((EC_COMPLETE == evCode) || (EC_USERABORT == evCode))
        { 
            CleanUp();
            break;
        } 
    } 
}

/* WindowProc goes here. Add the following case statement:
        case WM_GRAPHNOTIFY:
            HandleEvent();
            break;
*/

// WinMain goes here.

Last updated on Wednesday, April 13, 2005

© 2005 Microsoft Corporation. All rights reserved.