Repository: microsoft/WindowsAppSDK

Issue #466: Preview 4 - FolderPicker and FilePicker not working / App crashes
Created: 2021-02-15T12:02:31Z
Simple to reproduce:
I have downloaded VisualStudio 2019 Preview Version 16.9 and installed with WinUI 3 preview 4.
I have created a new WinUI Desktop App with .Net 5.
In the method myButton_Click i have this code:
string Filename = "";
var picker = new Windows.Storage.Pickers.FileOpenPicker();
((IInitializeWithWindow)(object)picker).Initialize(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();

and the interface in the same namespace:

[ComImport]
        [Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IInitializeWithWindow
        {
            void Initialize(IntPtr hwnd);
        }
I build my app, the app starts without errors. After i click on the button, the app crashs with this message:
Ausnahmefehler bei 0x76E36D21 (combase.dll) in App1.exe: 0xC000027B: Anwendungsinterne Ausnahme (Parameter: 0x1BD90608, 0x00000001)
Without the interface i receive the message "Invalid Window handle".
The same behavior is with the FolderPicker.

Windows app type:



UWP
Win32




No
Yes







Windows 10 version
Saw the problem?




Insider Build (xxxxx)



October 2020 Update (19042)
Yes


May 2020 Update (19041)



November 2019 Update (18363)



May 2019 Update (18362)



October 2018 Update (17763)



April 2018 Update (17134)



Fall Creators Update (16299)



Creators Update (15063)








Device form factor
Saw the problem?




Desktop
Yes


Xbox



Surface Hub



IoT




This is my first bug report an i hope this is correct.
Best regards
Andy

Duplicate issue #776: Error HRESULT E_FAIL has been returned from a call to a COM component when Windows.Storage.Pickers is using.
Created: 2021-04-28T07:51:35Z
Describe the bug


When I'm trying to use FileSavePicker or FileOpenPicker from the Windows.Storage.Pickers namespace - get this error:
Error HRESULT E_FAIL has been returned from a call to a COM component
Steps to reproduce the bug:

Create an empty ProjectReunion solution
Try to call FilePicker window:

  FileSavePicker fileSavePicker = new FileSavePicker();
  var file = await fileSavePicker.PickSaveFileAsync();
Screenshots


Version Info

NuGet package version:
Microsoft.ProjectReunion 0.5.6




Windows 10 version
Saw the problem?




Insider Build (xxxxx)



May 2020 Update (19041)
Yes


November 2019 Update (18363)



May 2019 Update (18362)



October 2018 Update (17763)

Closing comment (2023-03-06T17:41:02Z): Hi @LeftTwixWand, thanks for reporting this.
This is a dupe of #466, you have to call an InitializeWithWindow API before calling the FilePicker from desktop MSIX apps (this is needed to link the APIs to your HWND, and is by-design and not a bug).
 public sealed partial class MainWindow : Window
 {
      ...

        private async void myButton_Click(object sender, RoutedEventArgs e)
        {
            var filePicker = new FileOpenPicker();

            //Get the Window's HWND
            var hwnd = this.As<IWindowNative>().WindowHandle;

            //Make folder Picker work in Win32

            var initializeWithWindow = filePicker.As<IInitializeWithWindow>();
            initializeWithWindow.Initialize(hwnd);
            filePicker.FileTypeFilter.Add("*");

            var folder = await filePicker.PickSingleFileAsync();
            myText.Text = folder != null ? folder.Path : string.Empty;
        }

        [ComImport]
        [Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IInitializeWithWindow
        {
            void Initialize(IntPtr hwnd);
        }
        [ComImport]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        [Guid("EECDBF0E-BAE9-4CB6-A68E-9598E1CB57BB")]
        internal interface IWindowNative
        {
            IntPtr WindowHandle { get; }
        }
...
}
In the future, we could do some new feature work to eliminate this requirement and make the APIs work more straightforward (there's a pending ask for new file/folder picker APIs #88). But this pattern is required for a few Windows 10 APIs like secondary tiles. I'd suggest upvoting and commenting on #88 if you're interested in an improved API! Thanks!

This solution works for me with FileOpenPicker, but does not work with FileSavePicker. I've tested this with Win UI 3 + .NET 5, 6, and 7 + last 10sih versions of WASDK.