[SOLVED] Creating and connecting DirectShow filter: how to implement CreateInstance()?

Issue

This Content is from Stack Overflow. Question asked by Legat

I want to write my own DirectShow filter to pull out packets of information for my own purposes. To do this, I used the guide to creating filters.
I repeated stage 1-5, almost repeated stage 6. Failed to implement CreateInstance. Can’t instantiate the class because MSDN doesn’t pass parameters, but Delphi requires it. I used regsvr32, unfortunately I don’t know how to connect my DLL and I can’t think of it. The DSFMgr program also does not see my filter.
I read how filters are connected, tried to implement various searches, it’s useless. Tried to connect manually via CLSID. Everything is useless. I know the answer is somewhere on the surface, but I don’t see it. I can’t figure out how DirectShow should see my library if it didn’t exist in the first place. It’s not logical. I’ve been trying to implement this for a very long time, but it doesn’t work, I’m stuck.
Please don’t recommend FFmpeg and the like. I don’t want to use third party libraries. DirectX, as far as I know it’s built-in.

CUnknown * WINAPI CRleFilter::CreateInstance(LPUNKNOWN pUnk, HRESULT *pHr)
{
    CRleFilter *pFilter = new CRleFilter();
    if (pFilter== NULL) 
    {
        *pHr = E_OUTOFMEMORY;
    }
    return pFilter;
}

Implemented like this, but it doesn’t work. Errors: no variables sent

function TCRleFilter.CreateInstance(pUnk: PPUnknown;
  pHr: HRESULT): PUnknown;
var
  pFilter: TCRleFilter;
  begin
  pFilter:=   TCRleFilter.Create();
  if pFilter = nil then
  pHr:= E_OUTOFMEMORY;

  Result:= pFilter;

end;

I think at least a logical explanation should suffice



Solution

Your class inherites from TBCTransformFilter and the needed parameters are defined as:

constructor TBCTransformFilter.Create(ObjectName: string; unk: IUnKnown; const clsid: TGUID);

Untested, but it should be much more correct than your attempt:

function TCRleFilter.CreateInstance
( pUnk: IUnknown  // LPUNKNOWN
; var pHr: HRESULT  // Pointer to variable = VAR
): PUnknown;  // Pointer
var
  oFilter: TCRleFilter;  // Object, not pointer
begin
  try  // Failing constructors throw exceptions
    oFilter:= TCRleFilter.Create( 'my RLE encoder', pUnk, CLSID_CRleFilter );
    result:= oFilter;  // In doubt cast via "PUnknown(oFilter)"
  except  // Constructor failed, oFilter is undefined
    pHr:= E_OUTOFMEMORY;
    result:= nil;
  end;
end;

The var parameter ensures that assigned values inside the function also live on outside the function – otherwise you’d only have a local variable. Which is also the point (haha) of pointers in C++ parameters.


This Question was asked in StackOverflow by Legat and Answered by AmigoJack It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?