![](/img/trans.png)
[英]error C2664: 'int printf(const char *const ,…)': cannot convert argument 2 from 'void' to '…'
[英]Error C2664 'HRESULT IUnknown::QueryInterface(const IID &,void **)': cannot convert argument 1 from 'const winrt::guid' to 'const IID &'
當我使用來自microsoft docs 的幫助程序 function 從 cx 遷移到 winrt時,會發生此錯誤。 我在這里看到了一個類似的問題,但提到的解決方案似乎對我不起作用。 此處提到的解決方案在有此錯誤的文件中的任何其他 winrt 標頭之前添加 #include <Unknwn.h>。
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
。 根據C++/WinRT 中的新增功能:
- 突破性的變化。 GUID 現在投影為
winrt::guid
。 對於您實現的 API,您必須將winrt::guid
用於 GUID 參數。 否則,只要在包含任何 C++/WinRT 標頭之前包含 unknwn.h ,winrt::guid
轉換為 GUID。 請參閱與 ABI 的 GUID 結構互操作。
GUID
投影為winrt::guid
。 對於您實現的 API,您必須將winrt::guid
用於GUID
參數。 否則,只要在包含任何 C++/WinRT 標頭之前包含unknwn.h
(由 <windows.h> 和許多其他 header 文件隱式包含),winrt::guid
和GUID
之間就會自動轉換。如果你不這樣做,那么你可以在它們之間進行 hard-
reinterpret_cast
。
因此,要么確保在 WinRT 標頭之前包含unknwn.h
,要么您可以明確地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;
}
IInspectable 的評論也對我有用:
如果您#include <Unknwn.h>,請確保您確實在任何 winrt 標頭之前包含它。 使用預編譯頭文件時,您也必須包含 Unknwn.h。
.....在任何其他 winrt header 文件之前,您的編譯單元必須包含 Unknwn.h,這一點至關重要。 編譯單元的文件擴展名通常為.cpp、.cc 或.cxx。
我認為 header 必須包含在定義這些輔助函數的文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.