DirectShow(二)GraphEdit
- what
- GraphEdit is a visual tool for building and testing filter graphs.
- You can quickly build and test filter graphs to see if they function as you expect.
- You can even view a filter graph created by an application running in another process.
- ex.
- build filter graph


2.Load a Graph From an External Process(The application must register the filter graph instance in the Running Object Table (ROT).)。代码如下(在上一篇控制台程序中加入注册代码):
#include "stdafx.h"
#include <DShow.h>
#define REGISTER_FILTERGRAPH
DWORD g_dwGraphRegister=0;
HRESULT AddGraphToRot(IUnknown *pUnkGraph, DWORD *pdwRegister);
void RemoveGraphFromRot(DWORD pdwRegister);
int _tmain(int argc, _TCHAR* argv[])
{
IGraphBuilder *pGraph = NULL;
IMediaControl *pControl = NULL;
IMediaEvent *pEvent = NULL;
// Initialize the COM library.
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
printf("ERROR - Could not initialize COM library");
return 1;
}
// Create the filter graph manager and query for interfaces.
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
if (FAILED(hr))
{
printf("ERROR - Could not create the Filter Graph Manager.");
return 1;
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
// Build the graph. IMPORTANT: Change this string to a file on your system.
hr = pGraph->RenderFile(L"E:\\red_sun.wmv", NULL);
#ifdef REGISTER_FILTERGRAPH
if (SUCCEEDED(hr))
{
hr = AddGraphToRot(pGraph, &g_dwGraphRegister);
if (FAILED(hr))
{
printf("Failed to register filter graph with ROT!");
g_dwGraphRegister = 0;
}
}
#endif
if (SUCCEEDED(hr))
{
// Run the graph.
hr = pControl->Run();
if (SUCCEEDED(hr))
{
// Wait for completion.
long evCode;
pEvent->WaitForCompletion(INFINITE, &evCode);
// Note: Do not use INFINITE in a real application, because it
// can block indefinitely.
}
}
#ifdef REGISTER_FILTERGRAPH
if (g_dwGraphRegister)
{
RemoveGraphFromRot(g_dwGraphRegister);
g_dwGraphRegister = 0;
}
#endif
pControl->Release();
pEvent->Release();
pGraph->Release();
CoUninitialize();
return 0;
}
#ifdef REGISTER_FILTERGRAPH
HRESULT AddGraphToRot(IUnknown *pUnkGraph, DWORD *pdwRegister)
{
IMoniker * pMoniker;
IRunningObjectTable *pROT;
if (FAILED(GetRunningObjectTable(0, &pROT)))
{
return E_FAIL;
}
WCHAR wsz[128];
wsprintfW(wsz, L"FilterGraph %08x pid %08x", (DWORD_PTR)pUnkGraph,
GetCurrentProcessId());
HRESULT hr = CreateItemMoniker(L"!", wsz, &pMoniker);
if (SUCCEEDED(hr))
{
// Use the ROTFLAGS_REGISTRATIONKEEPSALIVE to ensure a strong reference
// to the object. Using this flag will cause the object to remain
// registered until it is explicitly revoked with the Revoke() method.
//
// Not using this flag means that if GraphEdit remotely connects
// to this graph and then GraphEdit exits, this object registration
// will be deleted, causing future attempts by GraphEdit to fail until
// this application is restarted or until the graph is registered again.
hr = pROT->Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, pUnkGraph,
pMoniker, pdwRegister);
pMoniker->Release();
}
pROT->Release();
return hr;
}
void RemoveGraphFromRot(DWORD pdwRegister)
{
IRunningObjectTable *pROT;
if (SUCCEEDED(GetRunningObjectTable(0, &pROT)))
{
pROT->Revoke(pdwRegister);
pROT->Release();
}
}
#endif
运行程序,打开GraphEdit,File-->Connect to Remote Graph...即可看到如上图的filter graph,不过它是由我们的程序建立的。

参考:
1、GraphEdit Help
2、DirectShow Help
浙公网安备 33010602011771号