Using winrt::com_ptr<ID3D11Device1> with D3D11CreateDevice()?

荒凉一梦 提交于 2020-12-07 05:15:48

问题


I've been studying the code from the DirectXTK example project and trying to implement it in a new project. It seems like Microsoft recommends using WinRT in new projects, though, so I decided I would try to switch instances of WRL::ComPtr to winrt::com_ptr. I'm stuck, though, trying to move between ID3D11Device1 in the project's Game class and ID3DDevice in D3D11CreateDevice().

Here's the example code, slightly abstracted for simplicity's sake:

ComPtr<ID3D11Device1> global_device;

void CreateDevice()
{
    ...

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

And here's my approximation of it with WinRT:

com_ptr<ID3D11Device1> global_device;

void createDevice()
{
    ...

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

Every time I run it, though, I get these errors:

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    

I've gone over the docs for WRL::ComPtr.As() here, the docs for winrt::com_ptr.as() here, and the "conversion" example here about as many times as I can stand at this point. What am I missing?


回答1:


Answer per IInspectable's comment:

"winrt::guid converts to GUID, as long as you include Unknwn.h before you include any C++/WinRT headers." See: 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



来源:https://stackoverflow.com/questions/56720866/using-winrtcom-ptrid3d11device1-with-d3d11createdevice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!