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

Closed
zhuxb711 opened this issue Sep 20, 2024 · 2 comments · Fixed by #499
Closed

EnumerateChildIds throws NullReferenceException if children not available #488

zhuxb711 opened this issue Sep 20, 2024 · 2 comments · Fixed by #499

Comments

@zhuxb711
Copy link
Contributor

zhuxb711 commented Sep 20, 2024

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.

Correct 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 👍

@zhuxb711
Copy link
Contributor Author

zhuxb711 commented Jan 2, 2025

@dahall Would you please help fix this issue?

zhuxb711 added a commit to zhuxb711/Vanara that referenced this issue Jan 2, 2025
…eferenceException if failed to enum the objects
dahall pushed a commit that referenced this issue Jan 11, 2025
…ceException if failed to enum the objects (#499)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants