使用winrt :: com_ptr <ID3D11Device1>与D3D11CreateDevice()吗?

时间:2019-06-23 03:52:01

标签: c++ com directx c++-winrt wrl

我一直在研究DirectXTK example project中的代码,并尝试在新项目中实现它。不过,好像Microsoft recommends using WinRT in new projects一样,所以我决定尝试将WRL::ComPtr的实例切换为winrt::com_ptr。不过,我被困在尝试在项目ID3D11Device1类的GameID3DDevice的{​​{1}}之间切换。

这是示例代码,为简单起见,对其进行了略微抽象:

D3D11CreateDevice()

这是我在WinRT中的近似值:

ComPtr<ID3D11Device1> global_device;

void CreateDevice()
{
    ...

    ComPtr<ID3D11Device> local_device;
    ThrowIfFailed(D3D11CreateDevice( ... local_device.ReleaseAndGetAddressOf() ... ));
    ThrowIfFailed(local_device.As(&global_device));
}

但是,每次运行它时,都会出现以下错误:

com_ptr<ID3D11Device1> global_device;

void createDevice()
{
    ...

    com_ptr<ID3D11Device> local_device;
    check_hresult(D3D11CreateDevice( ... local_device.put() ... ));
    global_device = local_device.as<ID3D11Device1>();
}

我已经遍历了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 here的文档,WRL::ComPtr.As() here的文档以及“转换”示例here的内容,现在我可以忍受的时间。我想念什么?

1 个答案:

答案 0 :(得分:1)

回答IInspectable的评论:

“ {winrt::guid会转换为GUID,只要在包含任何C ++ / WinRT标头之前就包含Unknwn.h。”参见:https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/news#news-and-changes-in-windows-sdk-version-100177630-windows-10-version-1809