From 9ac06aab01a90d8e4ba1e74dc70d9f0ae31316bb Mon Sep 17 00:00:00 2001 From: "seer-by-sentry[bot]" <157164994+seer-by-sentry[bot]@users.noreply.github.com> Date: Fri, 25 Jul 2025 02:23:00 +0000 Subject: [PATCH 1/2] Fix: Properly handle cancellation and null shell items in WindowsDialogService --- .../Services/Windows/WindowsDialogService.cs | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Files.App/Services/Windows/WindowsDialogService.cs b/src/Files.App/Services/Windows/WindowsDialogService.cs index 60ee67f767c6..649e4f2f4a7c 100644 --- a/src/Files.App/Services/Windows/WindowsDialogService.cs +++ b/src/Files.App/Services/Windows/WindowsDialogService.cs @@ -65,13 +65,17 @@ public unsafe bool Open_FileOpenDialog(nint hWnd, bool pickFoldersOnly, string[] pDialog.Get()->SetDefaultFolder(pDefaultFolderShellItem.Get()); // Show the dialog - pDialog.Get()->Show(new HWND(hWnd)); + hr = pDialog.Get()->Show(new HWND(hWnd)); + if (hr.Value == unchecked((int)0x800704C7)) // HRESULT_FROM_WIN32(ERROR_CANCELLED) + return false; + + hr.ThrowOnFailure(); // Get the file that user chose using ComPtr pResultShellItem = default; pDialog.Get()->GetResult(pResultShellItem.GetAddressOf()); - if (pResultShellItem.Get() == null) - throw new COMException("FileSaveDialog returned invalid shell item."); + if (pResultShellItem.Get() is null) + throw new COMException("FileOpenDialog returned invalid shell item."); pResultShellItem.Get()->GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out var lpFilePath); filePath = lpFilePath.ToString(); @@ -135,13 +139,17 @@ public unsafe bool Open_FileSaveDialog(nint hWnd, bool pickFoldersOnly, string[] pDialog.Get()->SetDefaultFolder(pDefaultFolderShellItem.Get()); // Show the dialog - pDialog.Get()->Show(new HWND(hWnd)); + hr = pDialog.Get()->Show(new HWND(hWnd)); + if (hr.Value == unchecked((int)0x800704C7)) // HRESULT_FROM_WIN32(ERROR_CANCELLED) + return false; + + hr.ThrowOnFailure(); // Get the file that user chose using ComPtr pResultShellItem = default; pDialog.Get()->GetResult(pResultShellItem.GetAddressOf()); - if (pResultShellItem.Get() == null) - throw new COMException("FileSaveDialog returned invalid shell item."); + if (pResultShellItem.Get() is null) + throw new COMException("FileOpenDialog returned invalid shell item."); pResultShellItem.Get()->GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out var lpFilePath); filePath = lpFilePath.ToString(); From 456250699a013fe60bfaff8741ea91a95484df5b Mon Sep 17 00:00:00 2001 From: Yair <39923744+yaira2@users.noreply.github.com> Date: Mon, 28 Jul 2025 10:13:28 -0400 Subject: [PATCH 2/2] Update src/Files.App/Services/Windows/WindowsDialogService.cs Co-authored-by: hishitetsu <66369541+hishitetsu@users.noreply.github.com> Signed-off-by: Yair <39923744+yaira2@users.noreply.github.com> --- src/Files.App/Services/Windows/WindowsDialogService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Files.App/Services/Windows/WindowsDialogService.cs b/src/Files.App/Services/Windows/WindowsDialogService.cs index 649e4f2f4a7c..57b16c37e0fe 100644 --- a/src/Files.App/Services/Windows/WindowsDialogService.cs +++ b/src/Files.App/Services/Windows/WindowsDialogService.cs @@ -149,7 +149,7 @@ public unsafe bool Open_FileSaveDialog(nint hWnd, bool pickFoldersOnly, string[] using ComPtr pResultShellItem = default; pDialog.Get()->GetResult(pResultShellItem.GetAddressOf()); if (pResultShellItem.Get() is null) - throw new COMException("FileOpenDialog returned invalid shell item."); + throw new COMException("FileSaveDialog returned invalid shell item."); pResultShellItem.Get()->GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out var lpFilePath); filePath = lpFilePath.ToString();