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.