c++ - 错误 C2664 'HRESULT IUnknown::QueryInterface(const IID &,void **)' : cannot convert argument 1 from 'const winrt::guid' to 'const IID &'

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

当我使用来自 microsoft docs to migrate to winrt from cx 的辅助函数时,这个错误发生在我身上.我看到一个类似的问题 here ,但提到的解决方案似乎对我不起作用。此处提到的解决方案在文件中出现此错误的任何其他 winrt header 之前添加#include

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;
}

这是整个文件:

#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)));
    }
}
}

最佳答案

winrt::guid_of()返回 winrt::guid。每What's new in C++/WinRT :

  • Breaking change. GUID is now projected as winrt::guid. For APIs that you implement, you must use winrt::guid for GUID parameters. Otherwise, winrt::guid converts to GUID, as long as you include unknwn.h before you include any C++/WinRT headers. See Interoperating with the ABI's GUID struct.

根据 Interoperating with the ABI's GUID struct :

GUID is projected as winrt::guid. For APIs that you implement, you must use winrt::guid for GUID parameters. Otherwise, there are automatic conversions between winrt::guid and GUID as long as you include unknwn.h (implicitly included by <windows.h> and many other header files) before you include any C++/WinRT headers.

If you don't do that, then you can hard-reinterpret_cast between them.

因此,要么确保 unknwn.h 包含在 WinRT header 之前,要么您可以显式地reinterpret_cast,例如:

template <typename T>
T from_cx(Platform::Object ^ from) {
    T to{nullptr};
    winrt::guid iid = winrt::guid_of<T>();

    winrt::check_hresult(
        reinterpret_cast<::IUnknown*>(from)->QueryInterface(
            reinterpret_cast<GUID&>(iid),
            reinterpret_cast<void**>(winrt::put_abi(to)))
    );

    return to;
}

关于c++ - 错误 C2664 'HRESULT IUnknown::QueryInterface(const IID &,void **)' : cannot convert argument 1 from 'const winrt::guid' to 'const IID &' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63382869/

相关文章:

c# - 在 x64 中从 C# 调用 C++ 代码,所有参数都移动 1

c++ - 同一文件流中的 fread 和 fgetc 不起作用

WPF 同一系列的多个字体文件

c++ - 带有额外可选模板参数的标准库容器?

javascript - 如何在 Windows 8 Metro 应用程序中保存捕获的视频文件?

javascript - 如何从WinJS/javascript使用WebView控件navigateToLocalStreamUri

vb.net - 在WinRT 8.1中使用sqlite使用datetime数据类型保存日期和时间:System.InvalidCastException

c# - 如何从 xaml.cs 在 xaml 中设置 ScaleTransform ScaleX 属性值

c#-4.0 - 在 WinRT 上保存带有背景图像的 Canvas

c++ - 可移植可执行 DOS header 长度