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

System.Runtime.InteropServices.COMException: 'Invalid window handle. (0x80070578) #1416

Open
yegorisB opened this issue Dec 13, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@yegorisB
Copy link

Which version of the app?

WinUI 3 Gallery

Description

Source: Windows.Storage.StorageFile file = await open.PickSingleFileAsync();

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

Screenshots

image

Windows version

Windows 11 22H2 (22621)

Additional context

Edition Windows 11 Home
Version 23H2
Installed on ‎9/‎11/‎2023
OS build 22631.2861
Experience Windows Feature Experience Pack 1000.22681.1000.0

Processor 13th Gen Intel(R) Core(TM) i9-13900H 2.60 GHz
Installed RAM 16.0 GB (15.6 GB usable)

System type 64-bit operating system, x64-based processor
Pen and touch No pen or touch input is available for this display

@yegorisB yegorisB added the bug Something isn't working label Dec 13, 2023
@yegorisB
Copy link
Author

   Everything is all right If you use this:
   
           private async void OpenButton_Click(object sender, RoutedEventArgs e)
    {

        //https://github.com/microsoft/WindowsAppSDK/issues/1188
        //More info on obtaining the HWND
        var filePicker = new FileOpenPicker();

        // Get the current window's HWND by passing in the Window object
        var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);

        // Associate the HWND with the file picker
        WinRT.Interop.InitializeWithWindow.Initialize(filePicker, hwnd);

        // Use file picker like normal!
        filePicker.FileTypeFilter.Add("*");
        var file = await filePicker.PickSingleFileAsync();

        if (file != null)
        {
            using (Windows.Storage.Streams.IRandomAccessStream randAccStream =
                await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                // Load the file into the Document property of the RichEditBox.
                editor.Document.LoadFromStream(Microsoft.UI.Text.TextSetOptions.FormatRtf, randAccStream);
            }
        }
    }

instead of this:

    private async void OpenButton_Click(object sender, RoutedEventArgs e)
    {

        //https://github.com/microsoft/WindowsAppSDK/issues/1188
        //More info on obtaining the HWND
        var filePicker = new FileOpenPicker();

        // Get the current window's HWND by passing in the Window object
        var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);

        // Associate the HWND with the file picker
        WinRT.Interop.InitializeWithWindow.Initialize(filePicker, hwnd);

        // Use file picker like normal!
        filePicker.FileTypeFilter.Add("*");
        var file = await filePicker.PickSingleFileAsync();

        if (file != null)
        {
            using (Windows.Storage.Streams.IRandomAccessStream randAccStream =
                await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                // Load the file into the Document property of the RichEditBox.
                editor.Document.LoadFromStream(Microsoft.UI.Text.TextSetOptions.FormatRtf, randAccStream);
            }
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant