1

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?

yingpar
  • 41
  • 4
  • This seems to be the root of the problem: "Reason: cannot convert from 'const winrt::guid' to 'const IID'", but I can't find any other examples of it on Google, which seems to indicate that I've done something uniquely bone-headed here. – yingpar Jun 23 '19 at 04:01
  • From [News, and changes, in Windows SDK version 10.0.17763.0 (Windows 10, version 1809)](https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/news#news-and-changes-in-windows-sdk-version-100177630-windows-10-version-1809): *"`winrt::guid` converts to GUID, as long as you include unknwn.h before you include any C++/WinRT headers."* Maybe that's applicable here. – IInspectable Jun 23 '19 at 07:09
  • Yep, that did it. It also cleared out a couple of weird warnings. Always read the patch notes--thanks! – yingpar Jun 23 '19 at 08:19
  • BTW, you can mix WRL and C++/WinRT in the same project. I use ``Microsoft::WRL::ComPtr`` because it's ubiquitous back to the Windows 8 SDK. The C++/WinRT solution is good, but it's not available in the default SDKs prior to Windows 10 SDK (17134) which also means it's not available for VS 2015. – Chuck Walbourn Jun 24 '19 at 16:27

1 Answers1

3

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://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/news#news-and-changes-in-windows-sdk-version-100177630-windows-10-version-1809

yingpar
  • 41
  • 4