Vanara icon indicating copy to clipboard operation
Vanara copied to clipboard

EnumerateChildIds throws NullReferenceException if children not available

Open zhuxb711 opened this issue 1 year ago • 1 comments

https://github.com/dahall/Vanara/blob/3da05fe4d87bc5de96527fad5c9b7e6058690deb/Windows.Shell.Common/ShellObjects/ShellFolder.cs#L213

StackTrace:
   at System.Runtime.InteropServices.Marshal.ReleaseComObject(Object o)
   at Vanara.Windows.Shell.ShellFolder.<EnumerateChildIds>d__23.<>m__Finally1()
   at Vanara.Windows.Shell.ShellFolder.<EnumerateChildIds>d__23.MoveNext()
   at Vanara.Windows.Shell.ShellFolder.<EnumerateChildren>d__22.MoveNext()

The root cause should be if iShellFolder.EnumObjects(parentWindow, (SHCONTF)filter, out var eo).Failed == true then it would not yield break.

Corrent one should be:

	public IEnumerable<PIDL> EnumerateChildIds(FolderItemFilter filter /*= FolderItemFilter.Folders | FolderItemFilter.IncludeHidden | FolderItemFilter.NonFolders | FolderItemFilter.IncludeSuperHidden */, HWND parentWindow = default, int fetchSize = 20)
	{
		if (iShellFolder.EnumObjects(parentWindow, (SHCONTF)filter, out var eo).Failed)
	        {
                    Debug.WriteLine($"Unable to enum children in folder.");
                    yield break;
                }

		try
		{
			foreach (PIDL p in eo!.Enumerate(fetchSize))
				yield return p;
		}
		finally { Marshal.ReleaseComObject(eo!); } // Maybe should not call ReleaseComObject in case RCW exception
	}

zhuxb711 avatar Sep 20 '24 18:09 zhuxb711

I'll take a look at it 👍

tajbender avatar Sep 23 '24 20:09 tajbender

@dahall Would you please help fix this issue?

zhuxb711 avatar Jan 02 '25 09:01 zhuxb711