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

[Bug] FilePicker throws COMException on Windows #2194

Closed
orryverducci opened this issue Aug 21, 2021 · 16 comments
Closed

[Bug] FilePicker throws COMException on Windows #2194

orryverducci opened this issue Aug 21, 2021 · 16 comments
Assignees
Labels
area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info platform/windows 🪟 s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working

Comments

@orryverducci
Copy link

Description

On WinUI projects on the Windows platform, using the PickAsync and PickMultipleAsync methods from the FilePicker class in Essentials results in a System.Runtime.InteropServices.COMException being thrown. The error message returned in the exception is "Invalid window handle. (0x80070578)".

I'm not sure if the issue lies within MAUI or WinUI itself.

Steps to Reproduce

  1. Create a new MAUI project and add a button to the main page
  2. Add a clicked event to the button with the following handler method:
    async private void ButtonClicked(object sender, EventArgs args)
    {
        FileResult file = await FilePicker.PickAsync();
    }
  3. Run the app on Windows and click the button

Expected Behavior

The file open dialog should appear.

Actual Behavior

An exception is thrown and the application crashes.

Basic Information

  • Version with issue: MAUI Preview 7
  • Last known good version: Not known
  • IDE: Visual Studio 2022 Preview 3.1
@josephatkuvio
Copy link

This appears to be a duplicate of #2102 (an Android bug), but it's interesting that it's also presenting in Windows.

I wonder if the internals of FilePicker try to store a reference to the parent window? My issue happened with the MSAL authentication library, but only on Android, which needs a parent window reference to properly open the system browser. It did not show up on iOS, which doesn't need that reference. So I'm wondering if the MAUI Parent Window logic is the common thread here.

@Eilon Eilon added area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info platform/windows 🪟 labels Sep 16, 2021
@Crauzer
Copy link

Crauzer commented Sep 19, 2021

This also happens for me on .NET 6 RC1 and VS2022 preview.
I have a MAUI Razor app and I'm trying to use the FolderPicker and FilePicker classes from Windows.Storage.Pickers but they always throw an exception when you try to call their pick methods.

I've also made sure to initialize the pickers with the HWND of the app window - https://docs.microsoft.com/en-us/windows/apps/desktop/modernize/winrt-com-interop-csharp

@joshfourbadideas
Copy link

Where is this at? Do we have a timeline for when the fix to these will drop?

@aokocax
Copy link

aokocax commented Jan 1, 2022

Hello, I am getting similar error on Windows platform.
filepicker
.
When I add this code to Mainactivity on the Android side, the error disappears.
Microsoft.Maui.Essentials.Platform.Init(this, savedInstanceState)
maaa
;

@aokocax
Copy link

aokocax commented Jan 2, 2022

For windows envrionment I was able to run it this way. It works flawlessly with both MAUI and MAUI-Blazor.

using Windows.Storage.Pickers;
using WinRT;
using System.Runtime.InteropServices;
public async void File()
{
using var process = Process.GetCurrentProcess();

  var hwnd = process.MainWindowHandle;

  var filePicker = new FileOpenPicker();

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

  var folder = await filePicker.PickSingleFileAsync();
}
 [ComImport]
    [Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IInitializeWithWindow
    {
        void Initialize(IntPtr hwnd);
    }

Ekran görüntüsü 2022-01-02 234523

@jamesmontemagno
Copy link
Member

jamesmontemagno commented Jan 3, 2022

This should have been fixed with -> #3232

var picker = new FileOpenPicker();

var hwnd = Platform.CurrentWindowHandle;
WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd);

Should work... You need to add the OnActivated lifecycle event though: https://github.com/jamesmontemagno/maui/blob/b8674a83bac2ee394ff984587e4cfde2c6185bf2/src/Essentials/samples/Samples/Startup.cs

I tried your code and got an exception though.. maybe something changed in v1 of the app sdk.

Looking at https://github.com/microsoft/CsWinRT/blob/master/docs/interop.md#windows-sdk this is what we do.... so just not sure if this got merged into P11

