C++ 使用winrt::com_ptr<;ID3D11设备1>;使用D3D11CreateDevice()?

C++ 使用winrt::com_ptr<;ID3D11设备1>;使用D3D11CreateDevice()?,c++,com,directx,c++-winrt,wrl,C++,Com,Directx,C++ Winrt,Wrl,我一直在研究源代码,并试图在一个新项目中实现它。不过,它看起来像微软,所以我决定尝试将WRL::ComPtr的实例切换到winrt::com\u ptr。不过,我被困在了项目的Game类中的ID3D11Device1和D3D11CreateDevice()中的ID3DDevice之间 下面是示例代码,为了简单起见稍微抽象了一下: ComPtr全局_设备; void CreateDevice() { ... ComPtr本地设备; 通过调用(D3D11CreateDevice(…local_dev

我一直在研究源代码,并试图在一个新项目中实现它。不过,它看起来像微软,所以我决定尝试将
WRL::ComPtr
的实例切换到
winrt::com\u ptr
。不过,我被困在了项目的
Game
类中的
ID3D11Device1
D3D11CreateDevice()中的
ID3DDevice
之间

下面是示例代码,为了简单起见稍微抽象了一下:

ComPtr全局_设备;
void CreateDevice()
{
...
ComPtr本地设备;
通过调用(D3D11CreateDevice(…local_device.release和getAddressof());
ThrowIfFailed(本地_设备.As(&global_设备));
}
下面是我对WinRT的近似值:

com_ptr全局_设备;
void createDevice()
{
...
com_ptr本地_设备;
检查结果(D3D11CreateDevice(…local_device.put()…);
全局_设备=本地_设备.as();
}
但是,每次我运行它时,都会出现以下错误:

Error   C2664    'HRESULT IUnknown::QueryInterface(const IID &,void **)': cannot convert argument 1 from 'const winrt::guid' to 'const IID &'   HelloDX11   .\x64\Debug\Generated Files\winrt\base.h    1955    
Message      No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called   HelloDX11   .\x64\Debug\Generated Files\winrt\base.h    1955    
Message      Reason: cannot convert from 'const winrt::guid' to 'const IID' HelloDX11   .\x64\Debug\Generated Files\winrt\base.h    1955    
Message      see reference to function template instantiation 'winrt::com_ptr<ID3D11Device1> winrt::com_ptr<ID3D11Device>::as<ID3D11Device1>(void) const' being compiled    HelloDX11   .\game.cpp  47  
Message      see reference to function template instantiation 'winrt::com_ptr<ID3D11Device1> winrt::impl::as<To,ID3D11Device>(From *)' being compiled
        with
        [
            To=ID3D11Device1,
            From=ID3D11Device
        ]   HelloDX11   .\x64\Debug\Generated Files\winrt\base.h    2377    
错误C2664'HRESULT IUnknown::QueryInterface(const IID&,void**)∶无法将参数1从'const winrt::guid'转换为'const IID&'HelloDX11。\x64\Debug\Generated Files\winrt\base.h 1955
消息没有可执行此转换的用户定义的转换运算符,或者该运算符不能称为HelloDX11。\x64\Debug\Generated Files\winrt\base.h 1955
消息原因:无法从“const winrt::guid”转换为“const IID”HelloDX11。\x64\Debug\Generated Files\winrt\base.h 1955
消息请参阅正在编译HelloDX11的函数模板实例化“winrt::com_ptr winrt::com_ptr::as(void)const”的参考。\game.cpp 47
消息请参阅正在编译的函数模板实例化“winrt::com_ptr winrt::impl::as(From*)”的引用
具有
[
To=ID3D11Device1,
From=ID3D11设备
]HelloDX11。\x64\Debug\Generated Files\winrt\base.h 2377
我已经阅读了
WRL::ComPtr.As()
winrt::com\u ptr.As()
的文档,以及“转换”示例,这些都是我目前所能理解的。我遗漏了什么?

回答per的评论:


winrt::guid
转换为guid,只要在包含任何C++/winrt头之前包含Unknwn.h。”请参阅:

这似乎是问题的根源:“原因:无法从'const winrt::guid'转换为'const IID',但我在Google上找不到任何其他示例,这似乎表明我在这里做了一些独特的事情。From:“
winrt::guid
转换为guid,只要在包含任何C++/winrt头之前包含unknwn.h。”也许这在这里是适用的。是的,就是这样。它还清除了一些奇怪的警告。请务必阅读补丁说明--谢谢!顺便说一句,您可以在同一个项目中混合使用WRL和C++/WinRT。我使用
Microsoft::WRL::ComPtr
,因为它在Windows8SDK中无处不在。C++/WinRT解决方案很好,但它在Windows 10 SDK(17134)之前的默认SDK中不可用,这也意味着它不适用于VS 2015。