Try to add this directive to pch.h: #include "winrt/Windows.Storage.Streams.h"
.
And use this line:
StorageFile file = co_await StorageFile::GetFileFromPathAsync( LR"(d:\temp\Function.png)" );
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
My last knowledge of C++ is from 1997. I am now trying to understand 'modern' C++. I have created a new C++/WinRT project in VS 2022 and inserted the following code (If you see SQL or Yaml code, I didn't mark code as SQL or YAML):
pch.h:
#pragma once #include <winrt/Windows.Foundation.h> #include <winrt/Windows.Foundation.Collections.h> #include <winrt/Windows.System.h>
main.cpp: `#include "pch.h"
using namespace winrt; using namespace Windows::Foundation; using namespace winrt::Windows::Storage; using namespace winrt::Windows::System;
IAsyncAction OpenPicture();
int main() { init_apartment();
auto task { OpenPicture() };
task.get();
return 0;
}
IAsyncAction OpenPicture() { StorageFile file = co_await StorageFile::GetFileFromPathAsync(L"d:\temp\Function.png"); LauncherOptions options;
options.PreferredApplicationPackageFamilyName(L"Microsoft.Windows.Photos_8wekyb3d8bbwe");
co_await Launcher::LaunchFileAsync(file, options);
}
According to IntelliSense, the code is fine, but when I compile, I get the following error messages:
Error C2512 'winrt::Windows::Storage::StorageFile': no appropriate default constructor available
Error C3779 'winrt::Windows::Storage::StorageFile::GetFileFromPathAsync': a function that returns 'auto' cannot be used before it is defined WinRTTest
If I place the cursor on GetFileFromPathAsync
and press F12 (Go to definition), I get the following definition:
static auto GetFileFromPathAsync(param::hstring const& path);
When I press F1 on GetFileFromPathAsync
, I get the following definition in the documentation:
static IAsyncOperation<StorageFile> GetFileFromPathAsync(winrt::hstring const& path);
If I would use StorageFile * file
instead of StorageFile file
, I would not get the 1st compiler error.
How do I solve the compiler errors?
Try to add this directive to pch.h: #include "winrt/Windows.Storage.Streams.h"
.
And use this line:
StorageFile file = co_await StorageFile::GetFileFromPathAsync( LR"(d:\temp\Function.png)" );