@hartez hartez added the fatal label Jan 5, 2022
@dharber
Copy link

dharber commented Jan 7, 2022

I can't tell if Preview 11 got #3232 or not. However, my windows app is throwing a different exception now but that could be other changes in .NET MAUI...who knows?

FilePicker.PickAsync() is now throwing "Source object type is not a projected type and does not inherit from a projected type. (Parameter 'value')"

Is there a way to confirm whether #3232 made it into Preview 11? If not, is there a way to obtain a build of MAUI that does have it? Some kind of nightly build or something?

@aokocax
Copy link

aokocax commented Jan 9, 2022

This should have been fixed with -> #3232

var picker = new FileOpenPicker();

var hwnd = Platform.CurrentWindowHandle;
WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd);

Should work... You need to add the OnActivated lifecycle event though: https://github.com/jamesmontemagno/maui/blob/b8674a83bac2ee394ff984587e4cfde2c6185bf2/src/Essentials/samples/Samples/Startup.cs

I tried your code and got an exception though.. maybe something changed in v1 of the app sdk.

Looking at https://github.com/microsoft/CsWinRT/blob/master/docs/interop.md#windows-sdk this is what we do.... so just not sure if this got merged into P11

I tried but I can't reach the CurrentWindowHandle in my Application. When I look at the source codes, I see that this property is internal. Is there any method to get this property (I added lifecycle event )

@jamesmontemagno
Copy link
Member

Testing with P11 getting, will look as the fix should be in there...:

System.Runtime.InteropServices.COMException: 'Invalid window handle. (0x80070578)
Consider WindowNative, InitializeWithWindow
See https://aka.ms/cswinrt/interop#windows-sdk'

@AdamBraden
Copy link

WinUI3 supports the IWindowNative interface, support is built into the C# WinRT runtime, eg:

var window = new Microsoft.UI.Xaml.Window();
// ...
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.FileTypeFilter.Add("*");
WinRT.Interop.InitializeWithWindow.Initialize(folderPicker, hwnd);
var folder = await folderPicker.PickSingleFolderAsync();

@jamesmontemagno
Copy link
Member

I don't think we want to create a new Window... we would want to use the current Active Window correct....

var hwnd = Platform.CurrentWindowHandle;
WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd);

SetFileTypes(options, picker);

var resultList = new List<StorageFile>();

if (allowMultiple)
{
var fileList = await picker.PickMultipleFilesAsync();
if (fileList != null)
	resultList.AddRange(fileList);
}
else
{
var file = await picker.PickSingleFileAsync();
if (file != null)
	resultList.Add(file);
}

And for the CurrentWindowHandle we do

internal static IntPtr CurrentWindowHandle
			=> WinRT.Interop.WindowNative.GetWindowHandle(CurrentWindow);

This is what I do today...

@Redth thoughts?

@AdamBraden
Copy link

Yes, if the MAUI code is already tracking the current active window, that should work fine.

@jamesmontemagno
Copy link
Member

@Redth just tested on mainline and it was working so doing some debugging around to see if somethign else is going on.

@orryverducci
Copy link
Author

Just tried this again with MAUI Preview 12 / Visual Studio 17.1 Preview 4 and I am now getting a different exception:

System.ArgumentException: 'Source object type is not a projected type and does not inherit from a projected type. (Parameter 'value')'

Unfortunately the call stack doesn't provide anything useful, it only shows the method I called PickMultipleAsync from marked as external code.

@shimingsg
Copy link

Not reproducible with windows 10.0.19041.

@shimingsg shimingsg added the s/verified Verified / Reproducible Issue ready for Engineering Triage label Feb 18, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 20, 2022
@mattleibow
Copy link
Member

This is the issue: microsoft/WindowsAppSDK#1063

@mattleibow mattleibow closed this as not planned Won't fix, can't repro, duplicate, stale Nov 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info platform/windows 🪟 s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests