'Internal combase.dll error from using a FileOpenPicker

Context: First attempt to use Win-UI 3. Visual Studio 2019, 16.11.8, if it matters. I created a Blank App (WinUI 3 in Desktop) project. It creates a project with a "Clickme" button in the MainWindow.

I added 'using Windows.Storage.Pickers;' and changed the click method like so:

        private async void myButton_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker fop = new FileOpenPicker() { ViewMode = PickerViewMode.Thumbnail, SuggestedStartLocation = PickerLocationId.Downloads };
            fop.FileTypeFilter.Add(".jpeg");
            fop.FileTypeFilter.Add(".jpg");
            fop.FileTypeFilter.Add(".png");

            var v = await fop.PickSingleFileAsync();
            if ( v != null )
            {
                myButton.Content = v.DisplayName;
            }
        }

Only warning tells me Windows version 10.0.17763.0 or later is necessary. I'm running 10.0.19042. The warning was pre-existing from the creation of the project.

Start a debug session. When the button is clicked, I get the following error as PickSingleFileAsync is entered: Unhandled exception at 0x759857B1 (combase.dll) in TestFilePicker.exe: 0xC000027B: An application-internal exception has occurred (parameters: 0x1A8D8770, 0x00000001).

Obviously, something has gone very wrong. Where should I start?



Solution 1:[1]

You need to capture and pass the window's HWND to the FileOpenPicker because reasons.

Adding the following code between creation and method call fixes it:

            var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
            WinRT.Interop.InitializeWithWindow.Initialize(fop, hwnd);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sillyrobot