Skip to content

Conversation

OmerMor
Copy link

@OmerMor OmerMor commented Aug 17, 2012

There's a trick involving the CoClassAttribute that allows instantiating (COM) interfaces in .NET.
You can read about it here:

The trick can make the usage of this lib easier: you could instantiate the interface instead of the class, and save the need to cast it before you can call any methods on it.

Here is an example for changing one com class (StreamBufferSource):

    /// <summary>
    /// CLSID_StreamBufferSource
    /// </summary>
    [ComImport, Guid("C9F5FE02-F851-4eb5-99EE-AD602AF1E619")]
    public class StreamBufferSourceImpl // used to be called StreamBufferSource
    {
    }

    /// <summary>
    /// CLSID_StreamBufferSource
    /// </summary>
    [ComImport]
    [CoClass(typeof(StreamBufferSourceImpl))]
    [Guid("C9F5FE02-F851-4eb5-99EE-AD602AF1E619")]
    public interface StreamBufferSource : IAMFilterMiscFlags,
                                          IBaseFilter,
                                          IFileSourceFilter,
                                          ISpecifyPropertyPages,
                                          IStreamBufferMediaSeeking2,
                                          IStreamBufferInitialize,
                                          IStreamBufferSource,
                                          ISBE2GlobalEvent,
                                          ISBE2SpanningEvent,
                                          ISBE2Crossbar
    {
    }

With this declaration in place, we can use it like this:

    var source = new StreamBufferSource();
    source.SetStreamSink(sink);

Notice there is no need to cast here. We also enjoy the power of intellisense with our coclass objects.

In this pull request I started adding co-classes (3 so far), hoping it'll drive someone (me?) to continue and adopt this for all the com objects in this library, 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant