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

FileOpenPicker throw Invalid Window handle exception in Project Reunion 0.5 desktop app #4519

Closed
gcaughey opened this issue Mar 15, 2021 · 8 comments
Labels
appModel-win32 Exclusive to WinUI 3 Win32 Desktop apps duplicate This issue or pull request already exists product-winui3 WinUI 3 issues

Comments

@gcaughey
Copy link

Describe the bug

FileOpenPicker throws Invalid Window handle exception on PickSingleFileAsync()

Steps to reproduce the bug

Steps to reproduce the behavior:

  1. Step through the following code:

var picker = new FileOpenPicker();
var file = await picker.PickSingleFileAsync();

Expected behavior

picker.PickerSingleFileAsync() should show FileOpenPicker

Screenshots

Version Info

NuGet package version:

WinUI 3 - Project Reunion 0.5 Preview: 0.5.0-prerelease

Windows app type:

UWP Win32
No Yes

This fails running on Win32 but does not fail running in UWP app model.

Windows 10 version Saw the problem?
Insider Build (21332) Yes
October 2020 Update (19042)
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

Additional context

This app runs without error when running under UWP.

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Mar 15, 2021
@Marv51
Copy link
Contributor

Marv51 commented Mar 15, 2021

I had the same issue and this worked for me: #4167

        [ComImport, System.Runtime.InteropServices.Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IInitializeWithWindow
        {
            void Initialize([In] IntPtr hwnd);
        }

        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto, PreserveSig = true, SetLastError = false)]
        public static extern IntPtr GetActiveWindow();

        var picker = new FileOpenPicker();
        var initializeWithWindowWrapper = picker.As<IInitializeWithWindow>();
        initializeWithWindowWrapper.Initialize(GetActiveWindow());
        var file = await picker.PickSingleFileAsync();

@gcaughey
Copy link
Author

I'm getting a compiler error on this line of code:
var initializeWithWindowWrapper = picker.As<IInitializeWithWindow>();
Member 'FileOpenPicker.As()' cannot be accessed with an instance reference.

Interestingly the ContentDialog issue that was mentioned in your issue appears to work now.

@FreddyDgh
Copy link

@gcaughey add using WinRT; to use the As<T>() extension method.

@gcaughey
Copy link
Author

Thanks - that does indeed solve the compiler error. But now I'm getting a new exception on PickSingleFileAsync: Error HRESULT E_FAIL has been returned from a call to a COM component.

@FreddyDgh
Copy link

@gcaughey I just tried it out and unfortunately, it doesn't look like the FileTypeFilter gets set automatically either. So, you'll need to add yet another line to your current monstrosity: picker.FileTypeFilter.Add("*");

@gcaughey
Copy link
Author

Excellent! This makes a very usable workaround. Thanks!

@StephenLPeters StephenLPeters added appModel-win32 Exclusive to WinUI 3 Win32 Desktop apps product-winui3 WinUI 3 issues duplicate This issue or pull request already exists and removed needs-triage Issue needs to be triaged by the area owners labels Mar 16, 2021
@StephenLPeters
Copy link
Contributor

Closing as a duplicate of #4167

@YareYareO
Copy link

I have a WinUi3 project and this worked for me just now. FYI. Thanks alot to everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
appModel-win32 Exclusive to WinUI 3 Win32 Desktop apps duplicate This issue or pull request already exists product-winui3 WinUI 3 issues
Projects
None yet
Development

No branches or pull requests

5 participants