Need a little help : How to instansiate DirectShow filter from C# (1 Viewer)

ziphnor

Retired Team Member
  • Premium Supporter
  • August 4, 2005
    755
    13
    Copenhagen
    Home Country
    Denmark Denmark
    In relation to the Auto zoom/crop discussed in this i need to be able to have MediaPortal call methods on the AutoCrop DirectShow filter(to set properties etc) through a custom interface. I dont really have any experience with COM and only a bit with C# Interop so im having a few difficulties.

    My DirectShow filter called AutoCrop already exposes an interface called IAutopCrop(this was autogenerated by the wizard that set it up). I have added a test method to this interface which i want to call from a test C# program.

    The interface (and related stuff) is declared like this(i simplified it for testing purposes the actual interface has more methods):

    // 4211E930-0EEB-4649-895B-4FD47824646E
    DEFINE_GUID(CLSID_AutoCrop,
    0x4211e930, 0xeeb, 0x4649, 0x89, 0x5b, 0x4f, 0xd4, 0x78, 0x24, 0x64, 0x6e);

    ....

    // C19647D5-A861-4845-97A6-EBD0A135D0BF
    DEFINE_GUID(IID_IAutoCrop,
    0xc19647d5, 0xa861, 0x4845, 0x97, 0xa6, 0xeb, 0xd0, 0xa1, 0x35, 0xd0, 0xbf);

    DECLARE_INTERFACE_(IAutoCrop, IUnknown)
    {
    STDMETHOD(RequestCrop) (
    ) PURE;
    };

    On the C# side i have declared the following(based on how things are done in the MediaPortal DirectShowLib source):

    [Guid("C19647D5-A861-4845-97A6-EBD0A135D0BF"),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IAutoCrop
    {
    [PreserveSig]
    new int RequestCrop();

    }

    I can see in the MediaPortal source code that Marshal.BindMoniker is used to obtain IBaseFilter instances(which should be no different from what i am doing), but im a bit unsure as to what a 'moniker' is. I have the CLSID and IID GUID's (as seen above) but what am i supposed to do with them ? :)

    A little help, or a link to something that explains this clearly would be greatly appreciated.
     

    ziphnor

    Retired Team Member
  • Premium Supporter
  • August 4, 2005
    755
    13
    Copenhagen
    Home Country
    Denmark Denmark
    Hmm, im still a bit clueless about monikers but in C# i managed to create an instance of my AutoCrop filter in this manner:

    const uint CLSCTX_INPROC_SERVER = 1;
    [DllImport("ole32.Dll")]
    static public extern uint CoCreateInstance(ref Guid clsid,
    [MarshalAs(UnmanagedType.IUnknown)] object inner,
    uint context,
    ref Guid uuid,
    [MarshalAs(UnmanagedType.IUnknown)] out object rReturnedComObject);

    static void Main(string[] args)
    {
    Guid clsid = new Guid("4211E930-0EEB-4649-895B-4FD47824646E");
    Guid IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");
    object instance = null;
    CoCreateInstance(ref clsid, null, CLSCTX_INPROC_SERVER, ref IID_IUnknown, out instance);
    IAutoCrop cropper = instance as IAutoCrop;
    cropper.RequestCrop();
    ...

    However, im still hoping someone could explain to me how the different filters are instantiated in MP, ie do i need to use Interop to call CoCreateInstance, or is there already something in Marshal that will do the job?
     

    ziphnor

    Retired Team Member
  • Premium Supporter
  • August 4, 2005
    755
    13
    Copenhagen
    Home Country
    Denmark Denmark
    Thanks, i wasnt aware the DirectShowLib was external to MP.

    Anyway, i searched the MP source for CoCreateInstance, and found a method which is a shortcut to Activator.CreateInstance, so it becomes as simple as this:

    Guid clsid = new Guid("4211E930-0EEB-4649-895B-4FD47824646E");
    object instance = null;
    instance = Activator.CreateInstance(Type.GetTypeFromCLSID(clsid));
    IAutoCrop cropper = instance as IAutoCrop;
    cropper.RequestCrop();

    That is clean enough(ie no redundant Win32 API calls dragged in with interop), thanks for the help, while it only indirectly solved the problem it helps me better understand the code structure of MP.
     

    Users who are viewing this thread

    Top Bottom