Alright, well g_pControl is NULL to start with and that's the first function I call involving it. So it should just be as follows, right?
hr = g_pGraph->QueryInterface(IID_IMediaControl, (void **)&g_pControl);
Correct. The call to QueryInterface() will AddRef() g_pControl for you. So now when you no longer need it, you call g_pControl->Release(). AddRef() and Release() must be paired, so it gets confusing as to who calls which at which time. Basically though, output parameters are always AddRef()'d for you by the method you call. Input parameters are expected to be AddRef()'d by you so that the method called can use the parameter during its implicit lifetime without error. If the method you call wants to save it for further work, it will AddRef() and Release() it on it's own. All you need to be sure of is that during the lifetime of the call you KNOW you're making, the parameter is AddRef()'d. After that you can Release() it, keep it around and Release() it later, or whatever. There are subtle variations to these rules when you get into cross-apartment calls, outbound events, etc, but the main rules are as I have said.
As for VB similaries, I haven't used VB in years, but I'm pretty sure this is close to accurate...
' This is somewhere else... VB is hiding the fact that FilgraphManager is a class object
Dim g_Graph As FilgraphManager
Set g_Graph = New FilgraphManager
' Then you have...
Dim g_Control As MediaControl
Set g_Control = g_Graph
To accomplish the same thing in C++, you have this...
// Create a new instance of the FilterGraph (FilgraphManager) class, and request an initial reference to its IGraphBuilder interface
IGraphBuilder* g_pGraph = NULL;
CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&g_pGraph);
// Now ask the same instance for a reference to its IMediaControl interface...
IMediaControl* g_pControl = NULL;
g_pGraph->QueryInterface(IID_IMediaControl, (void**)&g_pGraph);
In VB you use
New to create objects. In COM, you can use several different methods, but the primary one is
CoCreateInstanceEx.
In VB you use
Set to convert from one COM interface to another. In COM, you use
QueryInterface.
You picked a complicated set of technologies to get your feet wet with C++. COM is tough, DirectShow is also tough, DirectShow in C++ via COM is near torture.
- Jeremy