当前位置:   article > 正文

2402C++,现代窗口接口

2402C++,现代窗口接口
#include <string>
#include <iostream>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Web.Http.h>
#include <winrt/Windows.Storage.h>
#include <winrt/Windows.Storage.Streams.h>

using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Web::Http;
using namespace Windows::Storage;
using namespace Windows::Storage::Streams;

IAsyncAction DownloadToFile(std::wstring &url, std::wstring &dirPath) {
    HttpClient client;
    auto response = co_await client.GetAsync(Uri(url));
    response.EnsureSuccessStatusCode();
    auto buffer = co_await response.Content().ReadAsBufferAsync();
    auto localFolder = co_await StorageFolder::GetFolderFromPathAsync(dirPath);
    auto file = co_await localFolder.CreateFileAsync(L"downloaded.html", CreationCollisionOption::ReplaceExisting);
    co_await FileIO::WriteBufferAsync(file, buffer);
    co_return;
}

IAsyncOperation<int> Main() {
    std::wstring url = __argc > 1 ? __wargv[1] : L"https://aka.ms/cppwinrt";
    std::wstring dir_path = __argc > 2 ? __wargv[2] : L"C:\\Temp";
    try {
        co_await DownloadToFile(url, dir_path);
        std::wcout << "Done." << std::endl;
        co_return 0;
    } catch (hresult_error const &ex) {
        std::wcout << "Error: " << std::wstring_view(ex.message()) << std::endl;
        co_return 1;
    }
}

int main() {
    init_apartment();
    return Main().get();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/111231
推荐阅读
相关标签
  

闽ICP备14008679号