Click here to Skip to main content
12,077,332 members (42,469 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# COM
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.
Posted 18-Apr-13 0:34am
Edited 18-Apr-13 0:37am
v3
Comments
Kenneth Haugland 18-Apr-13 5:42am
   
I assume the the , null, is the problem?

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

player = instancePlayer as WMPLib.IWMPPlayer; //Here
            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)
            {
It seems like the object "player" is not initialized right and stays null.
Which basically means it is not initialized on this line:
CoCreateInstance(guid_IWMPPlayer, null, CLSCTX.CLSCTX_INPROC_SERVER, guid_IUnknown, out instancePlayer);
Check if "instancePlayer" is not null after executing this line.
  Permalink  
v2
Comments
longit644 18-Apr-13 8:00am
   
How to initialize object "player" right ?
CodezBob 18-Apr-13 9:25am
   
Search google.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160212.1 | Last Updated 18 Apr 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100