You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
In WinUI 2 I had a "Share" functionality to share an image with Windows 10 system APIs. DataTransferManager has an event called DataRequested that you need to subscribe to that makes this work.
In WinUI3 with CsWinRT this is pretty difficult, because you need to get your `DataTransferManager from IDataTransferManagerInterop, like this:
usingWindows.ApplicationModel.DataTransfer;usingWindows.Win32.UI.Shell;varguid=Guid.Parse("a5caee9b-8708-49d1-8d36-67d25a8da00c");variop=DataTransferManager.As<IDataTransferManagerInterop>();varmanager=iop.GetForWindow(GetMainHwnd(),guid);vardtm=managerasDataTransferManager;// cast is always null here, I believe manager should be cast to an interface, but no interface for DataTransferManager exists.
However, it is unclear to me how to use that returned object. I can't cast to a DataTransferManager, maybe because that would only work with an interface? But DataTransferManager does not have any interface in the API docs.
I'm getting the IDataTransferManagerInterop from CsWin32, I asked on their repo, but no solution could be found.
Is this a projection issue with CsWinRT, or should I be asking somewhere else?
Marvin Rühe (@Marv51) there are details in #966 that may help with this scenario. You will need to do something like the following, where eventArg is the DataTransferManager object instead.
Full exception:
System.InvalidCastException: 'Unable to cast COM object of type 'System.__ComObject' to class type 'WinRT.IInspectable'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.'
The way lhak does it, works for me. So is this a CsWin32 issue and the return value should be an IntPtr? Or is there a different way to use the objectreturn value?
Marvin Rühe (@Marv51) a return type of object relies on the .NET runtime to handle the marshaling which is no longer supported starting with .NET 5. The replacement for this is the IntPtr return with marshaling done by the CsWinRT APIs.
Activity
angelazhangmsft commentedon Aug 19, 2021
Marvin Rühe (@Marv51) there are details in #966 that may help with this scenario. You will need to do something like the following, where
eventArgis theDataTransferManagerobject instead.Marv51 commentedon Aug 20, 2021
angelazhangmsft Thank you so much! Unfortunately, when I cast to IInspectable I get an InvalidCastException.
Could this be happening, because in CsWin32 the object is marshalled as IUnknown?
Full exception:
System.InvalidCastException: 'Unable to cast COM object of type 'System.__ComObject' to class type 'WinRT.IInspectable'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.'
lhak commentedon Aug 20, 2021
This code worked for me in the past (have not tested it for a couple of weeks so some things might have changed):
Marv51 commentedon Aug 30, 2021
The way lhak does it, works for me. So is this a CsWin32 issue and the return value should be an
IntPtr? Or is there a different way to use theobjectreturn value?manodasanW commentedon Aug 31, 2021
Marvin Rühe (@Marv51) a return type of object relies on the .NET runtime to handle the marshaling which is no longer supported starting with .NET 5. The replacement for this is the IntPtr return with marshaling done by the CsWinRT APIs.