在没有 UWP 的情况下从 C++/WinRT 使用 FileOpenPicker 时出现视觉"Invalid window handle"错误
visual "Invalid window handle" error when using FileOpenPicker from C++/WinRT without UWP
我正在尝试使用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);
相关文章:
- 如何解决"invalid conversion from 'char' to 'const char*'"
- 视觉studo 2019中的漫画和静态/动态绑定
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 如何改变c++应用程序的视觉效果
- 视觉工作室项目.提取源文件夹名称
- C++ "error: invalid use of void expression"
- 为什么我在尝试模板时遇到视觉工作室C++错误
- 视觉工作室 2017;启用 /permissive 时,类型 "const wchar_t *" 的参数与类型 "PWSTR" 的参数不兼容
- 收到错误"invalid use of non-static data member 'stu::n' "
- 视觉我希望一个函数在另一个函数C++中进行计算
- 视觉 如何循环访问C++中遵循类似格式的多个.txt文件?
- Poloniex API "Invalid command" c++ libcurl
- remove(str.begin(), str.end(), );无法正常工作(我正在使用视觉工作室 2012)
- C++模板错误:"invalid explicitly-specified argument for template parameter"
- 背包问题,视觉工作室问题
- 如何在视觉c ++项目中添加箭头键作为修饰键Microsoft
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- SDL_CreateTextureFromSurface() "Invalid texture"错误?
- C++ "Invalid use of 'this' in non-member function" ,
- 在没有 UWP 的情况下从 C++/WinRT 使用 FileOpenPicker 时出现视觉"Invalid window handle"错误