title: "Invalid window handle" error when using FileOpenPicker from C++/WinRT without UWP
body: <p>I am trying to use <a href="https://github.com/Microsoft/cppwinrt" rel="nofollow noreferrer">C++/WinRT</a> to write something interesting. Having very little experience in Windows programming and no experience in C++/CX, I started out by trying <a href="https://github.com/Microsoft/cppwinrt/tree/master/10.0.16299.0/Samples/Ocr" rel="nofollow noreferrer">sample program (OCR)</a>.</p>
  
  <p>The sample program is about optical character recognition, I modified it to be face detector (console-based). It worked very well.</p>
  
  <p>I want to convert getting file from command-line to file dialog, so I ported the following snippet <a href="https://docs.microsoft.com/en-us/uwp/api/Windows.Storage.Pickers.FileOpenPicker" rel="nofollow noreferrer">from C#</a>:</p>
  
  <pre><code>  FileOpenPicker picker = FileOpenPicker();
    picker.ViewMode(PickerViewMode::Thumbnail);
    picker.SuggestedStartLocation(PickerLocationId::PicturesLibrary);
    picker.FileTypeFilter().Append(L".jpg");
    picker.FileTypeFilter().Append(L".jpeg");
    picker.FileTypeFilter().Append(L".png");
  
    StorageFile file = co_await picker.PickSingleFileAsync();
  </code></pre>
  
  <p>No compile error, but when I run the program, I get this error message:</p>
  
  <pre><code>hresult_error: (0x80070578) Invalid window handle.
  </code></pre>
  
  <p>I think the error occurs because it is a console-based program (wmain), not wWinMain.</p>
  
  <p>I tried to find a solution online, but they are all about UWP. Please provide a solution that does not involve UWP and must be able to compile from <code>cl.exe</code> directly.</p>
  
  <p><strong>Note</strong>:</p>
  
  <p>According to <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/mt695951(v=vs.85).aspx" rel="nofollow noreferrer">UWP APIs callable from a classic desktop app</a>, if an API has <a href="https://docs.microsoft.com/en-us/uwp/api/Windows.Foundation.Metadata.DualApiPartitionAttribute" rel="nofollow noreferrer">DualApiPartitionAttribute</a> attribute, it will work in classic desktop app, otherwise it won't. For example, <a href="https://docs.microsoft.com/en-us/uwp/api/Windows.Devices.Geolocation.Geolocator" rel="nofollow noreferrer">Geolocator</a> has this attribute so it will work.</p>
  
  <p>However, even though <a href="https://docs.microsoft.com/en-us/uwp/api/windows.media.faceanalysis.facedetector" rel="nofollow noreferrer">FaceDetector</a> does <em>not</em> have this attribute it still works proven in my toy program.</p>
  
  <p>Anything from <code>Windows::UI</code> definitely requires UWP (although there is <a href="https://aka.ms/windowsui/inwin32" rel="nofollow noreferrer">https://aka.ms/windowsui/inwin32</a>). FileOpenPicker does not have this attribute but it is not under <code>Windows::UI</code>, so there might be a chance it can be workaround.</p>
  
  <p>Complete code:</p>
  
  <pre><code>#pragma comment(lib, "windowsapp")
  
  #include &lt;winrt/Windows.Storage.Pickers.h&gt;
  #include &lt;winrt/Windows.Storage.Streams.h&gt;
  #include &lt;winrt/Windows.Graphics.Imaging.h&gt;
  #include &lt;winrt/Windows.Media.FaceAnalysis.h&gt;
  
  using namespace winrt;
  using namespace std::chrono;
  
  using namespace Windows::Foundation;
  using namespace Windows::Storage;
  using namespace Windows::Storage::Pickers;
  using namespace Windows::Storage::Streams;
  using namespace Windows::Graphics::Imaging;
  using namespace Windows::Media::FaceAnalysis;
  
  using Windows::Foundation::Collections::IVector;
  
  IAsyncOperation&lt;int&gt; AsyncSample() {
    FileOpenPicker picker = FileOpenPicker();
    picker.ViewMode(PickerViewMode::Thumbnail);
    picker.SuggestedStartLocation(PickerLocationId::PicturesLibrary);
    picker.FileTypeFilter().Append(L".jpg");
    picker.FileTypeFilter().Append(L".jpeg");
    picker.FileTypeFilter().Append(L".png");
  
    StorageFile file = co_await picker.PickSingleFileAsync();
    //StorageFile file = co_await StorageFile::GetFileFromPathAsync(
    //    L"C:\\Users\\user\\Pictures\\20170318_202325.jpg");
    IRandomAccessStream stream = co_await file.OpenAsync(FileAccessMode::Read);
  
    BitmapDecoder decoder = co_await BitmapDecoder::CreateAsync(stream);
    SoftwareBitmap bitmap = co_await decoder.GetSoftwareBitmapAsync();
  
    FaceDetector detector = co_await FaceDetector::CreateAsync();
  
    SoftwareBitmap converted =
        SoftwareBitmap::Convert(bitmap, BitmapPixelFormat::Nv12);
  
    IVector&lt;DetectedFace&gt; result = co_await detector.DetectFacesAsync(converted);
    printf("Detection done\n");
    for (auto&amp; face : result) {
      BitmapBounds box = face.FaceBox();
      printf("[%u %u %u %u]\n", box.X, box.Y, box.Width, box.Height);
    }
    printf("Printing done\n");
  
    return 0;
  }
  
  int wmain() {
    init_apartment();
  
    try {
      int res = AsyncSample().get();
      printf("%d\n", res);
    } catch (hresult_error&amp; e) {
      printf("hresult_error: (0x%8X) %ls\n", e.code(), e.message().c_str());
    }
  
    return 0;
  }
  </code></pre>