0
votes

I try flowing code to create an WMPLib.IWMPFolderMonitorServices in C# to manage folders contain music.

public partial class MainWindow : Window
    {
        private WMPLib.IWMPPlayer player;
        private WMPLib.IWMPFolderMonitorServices manageFolder;


        [DllImport("ole32.dll", ExactSpelling = true, PreserveSig = false)]
        [return: MarshalAs(UnmanagedType.Interface)]
        static extern void CoCreateInstance([In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
                                            [MarshalAs(UnmanagedType.IUnknown)] object pUnkOuter,
                                             CLSCTX dwClsContext,
                                            [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid,
                                            [MarshalAs(UnmanagedType.IUnknown)] out object rReturnedComObject);

        public MainWindow()
        {
            InitializeComponent();

            Object instancePlayer = null;
            Guid guid_IWMPPlayer = typeof(WMPLib.IWMPPlayer).GUID;
            Guid guid_IUnknown = new Guid("00000000-0000-0000-C000-000000000046"); 
            try
            {
                CoCreateInstance(guid_IWMPPlayer, null, CLSCTX.CLSCTX_INPROC_SERVER, guid_IUnknown, out instancePlayer);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }            
            Type type = Type.GetTypeFromCLSID(guid_IWMPPlayer);

            player = instancePlayer as WMPLib.IWMPPlayer;

            Object instanceFolder = null;
            Guid guid_IWMFolder = typeof(WMPLib.IWMPFolderMonitorServices).GUID;
            IntPtr ptr1, ptr2;

            try
            {
                ptr1 = Marshal.GetComInterfaceForObject(instancePlayer, type);
                Marshal.QueryInterface(ptr1, ref guid_IWMFolder, out ptr2);
                Marshal.PtrToStructure(ptr2, instanceFolder);
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);
            }
            manageFolder = instanceFolder as WMPLib.IWMPFolderMonitorServices;
        }
    }
}

I get exception :

Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))

at :

CoCreateInstance(guid_IWMPPlayer, null, CLSCTX.CLSCTX_INPROC_SERVER, guid_IUnknown, out instancePlayer);

and exception:

Value cannot be null. Parameter name: o

in second try block. I search on google for this exception but i can't fix it.

2
First parameter to CoCreateInstance is a CLSID, not a IID.Simon Mourier
Do you have an other way ??Longit644

2 Answers

0
votes

If you just need to create an instance of the Media Player, just do this:

WindowsMediaPlayerClass wmp = new WindowsMediaPlayerClass();

WindowsMediaPlayerClass has been created when you imported WMPLib.

0
votes

There is no such thing as "creating an instance of an interface" in COM. At best, you create instances of a class (so, an object) that happen to implement an interface. I'm not being pedantic; it's an important distinction.

Knowing the interface you want (presumably because you would like to call a method declared in that interface) tells you absolutely nothing about how to obtain an object that implements it. Maybe you call CoCreateInstance(). Or maybe you call some "GetMonitoringServices()" method on some other object you have to get first. Or maybe you have to call some "Subscribe()" method with a callback function pointer that receives an interface pointer as a parameter. I don't know. You have to read the documentation for the class library to find out.

I don't know anything about WMP automation, but it's not hard to follow the road, starting from the IWMPFolderMonitorServices documentation:

To use this interface, you must create a remoted instance of the Windows Media Player 11 control. For more information about remoting, see Remoting the Windows Media Player Control.

(You probably should read that page linked in the quote as well)

And:

Retrieve a pointer to IWMPFolderMonitorServices by calling QueryInterface through IWMPPlayer.

Ok. Looking at the IWMPPlayer documentation:

Retrieve a pointer to an IWMPPlayer interface by calling the COM CoCreateInstance method

That tells me the code should look something like this:

var player = new WindowsMediaPlayerClass(); 
var monitoringSvc = (WMPLib.IWMPFolderMonitorServices)player;

I haven't tested the code. Some of the details might be off (namespaces, etc.). But you get the idea.

Finally, notice on the first link:

The IWMPFolderMonitorServices interface is deprecated.

You really should research and understand why the interface is deprecated. It could be that the interface still exists but cannot be used; or that the functionality it exposes is permanently disabled in modern versions of Windows; or that it won't be implemented at all by the next version of WMP.