Alexander Ryan Baggett
Alexander Ryan Baggett

Reputation: 2397

DirectShow.Net interfaces issue in F#

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 :?>

Upvotes: 2

Views: 123

Answers (1)

bruinbrown
bruinbrown

Reputation: 1006

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())

Upvotes: 2

Related Questions