Soleil
Soleil

Reputation: 7287

Porting Directx12 Asset into XAML element: advice on WRL::ComPtr replacement?

I'm working on creating a WPF asset (like D3D11Image), from a DirectX12 Win32 desktop sample : D3D12PipelineStateCache, so I can embedd it as XAML element in a WPF application.

Microsoft Directx12 samples are having extensive usage of ComPtr (using Microsoft::WRL::ComPtr; and #include <wrl.h>), smart pointers, but build fails because of:

// Don't allow to compile sources with /clr
#ifdef _MANAGED
#error WRL cannot be compiled with /clr option enabled
#endif

Repro: I started to change configuration properties in the D3D12PipelineStateCache project:

The goal was to "detach stop by stop the code from win32" and make it interfaced with XAML assets.

Do you have any recommendation of smart pointer to replace ComPtr or do you recommend to proceed differently, eg., by building a pure c++ dll with interop with clr dll, and in that case, how ?

Upvotes: 2

Views: 370

Answers (1)

Chuck Walbourn
Chuck Walbourn

Reputation: 41057

WRL generally assumes you will be using the Windows Runtime which is explicitly designed to interop with C# and C++, so the Managed C++ (/clr)) scenario is exclude.

You should be able to use ATL's CComPtr instead by including <atlbase.h>, but it will take some code changes to work.

  • Keep in mind is that operator& in the older ATL CComPtr asserts that the pointer is always null before doing the equivalent of GetAddressOf. In ComPtr the use of operator& explicitly calls the equivalent of ReleaseAndGetAddressOf to release any existing pointer to avoid potential memory leaks.

  • There's no Get, GetAddressOf, or ReleaseAndGetAddressOf method as CComPtr uses the old-school automatic conversion to a raw pointer which is problematic

  • There is no Reset method for clearing CComPtr so you have to set it to NULL.

  • There is no As method so you have to use the more wordy QueryInterface solution.

You can probably work around most of these issues by deriving a helper version that adds the missing methods but it will take a bit of work...

struct MyComPtr : public ATL::CComPtr

Upvotes: 2

Related Questions