Confusion casting WinRT object to COM object (Unable to cast COM object to System.IDisposable) #2090
Replies: 1 comment
-
|
CsWin32 by default uses the .NET's built-in With that said, CsWin32 recently added a new mode which relies on the source generated COM marshalers support that .NET has added to replace |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I was trying to use CreateDirect3D11DeviceFromDXGIDevice, a function that takes a ID3D11Device (COM interface) and outputs an IInspectable that implements IDirect3DDevice, which is a WinRT type.
Using CsWin32 I got a
Windows.Win32.System.WinRT.IInspectable, which is very easy to confuse with aWinRT.IInspectableif you're using implicit types.Then I cast it to Windows.Graphics.DirectX.Direct3D11.IDirect3DDevice, which is a WinRT type (an interface with the [WindowsRuntimeType] attribute, and without [ComImport]).
Either of these have the same result:
Then I noticed the object implemented IDisposable, so I tried to dispose it, and...
The problem is that confusing_object here is not a WinRT proxy, as one would expect from the type, but rather a RCW (i.e. a COM proxy object). As I understand, the runtime saw an interface definition with a GUID and quietly generated a COM implementation for it. (I also have no idea where that GUID for IDisposable came from)
But trying to diagnose this problem from that exception is almost impossible if you don't have strong prior understanding of COM interop and WinRT interop, and most people would expect that if a type is in the WinRT namespace, the implementation would be WinRT-aware.
Is there anything that could be done on either side to prevent this?
Should the As() method detect this and handle the conversion properly? There is no documentation on any classes or methods so I'm not really sure what to expect from them.
Maybe the library could incorporate a .net code analyzer that gives warnings if it detects suspicious casts or usage?
(I'm also posting this so people who have that error will be able to find the answer)
Beta Was this translation helpful? Give feedback.
All reactions