我想从我使用 Qt 5.9.2 MinGW 32 位创建的应用程序中使用通过 WinRT API 可用的库(如这些)。我有 Windows 10 操作系统。
现在,我已经设置了如下所示的套件。
然后,在.pro文件中,我添加了:
CONFIG += c++1z
SOURCES += main.cpp
 ### Windows 10 SDK
 win32:CONFIG(release, debug|release): LIBS += -L$$PWD/'../../../../Program Files (x86)/Windows Kits/10/Lib/10.0.16299.0/um/x86/' -lWindowsApp
 else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/'../../../../Program Files (x86)/Windows Kits/10/Lib/10.0.16299.0/um/x86/' -lWindowsApp
 INCLUDEPATH += $$PWD/'../../../../Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/shared'
 DEPENDPATH += $$PWD/'../../../../Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/shared'
 INCLUDEPATH += $$PWD/'../../../../Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/um'
 DEPENDPATH += $$PWD/'../../../../Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/um'
 INCLUDEPATH += $$PWD/'../../../../Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/ucrt'
 DEPENDPATH += $$PWD/'../../../../Program Files …在 Win32 API 中注册窗口类、创建窗口然后通过消息泵循环保持活动状态的 C++ WinRT 等价物是什么?
我目前正在查看和阅读 WinRT 的文档,因为我想学习如何以现代 C++ 方式完成我过去在 Win32 中所做的所有事情。
到目前为止,我的经历很糟糕,我要直截了当地承认我没有得到它。
我试过了,但由于我没有在容器中运行,似乎尚未创建线程的 CoreWindow。
    int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
    {
        winrt::init_apartment(winrt::apartment_type::single_threaded);
        winrt::Windows::UI::Core::CoreWindow window = winrt::Windows::UI::Core::CoreWindow::GetForCurrentThread();
        window.Activate();
        auto dispatcher = window.Dispatcher();
        using DispatcherOptions = winrt::Windows::UI::Core::CoreProcessEventsOption;
        const DispatcherOptions options = DispatcherOptions::ProcessUntilQuit;
        dispatcher.ProcessEvents(options);
    }
