public int EnumObjects(IntPtr hwnd, ShellAPI.SHCONTF grfFlags, out IntPtr enumIDList) { checkDisposed(); return(_iShellFolder2.EnumObjects(hwnd, grfFlags, out enumIDList)); }
public void CanEnumerateDesktopFolders() { // Defines the type of items that we want to retieve below the desktop root item const SHCONTF flags = SHCONTF.NONFOLDERS | SHCONTF.FOLDERS | SHCONTF.INCLUDEHIDDEN; // Get the desktop root folder. IntPtr ptrDesktopFolder = default(IntPtr); IntPtr enumerator = default(IntPtr); IShellFolder2 iDesktopFolder = null; // Enumerate over children of given shell folder item using this interface // https://msdn.microsoft.com/en-us/library/windows/desktop/bb761983(v=vs.85).aspx IEnumIDList enumIDs = null; try { HRESULT hr = NativeMethods.SHGetDesktopFolder(out ptrDesktopFolder); Assert.IsTrue(hr == HRESULT.S_OK); if (ptrDesktopFolder != IntPtr.Zero) { iDesktopFolder = (IShellFolder2)Marshal.GetTypedObjectForIUnknown(ptrDesktopFolder, typeof(IShellFolder2)); } Assert.IsTrue(iDesktopFolder != null); // Create an enumerator and enumerate over each item. hr = iDesktopFolder.EnumObjects(IntPtr.Zero, flags, out enumerator); Assert.IsTrue(hr == HRESULT.S_OK); // Convert enum IntPtr to interface enumIDs = (IEnumIDList)Marshal.GetTypedObjectForIUnknown(enumerator, typeof(IEnumIDList)); Assert.IsTrue(enumIDs != null); uint fetched, count = 0; IntPtr apidl = default(IntPtr); // Get one item below desktop root at a time and process by getting its display name for (; enumIDs.Next(1, out apidl, out fetched) == HRESULT.S_OK; count++) { if (fetched <= 0) // End this loop if no more items are available { break; } IntPtr ptrStr = default(IntPtr); // get strings for this item try { string displayName = null, parseName = null; ptrStr = Marshal.AllocCoTaskMem(NativeMethods.MAX_PATH * 2 + 4); Marshal.WriteInt32(ptrStr, 0, 0); StringBuilder buf = new StringBuilder(NativeMethods.MAX_PATH); // The apidl ITEMIDLIST structures returned in the array are relative to // the IShellFolder being enumerated. if (iDesktopFolder.GetDisplayNameOf(apidl, SHGDNF.SHGDN_NORMAL, ptrStr) == HRESULT.S_OK) { NativeMethods.StrRetToBuf(ptrStr, ptrDesktopFolder, buf, NativeMethods.MAX_PATH); displayName = buf.ToString(); } if (iDesktopFolder.GetDisplayNameOf(apidl, SHGDNF.SHGDN_FORPARSING, ptrStr) == HRESULT.S_OK) { NativeMethods.StrRetToBuf(ptrStr, ptrDesktopFolder, buf, NativeMethods.MAX_PATH); parseName = buf.ToString(); } Assert.IsFalse(string.IsNullOrEmpty(displayName)); Assert.IsFalse(string.IsNullOrEmpty(parseName)); } finally { ptrStr = PidlManager.FreeCoTaskMem(ptrStr); apidl = PidlManager.FreeCoTaskMem(apidl); } } // There should be more than one item below the desktop root item // 'My PC', 'Recycle Bin', and 'Network' are already three Assert.IsTrue(count > 2); } finally { if (enumerator != default(IntPtr)) { Marshal.Release(enumerator); } if (iDesktopFolder != null) { Marshal.ReleaseComObject(iDesktopFolder); } if (ptrDesktopFolder != default(IntPtr)) { Marshal.Release(ptrDesktopFolder); } } }