【问题标题】:Using winrt::com_ptr<ID3D11Device1> with D3D11CreateDevice()?将 winrt::com_ptr<ID3D11Device1> 与 D3D11CreateDevice() 一起使用?
【发布时间】:2019-06-23 03:52:01
【问题描述】:

我一直在研究DirectXTK example project 的代码,并尝试在一个新项目中实现它。不过,它看起来像 Microsoft recommends using WinRT in new projects,所以我决定尝试将 WRL::ComPtr 的实例切换到 winrt::com_ptr。不过,我被困住了,试图在项目的Game 类中的ID3D11Device1D3D11CreateDevice() 中的ID3DDevice 之间移动。

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

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() here 的文档,winrt::com_ptr.as() here 的文档,以及“转换”示例 here 的次数,我现在可以忍受的次数差不多了。我错过了什么?

【问题讨论】:

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

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


【解决方案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

【讨论】:

    猜你喜欢
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 2020-10-29
    • 2013-12-11
    相关资源
    最近更新 更多