Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnumerateChildIds throws NullReferenceException if children not available #488

Open
zhuxb711 opened this issue Sep 20, 2024 · 1 comment

Comments

@zhuxb711
Copy link

finally { Marshal.ReleaseComObject(eo!); }

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
	}
@tajbender
Copy link
Contributor

I'll take a look at it 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants