From 4620e983be2fa6acf877d47aecebcfa817ce7bbc Mon Sep 17 00:00:00 2001 From: David Hall Date: Mon, 17 Jun 2024 13:40:53 -0400 Subject: [PATCH] Undid changes to params of INamespaceWalkCB and INamespaceWalkCB2 --- PInvoke/Shell32/ShObjIdl.INamespaceWalk.cs | 12 +++--- UnitTests/PInvoke/Shell32/ExplorerTests.cs | 67 +++++++++++++----------------- 2 files changed, 35 insertions(+), 44 deletions(-) diff --git a/PInvoke/Shell32/ShObjIdl.INamespaceWalk.cs b/PInvoke/Shell32/ShObjIdl.INamespaceWalk.cs index b66de765..8cdd2e01 100644 --- a/PInvoke/Shell32/ShObjIdl.INamespaceWalk.cs +++ b/PInvoke/Shell32/ShObjIdl.INamespaceWalk.cs @@ -256,7 +256,7 @@ public static partial class Shell32 // https://docs.microsoft.com/en-us/windows/desktop/api/shobjidl_core/nf-shobjidl_core-inamespacewalkcb-founditem HRESULT // FoundItem( IShellFolder *psf, PCUITEMID_CHILD pidl ); [PreserveSig] - HRESULT FoundItem([In] IShellFolder psf, [In] PIDL pidl); + HRESULT FoundItem([In] IShellFolder psf, [In] IntPtr pidl); /// /// Called when a folder is about to be entered during a namespace walk. Use this method for any initialization of the retrieved item. @@ -276,7 +276,7 @@ public static partial class Shell32 // https://docs.microsoft.com/en-us/windows/desktop/api/shobjidl_core/nf-shobjidl_core-inamespacewalkcb-enterfolder HRESULT // EnterFolder( IShellFolder *psf, PCUITEMID_CHILD pidl ); [PreserveSig] - HRESULT EnterFolder([In] IShellFolder psf, [In] PIDL pidl); + HRESULT EnterFolder([In] IShellFolder psf, [In] IntPtr pidl); /// /// Called after a namespace walk through a folder. Use this method to perform any necessary cleanup following the actions @@ -297,7 +297,7 @@ public static partial class Shell32 // https://docs.microsoft.com/en-us/windows/desktop/api/shobjidl_core/nf-shobjidl_core-inamespacewalkcb-leavefolder HRESULT // LeaveFolder( IShellFolder *psf, PCUITEMID_CHILD pidl ); [PreserveSig] - HRESULT LeaveFolder([In] IShellFolder psf, [In] PIDL pidl); + HRESULT LeaveFolder([In] IShellFolder psf, [In] IntPtr pidl); /// /// Initializes the window title and cancel button text of the progress dialog box displayed during the namespace walk. @@ -355,7 +355,7 @@ public static partial class Shell32 // https://docs.microsoft.com/en-us/windows/desktop/api/shobjidl_core/nf-shobjidl_core-inamespacewalkcb-founditem HRESULT // FoundItem( IShellFolder *psf, PCUITEMID_CHILD pidl ); [PreserveSig] - new HRESULT FoundItem([In] IShellFolder psf, [In] PIDL pidl); + new HRESULT FoundItem([In] IShellFolder psf, [In] IntPtr pidl); /// /// Called when a folder is about to be entered during a namespace walk. Use this method for any initialization of the retrieved item. @@ -375,7 +375,7 @@ public static partial class Shell32 // https://docs.microsoft.com/en-us/windows/desktop/api/shobjidl_core/nf-shobjidl_core-inamespacewalkcb-enterfolder HRESULT // EnterFolder( IShellFolder *psf, PCUITEMID_CHILD pidl ); [PreserveSig] - new HRESULT EnterFolder([In] IShellFolder psf, [In] PIDL pidl); + new HRESULT EnterFolder([In] IShellFolder psf, [In] IntPtr pidl); /// /// Called after a namespace walk through a folder. Use this method to perform any necessary cleanup following the actions @@ -396,7 +396,7 @@ public static partial class Shell32 // https://docs.microsoft.com/en-us/windows/desktop/api/shobjidl_core/nf-shobjidl_core-inamespacewalkcb-leavefolder HRESULT // LeaveFolder( IShellFolder *psf, PCUITEMID_CHILD pidl ); [PreserveSig] - new HRESULT LeaveFolder([In] IShellFolder psf, [In] PIDL pidl); + new HRESULT LeaveFolder([In] IShellFolder psf, [In] IntPtr pidl); /// /// Initializes the window title and cancel button text of the progress dialog box displayed during the namespace walk. diff --git a/UnitTests/PInvoke/Shell32/ExplorerTests.cs b/UnitTests/PInvoke/Shell32/ExplorerTests.cs index 740bf53f..f32601e9 100644 --- a/UnitTests/PInvoke/Shell32/ExplorerTests.cs +++ b/UnitTests/PInvoke/Shell32/ExplorerTests.cs @@ -16,7 +16,7 @@ public class ExplorerTests using var pFolder = ComReleaserFactory.Create((IShellFolder2)new MyDocuments()); INamespaceWalk walk = new(); WalkerCallback callback = new("Progress Title", "Cancel"); - List mycnt = new(); + List mycnt = []; callback.ItemFound += (f, p) => mycnt.Add(pFolder.Item.GetDisplayNameOf(SHGDNF.SHGDN_FORPARSING, p) ?? ""); Assert.That(walk.Walk(pFolder.Item, NAMESPACEWALKFLAG.NSWF_NONE_IMPLIES_ALL | NAMESPACEWALKFLAG.NSWF_DONT_ACCUMULATE_RESULT | NAMESPACEWALKFLAG.NSWF_SHOW_PROGRESS, 0, callback), ResultIs.Successful); Assert.That(mycnt.Count, Is.GreaterThan(0)); @@ -40,24 +40,12 @@ public class ExplorerTests using var pFolder = ComReleaserFactory.Create((IShellFolder2)new MyDocuments()); ShellNamespaceWalker walker = new(pFolder.Item, 0, NAMESPACEWALKFLAG.NSWF_NONE_IMPLIES_ALL | NAMESPACEWALKFLAG.NSWF_DONT_ACCUMULATE_RESULT); var items = walker.ToArray(); - Assert.That(items, Has.Length.GreaterThanOrEqualTo(30)); + Assert.That(items, Has.Length.GreaterThanOrEqualTo(1)); items.WriteValues(); } - - public class ShellNamespaceWalker : IEnumerable, IAsyncEnumerable + public class ShellNamespaceWalker(object objToWalk, int depth, NAMESPACEWALKFLAG flags) : IEnumerable, IAsyncEnumerable { - private readonly object objToWalk; - private readonly int depth; - private readonly NAMESPACEWALKFLAG flags; - - public ShellNamespaceWalker(object objToWalk, int depth, NAMESPACEWALKFLAG flags) - { - this.objToWalk = objToWalk; - this.depth = depth; - this.flags = flags; - } - IAsyncEnumerator IAsyncEnumerable.GetAsyncEnumerator(CancellationToken cancellationToken) => throw new NotImplementedException(); IEnumerator IEnumerable.GetEnumerator() @@ -83,34 +71,37 @@ public class ExplorerTests IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)this).GetEnumerator(); } - class WalkerCallback : INamespaceWalkCB2, IActionProgress + private class WalkerCallback(string progressDlgTitle = "", string progressDlgCancelText = "Cancel") : INamespaceWalkCB2, IActionProgress { - private readonly string cancel; - private readonly string title; - - public WalkerCallback(string progressDlgTitle = "", string progressDlgCancelText = "Cancel") - { - cancel = progressDlgCancelText; - title = progressDlgTitle; - } - - public event Action? ItemFound; - public event Action? FolderEntered; - public event Action? FolderLeft; public event Action? Completed; - public HRESULT FoundItem([In] IShellFolder psf, [In] IntPtr pidl) { ItemFound?.Invoke(psf, new PIDL(pidl, true)); return HRESULT.S_OK; } - public HRESULT EnterFolder([In] IShellFolder psf, [In] IntPtr pidl) { FolderEntered?.Invoke(psf, new PIDL(pidl, true)); return HRESULT.S_OK; } - public HRESULT LeaveFolder([In] IShellFolder psf, [In] IntPtr pidl) { FolderLeft?.Invoke(psf, new PIDL(pidl, true)); return HRESULT.S_OK; } - public HRESULT InitializeProgressDialog([MarshalAs(UnmanagedType.LPWStr), Out] out string ppszTitle, [MarshalAs(UnmanagedType.LPWStr), Out] out string ppszCancel) - { ppszTitle = title; ppszCancel = cancel; return HRESULT.S_OK; } - public HRESULT WalkComplete(HRESULT hr) { Completed?.Invoke(hr); return HRESULT.S_OK; } + public event Action? FolderEntered; + + public event Action? FolderLeft; + + public event Action? ItemFound; public void Begin(SPACTION action, SPBEGINF flags) { } - public void UpdateProgress(ulong ulCompleted, ulong ulTotal) { } - public void UpdateText(SPTEXT sptext, [In, MarshalAs(UnmanagedType.LPWStr)] string pszText, [MarshalAs(UnmanagedType.Bool)] bool fMayCompact) { } - public bool QueryCancel() => false; - public void ResetCancel() { } + public void End() { } + + public HRESULT EnterFolder([In] IShellFolder psf, [In] IntPtr pidl) { FolderEntered?.Invoke(psf, new PIDL(pidl, true)); return HRESULT.S_OK; } + + public HRESULT FoundItem([In] IShellFolder psf, [In] IntPtr pidl) { ItemFound?.Invoke(psf, new PIDL(pidl, true)); return HRESULT.S_OK; } + + public HRESULT InitializeProgressDialog([MarshalAs(UnmanagedType.LPWStr), Out] out string ppszTitle, [MarshalAs(UnmanagedType.LPWStr), Out] out string ppszCancel) + { ppszTitle = progressDlgTitle; ppszCancel = progressDlgCancelText; return HRESULT.S_OK; } + + public HRESULT LeaveFolder([In] IShellFolder psf, [In] IntPtr pidl) { FolderLeft?.Invoke(psf, new PIDL(pidl, true)); return HRESULT.S_OK; } + + public bool QueryCancel() => false; + + public void ResetCancel() { } + + public void UpdateProgress(ulong ulCompleted, ulong ulTotal) { } + + public void UpdateText(SPTEXT sptext, [In, MarshalAs(UnmanagedType.LPWStr)] string pszText, [MarshalAs(UnmanagedType.Bool)] bool fMayCompact) { } + + public HRESULT WalkComplete(HRESULT hr) { Completed?.Invoke(hr); return HRESULT.S_OK; } } } \ No newline at end of file