2

With reference to my previous question

I am exploring WinUI3 using C++ and struggling to find information and material on different community portals.

I developed a demo app which has a window and 2 pages. On one of the pages, I want to open a file picker.

Tab1Page.xaml.cpp

void winrt::App1::implementation::Tab1Page::Button_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e)
{
    OutputTextBlock().Text(OutputTextBlock().Text() + L"Button Clicked\n");

    auto hwnd = GetProcessFirstWindowHandle();

    auto picker = winrt::Windows::Storage::Pickers::FileOpenPicker();
    //Initialize the folder picker with the window handle(HWND).
    auto initializeWithWindow { picker.as<::IInitializeWithWindow>()
    };
    initializeWithWindow->Initialize(hwnd);
    picker.SuggestedStartLocation(winrt::Windows::Storage::Pickers::PickerLocationId::Desktop);
    winrt::Windows::Storage::StorageFile file = picker.PickSingleFileAsync().get();
}

Error

winrt::Windows::Storage::StorageFile file = picker.PickSingleFileAsync().get();`

Exception thrown at 0x00007FF9A92706BC in App1.exe: Microsoft C++ exception: winrt::hresult_error at memory location 0x0000007EA60F9B88.
Borisonekenobi
  • 469
  • 4
  • 15
Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34
  • The exception will have an HRESULT inside it. What is the HRESULT? – Raymond Chen Feb 14 '23 at 05:18
  • This is probably an emergency shutdown, triggered by synchronously waiting on the UI thread (the `.get()`-call). – IInspectable Feb 14 '23 at 05:27
  • @IInspectable I used `winrt::fire_and_forget` and `co_wait` as well but same exception. – Gaurang Dave Feb 14 '23 at 06:02
  • Could you please tell us what is the error's HRESULT value? Did you see an error in the output, or did you receive an error in your code? – Jeaninez - MSFT Feb 14 '23 at 07:48
  • @Jeaninez-MSFT I think that there is something wrong with file filter values. This is what I got on output windows - 'The FileType Filter property must have at least one file type filter specified.'. But I still doubt that its related to either UI thread or window handler. – Gaurang Dave Feb 14 '23 at 08:00
  • @RaymondChen Please check this - Exception thrown at 0x00007FF9A92706BC (KernelBase.dll) in App1.exe: WinRT originate error - 0x80004005 : 'The FileTypeFilters property must have at least one file type filter specified.'. onecoreuap\shell\twinui\pickercore\api\lib\winrtpicker.cpp(943)\twinui.appcore.dll!00007FF9813081F6: (caller: 00007FF9AB5D5583) ReturnHr(1) tid(37d0) 80004005 Unspecified error CallContext:[\PickSingleFileAsync] Exception thrown at 0x00007FF9A92706BC in App1.exe: Microsoft C++ exception: winrt::hresult_error at memory location 0x0000004B58129808. – Gaurang Dave Feb 14 '23 at 08:24
  • @Jeaninez-MSFT Please check this - Exception thrown at 0x00007FF9A92706BC (KernelBase.dll) in App1.exe: WinRT originate error - 0x80004005 : 'The FileTypeFilters property must have at least one file type filter specified.'. onecoreuap\shell\twinui\pickercore\api\lib\winrtpicker.cpp(943)\twinui.appcore.dll!00007FF9813081F6: (caller: 00007FF9AB5D5583) ReturnHr(1) tid(37d0) 80004005 Unspecified error CallContext:[\PickSingleFileAsync] Exception thrown at 0x00007FF9A92706BC in App1.exe: Microsoft C++ exception: winrt::hresult_error at memory location 0x0000004B58129808. – Gaurang Dave Feb 14 '23 at 08:25

1 Answers1

1

You have two problems

so this should work:

Windows::Foundation::IAsyncAction winrt::App1::implementation::Tab1Page::Button_Click(IInspectable const& sender, RoutedEventArgs const& args)
{
  auto hwnd = GetFirstProcessWindowHandle();

  auto picker = winrt::Windows::Storage::Pickers::FileOpenPicker();
  picker.FileTypeFilter().Append(L"*");

  auto initializeWithWindow{ picker.as<IInitializeWithWindow>() };
  initializeWithWindow->Initialize(hwnd);
  picker.SuggestedStartLocation(winrt::Windows::Storage::Pickers::PickerLocationId::Desktop);
  auto file = co_await picker.PickSingleFileAsync();
}
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • 1
    Thanks Simon. I also resolved this. You posted the answer while I was editing my answer :D Thanks a lot. I made mistake by not looking into the details on output window. – Gaurang Dave Feb 14 '23 at 08:46
  • The coroutine footgun: A coroutine must do one of two things: `1` Take arguments by value, or `2` leave the `const&`, but remove the names so the arguments aren't being used. With coroutines, references become invalid at the first suspension point. – IInspectable Feb 14 '23 at 08:52