标签: c++-winrt

使用 MinGW 构建 Qt 项目以使用 WinRT API

我想从我使用 Qt 5.9.2 MinGW 32 位创建的应用程序中使用通过 WinRT API 可用的库(如这些)。我有 Windows 10 操作系统。

现在,我已经设置了如下所示的套件。

MinGW 套件设置

然后,在.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 …
Run Code Online (Sandbox Code Playgroud)

c++ qt mingw c++-winrt

5
推荐指数
1
解决办法
1033
查看次数

如何使用 WinRT 创建经典的 win32 应用程序窗口?

在 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);
    }
Run Code Online (Sandbox Code Playgroud)

c++ windows-runtime c++-winrt

5
推荐指数
1
解决办法
2935
查看次数

在 UWP 中检索文件夹中文件大小的快速方法

我想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)
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

目前,我的文件夹中只有不到 3000 个项目TempState,上面的代码需要 20 秒才能计算文件大小的总和。需要有一种方法来加快速度,对吗?


到目前为止我尝试过的是使用

options.IndexerOption(IndexerOption::OnlyUseIndexerAndOptimizeForIndexedProperties);
Run Code Online (Sandbox Code Playgroud)

但随后GetFilesAsync根本不返回任何文件。


可以采取什么措施来(大幅)提高性能?

uwp c++-winrt

5
推荐指数
1
解决办法
228
查看次数

错误:此模板尝试加载组件程序集“Microsoft.VisualStudio.Universal.TemplateWizards,版本=15.0.0.0

我在 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.

这个问题有简单的解决方法吗?

visual-studio visual-c++ c++-winrt

4
推荐指数
1
解决办法
3232
查看次数

在 C++/WinRT 项目中使用 CoreDispatcher::RunAsync 时出现“在定义之前不能使用返回‘auto’的函数”

在我的 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.
});
Run Code Online (Sandbox Code Playgroud)

该实现来自自动生成的 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;
};
Run Code Online (Sandbox Code Playgroud)

有什么我遗漏的还是这是一个错误?

uwp c++-winrt

4
推荐指数
1
解决办法
1015
查看次数

错误 C2664 'HRESULT IUnknown::QueryInterface(const IID &amp;,void **)':无法将参数 1 从 'const winrt::guid' 转换为 'const IID &amp;'

当我使用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;
}
Run Code Online (Sandbox Code Playgroud)

这是整个文件:

#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)));
    }
}
}
Run Code Online (Sandbox Code Playgroud)

c++ windows-runtime c++-cx winrt-xaml c++-winrt

4
推荐指数
1
解决办法
943
查看次数

如何在 C++/WinRT 中获取基础类型的 TypeName?

我正在尝试实现该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;
Run Code Online (Sandbox Code Playgroud)

c++ c++-winrt

3
推荐指数
1
解决办法
1000
查看次数

使用VS C ++ Compiler 15.0和C ++ 17构建Qt项目以使用WinRT API

我想使用通过Qt 5.9.2为UWP 64bit(MSVC 2017)创建的UWP应用程序中通过WinRT API提供的库(类似这些)。我在计算机上安装了Visual Studio Build Tool 2017 v15.5.7。Windows 10是我的操作系统。

现在,我已经建立了如下所示的工具包。

UWP套件设置

然后,在.pro文件中,我添加了:

SOURCES += main.cpp

INCLUDEPATH += $$PWD/cppwinrt/10.0.16299.0/
DEPENDPATH += $$PWD/cppwinrt/10.0.16299.0/
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

运行qmake和之后build,出现此错误:

C:\ Program Files(x86)\ Microsoft Visual Studio \ 2017 \ BuildTools \ VC \ Tools \ MSVC \ 14.12.25827 \ include \ optional(15):致命错误C1189:#错误:可选的类模板仅在C …

c++ qt windows-10 uwp c++-winrt

3
推荐指数
1
解决办法
1265
查看次数

如何使用 midlrt.exe 将 .idl 编译为 .winmd?

背景:我需要构建一个 Windows 运行时组件,作为设置为使用CMake生成其构建系统的系统的一部分。作为准备步骤,我试图在命令行上构建它。


从一个简单的 .idl 文件 (MyType.idl) 开始

namespace NS
{
    [default_interface]
    runtimeclass MyType
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用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
Run Code Online (Sandbox Code Playgroud)

生成 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
Run Code Online (Sandbox Code Playgroud)

/metadata_dir上的官方文档对此没有增加太多(除了令人困惑的评论:“使用此开关指定 Windows 的主元数据文件的位置,该文​​件名为 windows.winmd。”)。没有关于/reference …

midl windows-runtime winrt-component c++-winrt

3
推荐指数
1
解决办法
627
查看次数

如何从仅使用 GPU 访问创建的 IDXGISurface 中获取像素数据?

概括地说,我想要完成的是捕获(部分)屏幕并将捕获的内容转换为数字图像格式。以下步骤概述了我认为的解决方案:

\n
    \n
  1. 设置Direct3D11CaptureFramePool并订阅其FrameArrived事件
  2. \n
  3. FrameArrived访问事件委托中的像素数据
  4. \n
  5. 将图像数据传递到Windows Imaging Component进行编码
  6. \n
\n

我的问题在于步骤 2:虽然我可以获得捕获的帧,但获得对表面的 CPU 读取访问权限失败。这是我的FrameArrived事件委托实现(完整重现如下):

\n
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}\n
Run Code Online (Sandbox Code Playgroud)\n

调用Map()失败并显示E_INVALIDARG调试层提供了额外的、有用的错误诊断: …

winapi screen-capture dxgi c++-winrt

3
推荐指数
1
解决办法
462
查看次数