当我使用来自 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 usewinrt::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 aswinrt::guid
. For APIs that you implement, you must usewinrt::guid
forGUID
parameters. Otherwise, there are automatic conversions betweenwinrt::guid
andGUID
as long as you includeunknwn.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/