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

Error HRESULT E_FAIL has been returned from a call to a COM component when Windows.Storage.Pickers is using. #776

Closed
LeftTwixWand opened this issue Apr 28, 2021 · 3 comments
Assignees

Comments

@LeftTwixWand
Copy link

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:

  1. Create an empty ProjectReunion solution
  2. Try to call FilePicker window:
  FileSavePicker fileSavePicker = new FileSavePicker();
  var file = await fileSavePicker.PickSaveFileAsync();

Screenshots
image
image

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)
@ghost ghost added the needs-triage label Apr 28, 2021
@andrewleader andrewleader self-assigned this Apr 29, 2021
@andrewleader
Copy link
Contributor

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!

@ghost ghost removed the needs-triage label Apr 29, 2021
@LeftTwixWand
Copy link
Author

Thank You so much!

@cody3b
Copy link

cody3b commented Mar 6, 2023

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.

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

3 participants