gpt4 book ai didi

c++ - co_await 表达式需要 await_ready 函数

转载 作者:太空狗 更新时间:2023-10-29 20:50:51 26 4
gpt4 key购买 nike

我有一个 Win32 程序,我想在其中添加一些 winRT 调用。除其他事项外,我想打开一个没有 GUI 界面的文件。

我使用 StorageFile 类的异步文件打开调用,因为下一个调用需要一个 IStorageFile 接口(interface)。

#include <roapi.h>
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Foundation.h>

void openFile()
{
using namespace winrt;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Storage;

HRESULT rtn = RoInitialize(RO_INIT_MULTITHREADED);
winrt::hstring path{ L"C:\\Users...\\mytextfile.txt"};

//wait for open the file
auto file = co_await StorageFile::GetFileFromPathAsync(path);

//IStorageFile interface needed
}

int main()
{
openFile();
return 0;
}

此刻,编译器提示 co_await 表达式需要一个合适的“await_ready”函数,但没有找到。

我不确定这种情况是由于缺少 header 包含还是“co_await”不能在 win32 应用程序中使用。

编辑:我的 Visual Studio 项目设置是:- 使用 c++17,将 cppwinrt.exe 添加到我的包含目录,链接到 windowsapp.lib 并使用 windows sdk 版本 10.0.17134.0。

最佳答案

问题是 openFile() 函数没有正确的返回类型来处理 co_await

查看我为 C++11 threads to update MFC application windows. SendMessage(), PostMessage() required? 创建的答案的研究和工作其中包含针对协程的各种方法的建议列表。

这个问题是关于将 C++/WinRT 与 MFC 结合使用,但该 Material 也适用于 WinAPI。

另见synchronizing SDK with Windows 10 update and using WinRT with Standard C++其中包含一个简单的控制台应用程序示例,该示例使用 Web Syndication 异步功能从 RSS 提要中检索 URL 列表。有许多指向文档的链接,其中一些现在有点过时了。

附录:示例控制台应用程序

我使用 Visual Studio 2017 创建了以下简单的控制台应用程序。我创建了文本文件,然后在调试器中运行它。然后我重命名了文本文件并在调试器中再次运行它并抛出异常,因为具有该名称的文件不再存在。

另见 C++/WinRT, part of Windows SDK 17134 is not compatible with Visual Studio 15.8 Preview 3它描述了您可能需要更改的编译器选项。我做到了。

// console_winrt.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
// Requires the following changes to the project properties in the C++ compiler section.
// - C++ language standard must be set to C++17
// - Add /await to the Additional options

#include "pch.h"

#pragma comment(lib, "windowsapp")

#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Foundation.h>

#include <iostream>

winrt::Windows::Foundation::IAsyncAction openMyFile()
{

winrt::hstring path{ L"D:\\Users\\rickc\\mytextfile.txt" };

//wait for open the file
auto file = co_await winrt::Windows::Storage::StorageFile::GetFileFromPathAsync(path);

//IStorageFile interface needed
auto xDate = file.DateCreated();
std::cout << "file was found " << std::endl;
}

int main()
{
// initialize the WinRT apartment.
winrt::init_apartment();

auto x = openMyFile();

// wait on the file access since that is all we are doing and we need to give it time.
x.get();
return 0;
}

我使用了以下属性设置。

Properties dialog screen shot showing General Properties

Properties dialog screen shot showing C/C++ All options

关于c++ - co_await 表达式需要 await_ready 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52840223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com