1

我一直在研究DirectXTK 示例项目中的代码,并尝试在一个新项目中实现它。不过,微软似乎建议在新项目中使用 WinRT,所以我决定尝试切换 to 的WRL::ComPtr实例winrt::com_ptr。不过,我被困住了,试图ID3D11Device1在项目的类GameID3DDevice.D3D11CreateDevice()

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

ComPtr<ID3D11Device1> global_device;

void CreateDevice()
{
    ...

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

这是我使用 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>();
}

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

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    

我已经阅读了这里的文档、WRL::ComPtr.As() 这里的文档以及winrt::com_ptr.as() 这里的“转换”示例我现在可以忍受的次数已经差不多了。我错过了什么?

4

1 回答 1

3

根据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-版本 1809

于 2019-06-23T08:22:08.803 回答