在没有 UWP 的情况下从 C++/WinRT 使用 FileOpenPicker 时出现视觉"Invalid window handle"错误

visual "Invalid window handle" error when using FileOpenPicker from C++/WinRT without UWP

本文关键字:视觉 Invalid 错误 handle window FileOpenPicker 使用 UWP 情况下 WinRT C++      更新时间:2023-10-16

我正在尝试使用C++/WinRT来编写一些有趣的东西。我在Windows编程方面几乎没有经验,在C++/CX方面也没有经验,所以我开始尝试示例程序(OCR)。

示例程序是关于光学字符识别的,我将其修改为人脸检测器(基于控制台)。它运行得很好。

我想将从命令行获取文件转换为文件对话框,所以我从C#移植了以下片段:

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();

没有编译错误,但当我运行程序时,我会收到以下错误消息:

hresult_error: (0x80070578) Invalid window handle.

我认为出现错误是因为它是一个基于控制台的程序(wmain),而不是wWinMain。

我试图在网上找到一个解决方案,但它们都是关于UWP的。请提供一个不涉及UWP的解决方案,并且必须能够直接从cl.exe编译。

注意

根据可从经典桌面应用程序调用的UWP API,如果API具有DualApiPartitionAttribute属性,则它将在经典桌面应用中工作,否则它将不会工作。例如,Geolocator具有此属性,因此它可以工作。

然而,即使FaceDetector没有这个属性,它仍然可以在我的玩具程序中验证。

Windows::UI中的任何内容都肯定需要UWP(尽管https://aka.ms/windowsui/inwin32)。FileOpenPicker没有此属性,但它不在Windows::UI下,因此它可能是一种变通方法。

完整代码:

#pragma comment(lib, "windowsapp")
#include <winrt/Windows.Storage.Pickers.h>
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.Graphics.Imaging.h>
#include <winrt/Windows.Media.FaceAnalysis.h>
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<int> 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<DetectedFace> result = co_await detector.DetectFacesAsync(converted);
printf("Detection donen");
for (auto& face : result) {
BitmapBounds box = face.FaceBox();
printf("[%u %u %u %u]n", box.X, box.Y, box.Width, box.Height);
}
printf("Printing donen");
return 0;
}
int wmain() {
init_apartment();
try {
int res = AsyncSample().get();
printf("%dn", res);
} catch (hresult_error& e) {
printf("hresult_error: (0x%8X) %lsn", e.code(), e.message().c_str());
}
return 0;
}

所以这是需要的修改:

IAsyncOperation<int> AsyncSample() {
HWND hwnd = GetConsoleWindow(); // #include <windows.h>
FileOpenPicker picker = FileOpenPicker();
picker.as<IInitializeWithWindow>()->Initialize(hwnd);