我尝试使用流动代码在 C# 中创建 WMPLib.IWMPFolderMonitorServices 来管理包含音乐的文件夹。
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;
}
}
}
我得到例外:
类未注册(HRESULT 异常:0x80040154 (REGDB_E_CLASSNOTREG))
在 :
CoCreateInstance(guid_IWMPPlayer, null, CLSCTX.CLSCTX_INPROC_SERVER, guid_IUnknown, out instancePlayer);
和例外:
值不能为空。参数名称:o
在第二个尝试块中。我在谷歌上搜索这个异常,但我无法修复它。