winrt::Windows::Devices::Usb::UsbDevice::FromIdAsync throws exception on get

Tunghohin 0 Reputation points
2024-05-28T08:54:29.7266667+00:00

code sample:

#include <guiddef.h>
#include <windows.h>
#include <ppltasks.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Devices.Enumeration.h>
#include <winrt/Windows.Devices.Usb.h>

using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Devices::Enumeration;
using namespace Windows::Devices::Usb;


class CameraType {
    using devices_list = std::vector<CameraType>;
public:
    explicit inline CameraType(DeviceInformation const& info)
     : info_(std::move(info)) {} 
    CameraType(CameraType const& obj) = default;
    CameraType(CameraType&& obj) = default;
    auto operator = (CameraType const& obj) -> CameraType& = default;
    auto operator = (CameraType&& obj) -> CameraType& = default;

    auto get_devinfo() const -> DeviceInformation const&;

    static auto find_in_env() -> concurrency::task<devices_list>;
    static auto is_this_model(DeviceInformation const&) -> bool;
private:
    DeviceInformation info_;
};

auto CameraType::get_devinfo() const -> DeviceInformation const& {
    return this->info_;
}

auto CameraType::is_this_model(DeviceInformation const& device) -> bool {
    return true; // not important
}

auto CameraType::find_in_env() -> concurrency::task<devices_list> {
    namespace rv = std::ranges::views;
    return concurrency::create_task([] { 
        return DeviceInformation::FindAllAsync(
            DeviceClass::VideoCapture
        ).get() 
        | rv::filter([](DeviceInformation const& device) {
            return CameraType::is_this_model(device);
        })
        | rv::transform([](DeviceInformation const& device) -> CameraType {
            return CameraType{device};
        })
        | std::to<std::vector>();
    });
}

auto main(int argc, char** argv) -> int {
    winrt::init_apartment();
    auto vec = CameraType::find_in_env().get();
    auto s = UsbDevice::FromIdAsync(vec[0].get_devinfo().Id()).get();
}

enter image description here

屏幕截图 2024-05-28 164934

Am I using it the wrong way? Or is this a bug?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,764 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.