我想TempState使用下面的代码来汇总应用程序文件夹中的所有文件大小,该代码工作正常。
const auto tempFolder { ApplicationData::Current().TemporaryFolder() };
int64_t size { 0 };
const QueryOptions options { CommonFolderQuery::DefaultQuery };
options.FolderDepth(FolderDepth::Shallow);
options.IndexerOption(IndexerOption::UseIndexerWhenAvailable);
options.SetPropertyPrefetch(PropertyPrefetchOptions::BasicProperties, {});
try
{
    for (auto item : co_await tempFolder.CreateFileQueryWithOptions(options).GetFilesAsync())
    {
        size += (co_await item.GetBasicPropertiesAsync()).Size();
    }
    // do something with the size
}
catch (winrt::hresult_error& err)
{
    // ...
}
目前,我的文件夹中只有不到 3000 个项目TempState,上面的代码需要 20 秒才能计算文件大小的总和。需要有一种方法来加快速度,对吗?
到目前为止我尝试过的是使用
options.IndexerOption(IndexerOption::OnlyUseIndexerAndOptimizeForIndexedProperties);
但随后GetFilesAsync根本不返回任何文件。
可以采取什么措施来(大幅)提高性能?
我在 Windows 10 Home 版本 10.0.16299 32 位、Windows SDK 版本 10.0.17134.12 和 C++/WinRT 版本 1.0.180505.2 上使用 Visual Studio Community 2017 版本 15.7.1。当我尝试使用任何 C++/WinRT 模板创建新项目时,我收到以下错误消息:
Error: this template attempted to load component assembly 
'Microsoft.VisualStudio.Universal.TemplateWizards, Version=15.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. For more 
information on this problem and how to enable this template, 
please see documentation on Customizing Project Templates.
这个问题有简单的解决方法吗?
在我的 C++/WinRT 项目中,我试图在 UI 线程上运行一些代码,但收到一条错误消息:
“winrt::impl::consume_Windows_UI_Core_ICoreDispatcher<winrt::Windows::UI::Core::ICoreDispatcher>::RunAsync':在定义之前不能使用返回'auto'的函数”
我正在调用这样的方法:
Dispatcher().RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [=]()
{
     // Code to be executed.
});
该实现来自自动生成的 winrt 文件,该文件auto作为返回类型返回。
template <typename D>
struct consume_Windows_UI_Core_ICoreDispatcher
{
    [[nodiscard]] auto HasThreadAccess() const;
    auto ProcessEvents(Windows::UI::Core::CoreProcessEventsOption const& options) const;
    auto RunAsync(Windows::UI::Core::CoreDispatcherPriority const& priority, Windows::UI::Core::DispatchedHandler const& agileCallback) const;
    auto RunIdleAsync(Windows::UI::Core::IdleDispatchedHandler const& agileCallback) const;
};
有什么我遗漏的还是这是一个错误?
当我使用microsoft docs 中的帮助程序函数从 cx 迁移到 winrt时,会发生此错误。我在这里看到类似的问题,但提到的解决方案似乎不适合我。此处提到的解决方案在存在此错误的文件中的任何其他 winrt 标头之前添加 #include <Unknwn.h>。
template <typename T>
T from_cx(Platform::Object ^ from) {
T to{nullptr};
winrt::check_hresult(reinterpret_cast<::IUnknown*>(from)->QueryInterface(
    winrt::guid_of<T>(), reinterpret_cast<void**>(winrt::put_abi(to))));
return to;
}
这是整个文件:
#pragma once
#include <Unknwn.h>
#include <winrt/Windows.Foundation.h>
namespace x {
namespace y {
template <typename T>
T from_cx(Platform::Object ^ from) {
    T to{nullptr};
    winrt::check_hresult(reinterpret_cast<::IUnknown*>(from)->QueryInterface(
        winrt::guid_of<T>(), reinterpret_cast<void**>(winrt::put_abi(to))));
    return to;
}
template <typename T>
    T ^
    to_cx(winrt::Windows::Foundation::IUnknown const& from) {
        return safe_cast<T ^>(reinterpret_cast<Platform::Object ^>(winrt::get_abi(from)));
    }
}
}
我正在尝试实现该ICustomPropertyProvider::Type()方法,但是我找不到获取TypeNameC++/WinRT 类型的方法。显然你在 C++/CX 中有Object::GetTypeand T::typeid,但在 C++/WinRT 中没有。
我尝试了以下代码,但这只是一个疯狂的猜测,因为它是我能找到的唯一与类型远程相关的东西。不过,我认为typeid()与 XAML 无关,因为它提供的唯一保证是它返回的字符串是唯一标识的。
using namespace Windows::Devices::Enumeration;
using namespace Windows::UI::Xaml::Interop;
TypeName name;
name.Name = typeid(DeviceInformation).name();
name.Kind = TypeKind::Metadata;
我想使用通过Qt 5.9.2为UWP 64bit(MSVC 2017)创建的UWP应用程序中通过WinRT API提供的库(类似这些)。我在计算机上安装了Visual Studio Build Tool 2017 v15.5.7。Windows 10是我的操作系统。
现在,我已经建立了如下所示的工具包。
然后,在.pro文件中,我添加了:
SOURCES += main.cpp
INCLUDEPATH += $$PWD/cppwinrt/10.0.16299.0/
DEPENDPATH += $$PWD/cppwinrt/10.0.16299.0/
我cppwinrt来自GitHub rep。
我只是添加了一些行main.cpp来测试正确的编译:
#include <QCoreApplication>
#include <winrt/Windows.Devices.WiFi.h>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    WiFiAdapter wiFiAdapter;
    return a.exec();
}
运行qmake和之后build,出现此错误:
C:\ Program Files(x86)\ Microsoft Visual Studio \ 2017 \ BuildTools \ VC \ Tools \ MSVC \ 14.12.25827 \ include \ optional(15):致命错误C1189:#错误:可选的类模板仅在C …
背景:我需要构建一个 Windows 运行时组件,作为设置为使用CMake生成其构建系统的系统的一部分。作为准备步骤,我试图在命令行上构建它。
从一个简单的 .idl 文件 (MyType.idl) 开始
namespace NS
{
    [default_interface]
    runtimeclass MyType
    {
    }
}
我正在尝试使用midlrt.exe工具生成匹配的 .winmd 文件。下面的命令行(为了可读性分成几行)
midlrt
    /metadata_dir "%WindowsSdkDir%References\%WindowsSDKVersion%Windows.Foundation.FoundationContract\3.0.0.0"
    /reference "%WindowsSdkDir%References\%WindowsSDKVersion%Windows.Foundation.FoundationContract\3.0.0.0\Windows.Foundation.FoundationContract.winmd"
    /winmd MyType.winmd
    /notlb
    /winrt
    /nomidl
    /nologo
    /enum_class
    /ns_prefix
    /client none
    /server none
    MyType.idl
生成 MyType.winmd 文件就好了,但我不知道为什么。我对/metadata_dir和/reference选项特别困惑。跑步midlrt /help提供以下功能:
/metadata_dir      Specify one or more directories containing platform metadata files
/reference         Specify one or more WinMD files to import
/metadata_dir上的官方文档对此没有增加太多(除了令人困惑的评论:“使用此开关指定 Windows 的主元数据文件的位置,该文件名为 windows.winmd。”)。没有关于/reference …
概括地说,我想要完成的是捕获(部分)屏幕并将捕获的内容转换为数字图像格式。以下步骤概述了我认为的解决方案:
\nDirect3D11CaptureFramePool并订阅其FrameArrived事件FrameArrived访问事件委托中的像素数据我的问题在于步骤 2:虽然我可以获得捕获的帧,但获得对表面的 CPU 读取访问权限失败。这是我的FrameArrived事件委托实现(完整重现如下):
void on_frame_arrived(Direct3D11CaptureFramePool const& frame_pool, winrt::Windows::Foundation::IInspectable const&)\n{\n    if (auto const frame = frame_pool.TryGetNextFrame())\n    {\n        if (auto const surface = frame.Surface())\n        {\n            if (auto const interop = surface.as<::Windows::Graphics::DirectX::Direct3D11::IDirect3DDxgiInterfaceAccess>())\n            {\n                com_ptr<IDXGISurface> dxgi_surface { nullptr };\n                check_hresult(interop->GetInterface(IID_PPV_ARGS(&dxgi_surface)));\n\n                DXGI_MAPPED_RECT info = {};\n                // Fails with `E_INVALIDARG`\n                check_hresult(dxgi_surface->Map(&info, DXGI_MAP_READ));\n            }\n        }\n    }\n}\nc++-winrt ×10
c++ ×5
uwp ×3
qt ×2
c++-cx ×1
dxgi ×1
midl ×1
mingw ×1
visual-c++ ×1
winapi ×1
windows-10 ×1
winrt-xaml ×1