Alex S

Alex S

  • NA
  • 24
  • 11.2k

Failed to call COM interface method

Jul 24 2012 7:38 AM
Hello, I create object of CapDevice in Main window (WPF) , and try to call GetBrightness() but it failed with error: Unable to cast COM object of type 'System.__ComObject' to interface type 'DirectShowLib.IAMVideoProcAmp'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{C6E13360-30AC-11D0-A18C-00A0C9118956}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

When I call it inside worker thread everything is OK.
Something wrong with marshalling to another thread ?

public class CapDevice:DependencyObject, IDisposable
{
  Thread worker;
  IGraphBuilder graphBuilder = null;
  ICaptureGraphBuilder2 captureGraphBuilder = null;
  IBaseFilter sourceFilter = null,
  CapGrabber capGrabber = null;
  IAMVideoProcAmp videoProcAmp = null;
  IMediaEventEx mediaEventEx = null;
  DsDevice capDevice = null;




  public void Start()
  {
    if (worker == null)
    {
      worker = new Thread(RunWorker);
      worker.SetApartmentState(ApartmentState.MTA);
      worker.Start();
    }
  }


  public int GetBrightness()
  {
    int br = 0;
    VideoProcAmpFlags CapsFlags;

    if (videoProcAmp != null)
    //THIS CALL FAILED WHEN GetBrightness() CALLED FROM ANOTHER WINDOW
        videoProcAmp.Get(VideoProcAmpProperty.Brightness, out br, out CapsFlags);

    return br;
  }

  void RunWorker()
  {
    // Create DirectShow interfaces
    graphBuilder = (IGraphBuilder)new FilterGraph();
    captureGraphBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();

    // Attach the filter graph to the capture graph
    hr = captureGraphBuilder.SetFiltergraph(graphBuilder);
    DsError.ThrowExceptionForHR(hr);

    // create a video capture/preview device
    sourceFilter = FindCaptureDevice(1);
    videoProcAmp = (IAMVideoProcAmp)sourceFilter;


    graphBuilder = (IGraphBuilder)new FilterGraph();
    captureGraphBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();

    int br = GetBrightness(); //HERE EVERYTHING IS OK
    //.....
    //Do some work
  }

}

Answers (1)