2

So I was looking at this example code for DirectShow.Net, specifically their PlayCap example under the Capture folder example. You can download the samples here Its in C#. It does some interesting things with casting objects to interfaces. When I tried to recreate it faithfully in F#, the objects would not cast properly.

C#:

    IVideoWindow  videoWindow = null;
    IMediaControl mediaControl = null;
    IMediaEventEx mediaEventEx = null;
    IGraphBuilder graphBuilder = null;
    ICaptureGraphBuilder2 captureGraphBuilder = null;

and then later in GetInterfaces():

      this.graphBuilder = (IGraphBuilder) new FilterGraph();
      this.captureGraphBuilder = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();
      this.mediaControl = (IMediaControl) this.graphBuilder;
      this.videoWindow = (IVideoWindow) this.graphBuilder;
      this.mediaEventEx = (IMediaEventEx) this.graphBuilder;

So what I did for my code was this in F#:

    let mutable videoWindow:  IVideoWindow = null;
    let mutable mediaControl: IMediaControl  = null;
    let mutable mediaEventEx: IMediaEventEx  = null;
    let mutable graphBuilder: IGraphBuilder  = null;
    let mutable captureGraphBuilder: ICaptureGraphBuilder2  = null;

And later in GetInterfaces():

        graphBuilder <- new FilterGraph() :>  IGraphBuilder
        captureGraphBuilder <-  new CaptureGraphBuilder2() :> ICaptureGraphBuilder2
        mediaControl <- graphBuilder :> IMediaControl;
        videoWindow <- graphBuilder :> IVideoWindow;
        mediaEventEx <- graphBuilder :> IMediaEventEx;

It looked like a faithful recreation. Not even using functional style either. I was looking at the msdn on casting in F# to see if I was doing it correctly. It looks like I am, though they have 1 example and it is incredibly minimal in nature.

The problem is I get the error:

Type constraint mismatch. The type 'FilterGraph' is not compatible with the type 'IGraphBuilder'

I get a similar error for each one. I have tried downcasting too as well using :?>

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Alexander Ryan Baggett
  • 2,347
  • 4
  • 34
  • 61
  • 1
    Perhaps this Q [C# and F# casting - specifically the 'as' keyword](http://stackoverflow.com/questions/2361851/c-sharp-and-f-casting-specifically-the-as-keyword) has relevant answers, since you are interested in "COM casting" through underlying COM `QueryInterface` call. – Roman R. Jan 15 '15 at 08:59

1 Answers1

2

I found myself encountering a similar issue, it turns out that in C# it's possible to add a GUID attribute to a COM interface and a COM implementation and the class would appear as a valid implementation of the interface. The F# type system is, however, stricter and doesn't allow this. You can assign them by using the unbox function as such

graphBuilder <- unbox (new FilterGraph())
captureGraphBuilder <- unbox (new CaptureGraphBuilder2())
bruinbrown
  • 1,006
  • 2
  • 8
  • 13
  • I appreciate the answer so that I will know how to solve future issues of this type. But for now, I have switched to using Aforge's implementation of Directshow.NET for video capture since it is much simpler and less boilerplate code to use. – Alexander Ryan Baggett Jan 15 '15 at 18:50