<PackageReference Include="System.Drawing.Common" Version="10.0.0-rc.1.25451.107" />

Composition<TOleServices, TNrbfSerializer, TDataFormat>

sealed class Composition<TOleServices, TNrbfSerializer, TDataFormat> : IDataObjectInternal, Interface, IDataObject where TOleServices : IOleServices where TNrbfSerializer : INrbfSerializer where TDataFormat : IDataFormat<TDataFormat>
Contains the logic to move between IDataObjectInternal, Interface, and IDataObject calls.
using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Private.Windows.Core.Resources; using System.Private.Windows.Nrbf; using System.Reflection.Metadata; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; using System.Runtime.Serialization; using System.Text; using Windows.Win32; using Windows.Win32.Foundation; using Windows.Win32.System.Com; using Windows.Win32.System.Memory; using Windows.Win32.UI.Shell; namespace System.Private.Windows.Ole { [NullableContext(1)] [Nullable(0)] internal sealed class Composition<[Nullable(0)] TOleServices, [Nullable(0)] TNrbfSerializer, [Nullable(0)] TDataFormat> : IDataObjectInternal, global::Windows.Win32.System.Com.IDataObject.Interface, System.Runtime.InteropServices.ComTypes.IDataObject where TOleServices : IOleServices where TNrbfSerializer : INrbfSerializer where TDataFormat : IDataFormat<TDataFormat> { [NullableContext(0)] private sealed class ManagedToNativeAdapter : global::Windows.Win32.System.Com.IDataObject.Interface, IManagedWrapper<global::Windows.Win32.System.Com.IDataObject>, IManagedWrapper { private const int DATA_S_SAMEFORMATETC = 262448; [Nullable(1)] private readonly IDataObjectInternal _dataObject; [NullableContext(1)] public ManagedToNativeAdapter(IDataObjectInternal dataObject) { _dataObject = dataObject; } private static bool GetTymedUsable(global::Windows.Win32.System.Com.TYMED tymed) { return (tymed & (global::Windows.Win32.System.Com.TYMED)21) != global::Windows.Win32.System.Com.TYMED.TYMED_NULL; } public unsafe HRESULT GetData(global::Windows.Win32.System.Com.FORMATETC* pformatetcIn, global::Windows.Win32.System.Com.STGMEDIUM* pmedium) { if (pformatetcIn == null) return HRESULT.DV_E_FORMATETC; if (pmedium == null) return HRESULT.E_POINTER; if (DragDropHelper<TOleServices, TDataFormat>.IsInDragLoop(_dataObject)) { string name = DataFormatsCore<TDataFormat>.GetOrAddFormat(pformatetcIn->cfFormat).Name; if (!_dataObject.GetDataPresent(name)) { *pmedium = default(global::Windows.Win32.System.Com.STGMEDIUM); return HRESULT.S_OK; } DragDropFormat dragDropFormat = _dataObject.GetData(name) as DragDropFormat; if (dragDropFormat != null) { *pmedium = dragDropFormat.GetData(); return HRESULT.S_OK; } } *pmedium = default(global::Windows.Win32.System.Com.STGMEDIUM); if (!GetTymedUsable((global::Windows.Win32.System.Com.TYMED)pformatetcIn->tymed)) return HRESULT.DV_E_TYMED; if (!((global::Windows.Win32.System.Com.TYMED)pformatetcIn->tymed).HasFlag(global::Windows.Win32.System.Com.TYMED.TYMED_HGLOBAL)) { pmedium->tymed = (global::Windows.Win32.System.Com.TYMED)pformatetcIn->tymed; return GetDataHere(pformatetcIn, pmedium); } pmedium->tymed = global::Windows.Win32.System.Com.TYMED.TYMED_HGLOBAL; pmedium->hGlobal = PInvokeCore.GlobalAlloc(GLOBAL_ALLOC_FLAGS.GHND, (UIntPtr)(void*)1); if (pmedium->hGlobal.IsNull) return HRESULT.E_OUTOFMEMORY; HRESULT dataHere = GetDataHere(pformatetcIn, pmedium); if (dataHere.Failed) { PInvokeCore.GlobalFree(pmedium->hGlobal); pmedium->hGlobal = HGLOBAL.Null; } return dataHere; } public unsafe HRESULT GetDataHere(global::Windows.Win32.System.Com.FORMATETC* pformatetc, global::Windows.Win32.System.Com.STGMEDIUM* pmedium) { if (pformatetc == null) return HRESULT.DV_E_FORMATETC; if (pmedium == null) return HRESULT.E_POINTER; if (!GetTymedUsable((global::Windows.Win32.System.Com.TYMED)pformatetc->tymed) || !GetTymedUsable(pmedium->tymed)) return HRESULT.DV_E_TYMED; string name = DataFormatsCore<TDataFormat>.GetOrAddFormat(pformatetc->cfFormat).Name; if (!_dataObject.GetDataPresent(name)) return HRESULT.DV_E_FORMATETC; object data = _dataObject.GetData(name); if (data == null) return HRESULT.E_UNEXPECTED; if (((global::Windows.Win32.System.Com.TYMED)pformatetc->tymed).HasFlag(global::Windows.Win32.System.Com.TYMED.TYMED_HGLOBAL)) try { HRESULT hRESULT = SaveDataToHGLOBAL(data, name, ref *pmedium); if (hRESULT != HRESULT.E_UNEXPECTED) return hRESULT; } catch (NotSupportedException data2) { return SaveDataToHGLOBAL(data2, name, ref *pmedium); } catch (Exception) when (!pmedium->hGlobal.IsNull) { PInvokeCore.GlobalFree(pmedium->hGlobal); pmedium->hGlobal = HGLOBAL.Null; throw; } return ((IOleServices)).GetDataHere(name, data, pformatetc, pmedium); } public unsafe HRESULT QueryGetData(global::Windows.Win32.System.Com.FORMATETC* pformatetc) { if (pformatetc == null) return HRESULT.DV_E_FORMATETC; if (pformatetc->dwAspect != 1) return HRESULT.DV_E_DVASPECT; if (!GetTymedUsable((global::Windows.Win32.System.Com.TYMED)pformatetc->tymed)) return HRESULT.DV_E_TYMED; if (pformatetc->cfFormat == 0) return HRESULT.S_FALSE; if (!_dataObject.GetDataPresent(DataFormatsCore<TDataFormat>.GetOrAddFormat(pformatetc->cfFormat).Name)) return HRESULT.DV_E_FORMATETC; return HRESULT.S_OK; } public unsafe HRESULT GetCanonicalFormatEtc(global::Windows.Win32.System.Com.FORMATETC* pformatectIn, global::Windows.Win32.System.Com.FORMATETC* pformatetcOut) { if (pformatetcOut == null) return HRESULT.E_POINTER; *pformatetcOut = default(global::Windows.Win32.System.Com.FORMATETC); return (HRESULT)262448; } public unsafe HRESULT SetData(global::Windows.Win32.System.Com.FORMATETC* pformatetc, global::Windows.Win32.System.Com.STGMEDIUM* pmedium, BOOL fRelease) { if (pformatetc == null) return HRESULT.DV_E_FORMATETC; if (pmedium == null) return HRESULT.E_POINTER; if (!DragDropHelper<TOleServices, TDataFormat>.IsInDragLoopFormat(*pformatetc) && !DragDropHelper<TOleServices, TDataFormat>.IsInDragLoop(_dataObject)) return HRESULT.E_NOTIMPL; string name = DataFormatsCore<TDataFormat>.GetOrAddFormat(pformatetc->cfFormat).Name; if (_dataObject.GetDataPresent(name)) { DragDropFormat dragDropFormat = _dataObject.GetData(name) as DragDropFormat; if (dragDropFormat != null) { dragDropFormat.RefreshData(pformatetc->cfFormat, *pmedium, !(bool)fRelease); goto IL_00b3; } } _dataObject.SetData(name, new DragDropFormat(pformatetc->cfFormat, *pmedium, !(bool)fRelease)); goto IL_00b3; IL_00b3: return HRESULT.S_OK; } public unsafe HRESULT EnumFormatEtc(uint dwDirection, global::Windows.Win32.System.Com.IEnumFORMATETC** ppenumFormatEtc) { if (ppenumFormatEtc == null) return HRESULT.E_POINTER; if (dwDirection == 1) { *ppenumFormatEtc = ComHelpers.GetComPointer<global::Windows.Win32.System.Com.IEnumFORMATETC>((object)new FormatEnumerator(_dataObject, (string format) => DataFormatsCore<TDataFormat>.GetOrAddFormat(format).Id)); return HRESULT.S_OK; } return HRESULT.E_NOTIMPL; } public unsafe HRESULT DAdvise(global::Windows.Win32.System.Com.FORMATETC* pformatetc, uint advf, global::Windows.Win32.System.Com.IAdviseSink* pAdvSink, uint* pdwConnection) { if (pdwConnection == null) return HRESULT.E_POINTER; *pdwConnection = 0; return HRESULT.E_NOTIMPL; } public HRESULT DUnadvise(uint dwConnection) { return HRESULT.E_NOTIMPL; } public unsafe HRESULT EnumDAdvise(global::Windows.Win32.System.Com.IEnumSTATDATA** ppenumAdvise) { if (ppenumAdvise == null) return HRESULT.E_POINTER; *ppenumAdvise = null; return HRESULT.OLE_E_ADVISENOTSUPPORTED; } [NullableContext(1)] private HRESULT SaveDataToHGLOBAL(object data, string format, ref global::Windows.Win32.System.Com.STGMEDIUM medium) { Stream stream = data as Stream; if (stream != null) return stream.SaveStreamToHGLOBAL(ref medium.hGlobal); if (format != null) { switch (format.Length) { case 4: switch (format[0]) { case 'T': break; case 'X': goto IL_00b8; default: goto IL_023d; } if (!(format == "Text")) break; goto IL_017f; case 11: switch (format[0]) { case 'H': break; case 'U': if (format == "UnicodeText") return <SaveDataToHGLOBAL>g__SaveUtf16ToHGLOBAL|13_3(medium.hGlobal, data.ToString() ?? ""); goto IL_023d; default: goto IL_023d; } if (!(format == "HTML Format")) break; goto IL_01a4; case 8: switch (format[4]) { case 'D': if (format == "FileDrop") return <SaveDataToHGLOBAL>g__SaveFileListToHGLOBAL|13_1(medium.hGlobal, (string[])data); break; case 'N': if (format == "FileName") return <SaveDataToHGLOBAL>g__SaveDbcsToHGLOBAL|13_2(medium.hGlobal, ((string[])data)[0]); break; } break; case 16: if (!(format == "Rich Text Format")) break; goto IL_017f; case 7: if (!(format == "OEMText")) break; goto IL_017f; case 9: { if (format == "FileNameW") return <SaveDataToHGLOBAL>g__SaveUtf16ToHGLOBAL|13_3(medium.hGlobal, ((string[])data)[0]); break; } IL_01a4: return <SaveDataToHGLOBAL>g__SaveUtf8ToHGLOBAL|13_4(medium.hGlobal, data.ToString() ?? ""); IL_017f: return <SaveDataToHGLOBAL>g__SaveDbcsToHGLOBAL|13_2(medium.hGlobal, data.ToString() ?? ""); IL_00b8: if (!(format == "Xaml")) break; goto IL_01a4; } } goto IL_023d; IL_023d: if (!(format == "PersistentObject") && !(data is ISerializable) && !data.GetType().IsSerializable) return HRESULT.E_UNEXPECTED; return <SaveDataToHGLOBAL>g__SaveObjectToHGLOBAL|13_0(ref medium.hGlobal, data, format); } } [Nullable(0)] private sealed class NativeToManagedAdapter : IDataObjectInternal, global::Windows.Win32.System.Com.IDataObject.Interface { private readonly AgileComPointer<global::Windows.Win32.System.Com.IDataObject> _nativeDataObject; [NullableContext(0)] public unsafe NativeToManagedAdapter(global::Windows.Win32.System.Com.IDataObject* dataObject) { _nativeDataObject = new AgileComPointer<global::Windows.Win32.System.Com.IDataObject>(dataObject, true, 0); } [NullableContext(0)] public unsafe HRESULT DAdvise(global::Windows.Win32.System.Com.FORMATETC* pformatetc, uint advf, global::Windows.Win32.System.Com.IAdviseSink* pAdvSink, uint* pdwConnection) { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { return interface.Value->DAdvise(pformatetc, advf, pAdvSink, pdwConnection); } finally { interface.Dispose(); } } public unsafe HRESULT DUnadvise(uint dwConnection) { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { return interface.Value->DUnadvise(dwConnection); } finally { interface.Dispose(); } } [NullableContext(0)] public unsafe HRESULT EnumDAdvise(global::Windows.Win32.System.Com.IEnumSTATDATA** ppenumAdvise) { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { return interface.Value->EnumDAdvise(ppenumAdvise); } finally { interface.Dispose(); } } [NullableContext(0)] public unsafe HRESULT EnumFormatEtc(uint dwDirection, global::Windows.Win32.System.Com.IEnumFORMATETC** ppenumFormatEtc) { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { return interface.Value->EnumFormatEtc(dwDirection, ppenumFormatEtc); } finally { interface.Dispose(); } } [NullableContext(0)] public unsafe HRESULT GetData(global::Windows.Win32.System.Com.FORMATETC* pformatetcIn, global::Windows.Win32.System.Com.STGMEDIUM* pmedium) { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { return interface.Value->GetData(pformatetcIn, pmedium); } finally { interface.Dispose(); } } [NullableContext(0)] public unsafe HRESULT GetDataHere(global::Windows.Win32.System.Com.FORMATETC* pformatetc, global::Windows.Win32.System.Com.STGMEDIUM* pmedium) { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { return interface.Value->GetDataHere(pformatetc, pmedium); } finally { interface.Dispose(); } } [NullableContext(0)] public unsafe HRESULT QueryGetData(global::Windows.Win32.System.Com.FORMATETC* pformatetc) { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { return interface.Value->QueryGetData(pformatetc); } finally { interface.Dispose(); } } [NullableContext(0)] public unsafe HRESULT GetCanonicalFormatEtc(global::Windows.Win32.System.Com.FORMATETC* pformatectIn, global::Windows.Win32.System.Com.FORMATETC* pformatetcOut) { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { return interface.Value->GetCanonicalFormatEtc(pformatectIn, pformatetcOut); } finally { interface.Dispose(); } } [NullableContext(0)] public unsafe HRESULT SetData(global::Windows.Win32.System.Com.FORMATETC* pformatetc, global::Windows.Win32.System.Com.STGMEDIUM* pmedium, BOOL fRelease) { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { return interface.Value->SetData(pformatetc, pmedium, fRelease); } finally { interface.Dispose(); } } [NullableContext(2)] private unsafe static bool TryGetDataFromHGLOBAL<T>(HGLOBAL hglobal, [In] [RequiresLocation] ref DataRequest request, [NotNullWhen(true)] out T data) { data = default(T); if ((long)(IntPtr)hglobal == 0) return false; string format = request.Format; object obj; if (format != null) { switch (format.Length) { case 4: switch (format[0]) { case 'T': break; case 'X': goto IL_00ca; default: goto IL_01aa; } if (format == "Text") goto IL_0154; goto IL_01aa; case 11: switch (format[0]) { case 'H': break; case 'U': goto IL_0115; default: goto IL_01aa; } if (format == "HTML Format") goto IL_015e; goto IL_01aa; case 8: break; case 16: if (format == "Rich Text Format") goto IL_0154; goto IL_01aa; case 7: if (format == "OEMText") goto IL_0154; goto IL_01aa; case 9: if (!(format == "FileNameW")) goto IL_01aa; obj = new string[1] { NativeToManagedAdapter.ReadStringFromHGLOBAL(hglobal, true) }; goto IL_01b2; default: goto IL_01aa; IL_00ca: if (format == "Xaml") goto IL_015e; goto IL_01aa; IL_0115: if (!(format == "UnicodeText")) goto IL_01aa; obj = NativeToManagedAdapter.ReadStringFromHGLOBAL(hglobal, true); goto IL_01b2; IL_0154: obj = NativeToManagedAdapter.ReadStringFromHGLOBAL(hglobal, false); goto IL_01b2; IL_015e: obj = NativeToManagedAdapter.ReadUtf8StringFromHGLOBAL(hglobal); goto IL_01b2; } switch (format[4]) { case 'D': break; case 'N': goto IL_0136; default: goto IL_01aa; } if (format == "FileDrop") { obj = NativeToManagedAdapter.ReadFileListFromHDROP((HDROP)(IntPtr)(void*)hglobal); goto IL_01b2; } } goto IL_01aa; IL_01aa: obj = <TryGetDataFromHGLOBAL>g__ReadObjectOrStreamFromHGLOBAL|11_0<T>(hglobal, ref request); goto IL_01b2; IL_0136: if (!(format == "FileName")) goto IL_01aa; obj = new string[1] { NativeToManagedAdapter.ReadStringFromHGLOBAL(hglobal, false) }; goto IL_01b2; IL_01b2: object obj2 = obj; if (obj2 is T) { T val = data = (T)obj2; return true; } return false; } private unsafe static MemoryStream ReadByteStreamFromHGLOBAL(HGLOBAL hglobal, out bool isSerializedObject) { void* ptr = PInvokeCore.GlobalLock(hglobal); if (ptr != null) try { int length = (int)(ulong)PInvokeCore.GlobalSize(hglobal); byte[] array = GC.AllocateUninitializedArray<byte>(length, false); Marshal.Copy((IntPtr)ptr, array, 0, length); int num = 0; if (isSerializedObject = MemoryExtensions.StartsWith<byte>((ReadOnlySpan<byte>)MemoryExtensions.AsSpan<byte>(array), (ReadOnlySpan<byte>)Composition<TOleServices, TNrbfSerializer, TDataFormat>.s_serializedObjectID)) num = Composition<TOleServices, TNrbfSerializer, TDataFormat>.s_serializedObjectID.Length; return new MemoryStream(array, num, array.Length - num); } finally { PInvokeCore.GlobalUnlock(hglobal); } throw new ExternalException(System.Private.Windows.Core.Resources.SR.ExternalException, HRESULT.E_OUTOFMEMORY); } private unsafe static string ReadStringFromHGLOBAL(HGLOBAL hglobal, bool unicode) { string text = null; void* value = PInvokeCore.GlobalLock(hglobal); try { return unicode ? new string((char*)value) : new string((sbyte*)value); } finally { PInvokeCore.GlobalUnlock(hglobal); } } private unsafe static string ReadUtf8StringFromHGLOBAL(HGLOBAL hglobal) { void* bytes = PInvokeCore.GlobalLock(hglobal); try { int num = (int)(ulong)PInvokeCore.GlobalSize(hglobal); return Encoding.UTF8.GetString((byte*)bytes, num - 1); } finally { PInvokeCore.GlobalUnlock(hglobal); } } [return: Nullable(new byte[] { 2, 1 })] private unsafe static string[] ReadFileListFromHDROP(HDROP hdrop) { uint num = PInvokeCore.DragQueryFile(hdrop, uint.MaxValue, null, 0); if (num == 0) return null; Span<char> span = new Span<char>(stackalloc byte[522], 261); string[] array = new string[num]; fixed (char* value = &span.GetPinnableReference()) { for (uint num2 = 0; num2 < num; num2++) { uint num3 = PInvokeCore.DragQueryFile(hdrop, num2, value, (uint)span.Length); if (num3 != 0) { string text = array[num2] = span.Slice(0, (int)num3).ToString(); } } } return array; } [NullableContext(2)] private unsafe static bool TryGetObjectFromDataObject<T>([Nullable(0)] global::Windows.Win32.System.Com.IDataObject* dataObject, [In] [RequiresLocation] ref DataRequest request, out bool doNotContinue, [NotNullWhen(true)] out T data) { data = default(T); doNotContinue = false; bool flag = false; try { string format = request.Format; if (!((IOleServices)).TryGetObjectFromDataObject<T>(dataObject, format, out data)) { flag = TryGetHGLOBALData(dataObject, ref request, out doNotContinue, out data); if (flag) return flag; if (doNotContinue) return flag; flag = TryGetIStreamData(dataObject, ref request, out data); return flag; } return true; } catch (Exception ex) when (!ex.IsCriticalException()) { return flag; } } [NullableContext(2)] private unsafe static bool TryGetHGLOBALData<T>([Nullable(0)] global::Windows.Win32.System.Com.IDataObject* dataObject, [In] [RequiresLocation] ref DataRequest request, out bool doNotContinue, [NotNullWhen(true)] out T data) { data = default(T); doNotContinue = false; global::Windows.Win32.System.Com.FORMATETC fORMATETC = default(global::Windows.Win32.System.Com.FORMATETC); fORMATETC.cfFormat = (ushort)DataFormatsCore<TDataFormat>.GetOrAddFormat(request.Format).Id; fORMATETC.dwAspect = 1; fORMATETC.lindex = -1; fORMATETC.tymed = 1; global::Windows.Win32.System.Com.FORMATETC pformatetc = fORMATETC; if (!dataObject->QueryGetData(ref pformatetc).Failed) { global::Windows.Win32.System.Com.STGMEDIUM pmedium; HRESULT data2 = dataObject->GetData(ref pformatetc, out pmedium); bool result = false; try { if (pmedium.tymed != global::Windows.Win32.System.Com.TYMED.TYMED_HGLOBAL) return result; if (pmedium.hGlobal.IsNull) return result; if (!(data2 != HRESULT.COR_E_SERIALIZATION)) return result; result = TryGetDataFromHGLOBAL(pmedium.hGlobal, ref request, out data); return result; } catch (RestrictedTypeDeserializationException) { result = false; data = default(T); doNotContinue = true; return result; } catch (Exception ex2) when (!request.TypedRequest || !(ex2 is NotSupportedException)) { return result; } finally { PInvokeCore.ReleaseStgMedium(ref pmedium); } } return false; } [NullableContext(2)] private unsafe static bool TryGetIStreamData<T>([Nullable(0)] global::Windows.Win32.System.Com.IDataObject* dataObject, [In] [RequiresLocation] ref DataRequest request, [NotNullWhen(true)] out T data) { data = default(T); global::Windows.Win32.System.Com.FORMATETC fORMATETC = default(global::Windows.Win32.System.Com.FORMATETC); fORMATETC.cfFormat = (ushort)DataFormatsCore<TDataFormat>.GetOrAddFormat(request.Format).Id; fORMATETC.dwAspect = 1; fORMATETC.lindex = -1; fORMATETC.tymed = 4; global::Windows.Win32.System.Com.FORMATETC pformatetc = fORMATETC; HRESULT hRESULT = dataObject->QueryGetData(ref pformatetc); if (!hRESULT.Failed) { hRESULT = dataObject->GetData(ref pformatetc, out global::Windows.Win32.System.Com.STGMEDIUM pmedium); if (!hRESULT.Failed) { HGLOBAL hGLOBAL = default(HGLOBAL); try { if (pmedium.tymed == global::Windows.Win32.System.Com.TYMED.TYMED_ISTREAM && !pmedium.hGlobal.IsNull) { ComScope<global::Windows.Win32.System.Com.IStream> comScope = new ComScope<global::Windows.Win32.System.Com.IStream>((global::Windows.Win32.System.Com.IStream*)(void*)pmedium.hGlobal); try { comScope.Value->Stat(out global::Windows.Win32.System.Com.STATSTG pstatstg, 0); hGLOBAL = PInvokeCore.GlobalAlloc(GLOBAL_ALLOC_FLAGS.GHND, (UIntPtr)(uint)pstatstg.cbSize); if (!hGLOBAL.IsNull) { void* pv = PInvokeCore.GlobalLock(hGLOBAL); comScope.Value->Read(pv, (uint)pstatstg.cbSize, null); PInvokeCore.GlobalUnlock(hGLOBAL); return TryGetDataFromHGLOBAL(hGLOBAL, ref request, out data); } return false; } finally { comScope.Dispose(); } } return false; } finally { if (!hGLOBAL.IsNull) PInvokeCore.GlobalFree(hGLOBAL); PInvokeCore.ReleaseStgMedium(ref pmedium); } } } return false; } private static void ThrowIfFormatAndTypeRequireResolver<[Nullable(2)] T>(string format) { if (!DataFormatNames.IsPredefinedFormat(format) && !((IOleServices)).AllowTypeWithoutResolver<T>() && <ThrowIfFormatAndTypeRequireResolver>g__IsUnboundedType|19_0<T>()) throw new NotSupportedException(string.Format(System.Private.Windows.Core.Resources.SR.ClipboardOrDragDrop_InvalidType, typeof(T).FullName)); } [NullableContext(2)] private unsafe bool TryGetDataInternal<T>([In] [RequiresLocation] ref DataRequest request, [NotNullWhen(true)] out T data) { data = default(T); if (request.TypedRequest && request.Resolver == null) ThrowIfFormatAndTypeRequireResolver<T>(request.Format); ComScope<global::Windows.Win32.System.Com.IDataObject> scope = this._nativeDataObject.GetInterface(); try { bool doNotContinue; bool flag = TryGetObjectFromDataObject((global::Windows.Win32.System.Com.IDataObject*)ref scope, ref request, out doNotContinue, out data); if (!doNotContinue) { if (!flag && request.AutoConvert) { List<string> list = new List<string>(); DataFormatNames.AddMappedFormats<List<string>>(request.Format, list); foreach (string item in list) { if (!request.Format.Equals(item)) { DataRequest dataRequest = default(DataRequest); dataRequest.Format = item; dataRequest.AutoConvert = request.AutoConvert; dataRequest.Resolver = request.Resolver; dataRequest.TypedRequest = request.TypedRequest; DataRequest request2 = dataRequest; flag = TryGetObjectFromDataObject((global::Windows.Win32.System.Com.IDataObject*)ref scope, ref request2, out doNotContinue, out data); if (doNotContinue) break; if (flag) return flag; } } return flag; } return flag; } data = default(T); return false; } finally { scope.Dispose(); } } [return: Nullable(2)] public object GetData(string format, bool autoConvert) { DataRequest dataRequest = default(DataRequest); dataRequest.Format = format; dataRequest.AutoConvert = autoConvert; dataRequest.Resolver = null; dataRequest.TypedRequest = false; DataRequest request = dataRequest; TryGetDataInternal(ref request, out object data); return data; } [return: Nullable(2)] public object GetData(string format) { return GetData(format, true); } [return: Nullable(2)] public object GetData(Type format) { return GetData(ArgumentValidation.OrThrowIfNull<string>(format.FullName, "format.FullName")); } public bool GetDataPresent(Type format) { return GetDataPresent(ArgumentValidation.OrThrowIfNull<string>(format.FullName, "format.FullName")); } public bool GetDataPresent(string format, bool autoConvert) { bool dataPresentInner = GetDataPresentInner(format); if (dataPresentInner || !autoConvert) return dataPresentInner; List<string> list = new List<string>(); DataFormatNames.AddMappedFormats<List<string>>(format, list); foreach (string item in list) { if (!format.Equals(item) && (dataPresentInner = GetDataPresentInner(item))) return dataPresentInner; } return dataPresentInner; } public bool GetDataPresent(string format) { return GetDataPresent(format, true); } public unsafe string[] GetFormats(bool autoConvert) { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { ComScope<global::Windows.Win32.System.Com.IEnumFORMATETC> scope = new ComScope<global::Windows.Win32.System.Com.IEnumFORMATETC>(null); try { interface.Value->EnumFormatEtc(1, (global::Windows.Win32.System.Com.IEnumFORMATETC**)ref scope).AssertSuccess(); if (!scope.IsNull) { HashSet<string> hashSet = new HashSet<string>(); scope.Value->Reset(); global::Windows.Win32.System.Com.FORMATETC fORMATETC = default(global::Windows.Win32.System.Com.FORMATETC); while (scope.Value->Next(1, &fORMATETC, null) == HRESULT.S_OK) { string name = DataFormatsCore<TDataFormat>.GetOrAddFormat(fORMATETC.cfFormat).Name; hashSet.Add(name); if (autoConvert) DataFormatNames.AddMappedFormats<HashSet<string>>(name, hashSet); fORMATETC = default(global::Windows.Win32.System.Com.FORMATETC); } return Enumerable.ToArray<string>((IEnumerable<string>)hashSet); } return Array.Empty<string>(); } finally { scope.Dispose(); } } finally { interface.Dispose(); } } public string[] GetFormats() { return GetFormats(true); } public void SetData(string format, bool autoConvert, [Nullable(2)] object data) { } public void SetData(string format, [Nullable(2)] object data) { } public void SetData(Type format, [Nullable(2)] object data) { } [NullableContext(2)] public void SetData(object data) { } public bool TryGetData<[Nullable(2)] [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(string format, [Nullable(new byte[] { 1, 1, 2 })] Func<TypeName, Type> resolver, bool autoConvert, [NotNullWhen(true)] [MaybeNullWhen(false)] out T data) { DataRequest dataRequest = default(DataRequest); dataRequest.Format = format; dataRequest.AutoConvert = autoConvert; dataRequest.Resolver = resolver; dataRequest.TypedRequest = true; DataRequest request = dataRequest; return TryGetDataInternal(ref request, out data); } public bool TryGetData<[Nullable(2)] [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(string format, bool autoConvert, [NotNullWhen(true)] [MaybeNullWhen(false)] out T data) { DataRequest dataRequest = default(DataRequest); dataRequest.Format = format; dataRequest.AutoConvert = autoConvert; dataRequest.TypedRequest = true; DataRequest request = dataRequest; return TryGetDataInternal(ref request, out data); } public bool TryGetData<[Nullable(2)] [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(string format, [NotNullWhen(true)] [MaybeNullWhen(false)] out T data) { return TryGetData(format, true, out data); } public bool TryGetData<[Nullable(2)] [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>([NotNullWhen(true)] [MaybeNullWhen(false)] out T data) { return TryGetData(ArgumentValidation.OrThrowIfNull<string>(typeof(T).FullName, "typeof(T).FullName"), true, out data); } private unsafe bool GetDataPresentInner(string format) { global::Windows.Win32.System.Com.FORMATETC fORMATETC = default(global::Windows.Win32.System.Com.FORMATETC); fORMATETC.cfFormat = (ushort)DataFormatsCore<TDataFormat>.GetOrAddFormat(format).Id; fORMATETC.dwAspect = 1; fORMATETC.lindex = -1; fORMATETC.tymed = 21; global::Windows.Win32.System.Com.FORMATETC pformatetc = fORMATETC; ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { return interface.Value->QueryGetData(ref pformatetc) == HRESULT.S_OK; } finally { interface.Dispose(); } } } [NullableContext(0)] private sealed class NativeToRuntimeAdapter : System.Runtime.InteropServices.ComTypes.IDataObject { [Nullable(1)] private readonly AgileComPointer<global::Windows.Win32.System.Com.IDataObject> _nativeDataObject; public unsafe NativeToRuntimeAdapter(global::Windows.Win32.System.Com.IDataObject* dataObject) { _nativeDataObject = new AgileComPointer<global::Windows.Win32.System.Com.IDataObject>(dataObject, true, 0); } [NullableContext(1)] public unsafe int DAdvise(ref System.Runtime.InteropServices.ComTypes.FORMATETC pFormatetc, ADVF advf, System.Runtime.InteropServices.ComTypes.IAdviseSink adviseSink, out int connection) { ComScope<global::Windows.Win32.System.Com.IAdviseSink> scope = ComHelpers.TryGetComScope<global::Windows.Win32.System.Com.IAdviseSink>((object)adviseSink); try { fixed (global::Windows.Win32.System.Com.FORMATETC* pformatetc = &Unsafe.As<System.Runtime.InteropServices.ComTypes.FORMATETC, global::Windows.Win32.System.Com.FORMATETC>(ref pFormatetc)) { fixed (int* pdwConnection = &connection) { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { return interface.Value->DAdvise(pformatetc, (uint)advf, (global::Windows.Win32.System.Com.IAdviseSink*)ref scope, (uint*)pdwConnection); } finally { interface.Dispose(); } } } } finally { scope.Dispose(); } } public unsafe void DUnadvise(int connection) { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { interface.Value->DUnadvise((uint)connection).ThrowOnFailure((IntPtr)0); } finally { interface.Dispose(); } } [NullableContext(2)] public unsafe int EnumDAdvise(out System.Runtime.InteropServices.ComTypes.IEnumSTATDATA enumAdvise) { ComScope<global::Windows.Win32.System.Com.IEnumSTATDATA> scope = new ComScope<global::Windows.Win32.System.Com.IEnumSTATDATA>(null); try { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { HRESULT value = interface.Value->EnumDAdvise((global::Windows.Win32.System.Com.IEnumSTATDATA**)ref scope); ComHelpers.TryGetObjectForIUnknown<System.Runtime.InteropServices.ComTypes.IEnumSTATDATA>(scope.AsUnknown, out enumAdvise); return value; } finally { interface.Dispose(); } } finally { scope.Dispose(); } } [NullableContext(1)] public unsafe System.Runtime.InteropServices.ComTypes.IEnumFORMATETC EnumFormatEtc(DATADIR direction) { ComScope<global::Windows.Win32.System.Com.IEnumFORMATETC> scope = new ComScope<global::Windows.Win32.System.Com.IEnumFORMATETC>(null); try { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { if (interface.Value->EnumFormatEtc((uint)direction, (global::Windows.Win32.System.Com.IEnumFORMATETC**)ref scope).Failed) throw new ExternalException(System.Private.Windows.Core.Resources.SR.ExternalException, HRESULT.E_NOTIMPL); return (System.Runtime.InteropServices.ComTypes.IEnumFORMATETC)ComHelpers.GetObjectForIUnknown<global::Windows.Win32.System.Com.IEnumFORMATETC>(scope); } finally { interface.Dispose(); } } finally { scope.Dispose(); } } public unsafe int GetCanonicalFormatEtc(ref System.Runtime.InteropServices.ComTypes.FORMATETC formatIn, out System.Runtime.InteropServices.ComTypes.FORMATETC formatOut) { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { global::Windows.Win32.System.Com.FORMATETC pformatetcOut; HRESULT canonicalFormatEtc = interface.Value->GetCanonicalFormatEtc(ref Unsafe.As<System.Runtime.InteropServices.ComTypes.FORMATETC, global::Windows.Win32.System.Com.FORMATETC>(ref formatIn), out pformatetcOut); formatOut = Unsafe.As<global::Windows.Win32.System.Com.FORMATETC, System.Runtime.InteropServices.ComTypes.FORMATETC>(ref pformatetcOut); return canonicalFormatEtc; } finally { interface.Dispose(); } } public unsafe void GetData(ref System.Runtime.InteropServices.ComTypes.FORMATETC format, out System.Runtime.InteropServices.ComTypes.STGMEDIUM medium) { global::Windows.Win32.System.Com.FORMATETC fORMATETC = Unsafe.As<System.Runtime.InteropServices.ComTypes.FORMATETC, global::Windows.Win32.System.Com.FORMATETC>(ref format); global::Windows.Win32.System.Com.STGMEDIUM stg = default(global::Windows.Win32.System.Com.STGMEDIUM); ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { interface.Value->GetData(&fORMATETC, &stg).ThrowOnFailure((IntPtr)0); medium = (System.Runtime.InteropServices.ComTypes.STGMEDIUM)stg; stg.ReleaseUnknown(); } finally { interface.Dispose(); } } public unsafe void GetDataHere(ref System.Runtime.InteropServices.ComTypes.FORMATETC format, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium) { global::Windows.Win32.System.Com.FORMATETC fORMATETC = Unsafe.As<System.Runtime.InteropServices.ComTypes.FORMATETC, global::Windows.Win32.System.Com.FORMATETC>(ref format); global::Windows.Win32.System.Com.STGMEDIUM stg = (global::Windows.Win32.System.Com.STGMEDIUM)medium; ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { interface.Value->GetDataHere(&fORMATETC, &stg).ThrowOnFailure((IntPtr)0); medium = (System.Runtime.InteropServices.ComTypes.STGMEDIUM)stg; stg.ReleaseUnknown(); } finally { interface.Dispose(); } } public unsafe int QueryGetData(ref System.Runtime.InteropServices.ComTypes.FORMATETC format) { ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { return interface.Value->QueryGetData(ref Unsafe.As<System.Runtime.InteropServices.ComTypes.FORMATETC, global::Windows.Win32.System.Com.FORMATETC>(ref format)); } finally { interface.Dispose(); } } public unsafe void SetData(ref System.Runtime.InteropServices.ComTypes.FORMATETC formatIn, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium, bool release) { global::Windows.Win32.System.Com.STGMEDIUM stg = (global::Windows.Win32.System.Com.STGMEDIUM)medium; global::Windows.Win32.System.Com.FORMATETC fORMATETC = Unsafe.As<System.Runtime.InteropServices.ComTypes.FORMATETC, global::Windows.Win32.System.Com.FORMATETC>(ref formatIn); ComScope<global::Windows.Win32.System.Com.IDataObject> interface = _nativeDataObject.GetInterface(); try { HRESULT hRESULT = interface.Value->SetData(&fORMATETC, &stg, release); medium = (System.Runtime.InteropServices.ComTypes.STGMEDIUM)stg; stg.ReleaseUnknown(); hRESULT.ThrowOnFailure((IntPtr)0); } finally { interface.Dispose(); } } } [NullableContext(0)] private sealed class RuntimeToNativeAdapter : global::Windows.Win32.System.Com.IDataObject.Interface, System.Runtime.InteropServices.ComTypes.IDataObject, IManagedWrapper<global::Windows.Win32.System.Com.IDataObject>, IManagedWrapper { [Nullable(1)] private readonly System.Runtime.InteropServices.ComTypes.IDataObject _runtimeDataObject; [NullableContext(1)] public RuntimeToNativeAdapter(System.Runtime.InteropServices.ComTypes.IDataObject dataObject) { _runtimeDataObject = dataObject; } [NullableContext(1)] public int DAdvise(ref System.Runtime.InteropServices.ComTypes.FORMATETC pFormatetc, ADVF advf, System.Runtime.InteropServices.ComTypes.IAdviseSink adviseSink, out int connection) { return _runtimeDataObject.DAdvise(ref pFormatetc, advf, adviseSink, out connection); } public void DUnadvise(int connection) { _runtimeDataObject.DUnadvise(connection); } [NullableContext(2)] public int EnumDAdvise(out System.Runtime.InteropServices.ComTypes.IEnumSTATDATA enumAdvise) { return _runtimeDataObject.EnumDAdvise(out enumAdvise); } [NullableContext(1)] public System.Runtime.InteropServices.ComTypes.IEnumFORMATETC EnumFormatEtc(DATADIR direction) { return _runtimeDataObject.EnumFormatEtc(direction); } public int GetCanonicalFormatEtc(ref System.Runtime.InteropServices.ComTypes.FORMATETC formatIn, out System.Runtime.InteropServices.ComTypes.FORMATETC formatOut) { return _runtimeDataObject.GetCanonicalFormatEtc(ref formatIn, out formatOut); } public void GetData(ref System.Runtime.InteropServices.ComTypes.FORMATETC format, out System.Runtime.InteropServices.ComTypes.STGMEDIUM medium) { _runtimeDataObject.GetData(ref format, out medium); } public void GetDataHere(ref System.Runtime.InteropServices.ComTypes.FORMATETC format, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium) { _runtimeDataObject.GetDataHere(ref format, ref medium); } public int QueryGetData(ref System.Runtime.InteropServices.ComTypes.FORMATETC format) { return _runtimeDataObject.QueryGetData(ref format); } public void SetData(ref System.Runtime.InteropServices.ComTypes.FORMATETC formatIn, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium, bool release) { _runtimeDataObject.SetData(ref formatIn, ref medium, release); } unsafe HRESULT global::Windows.Win32.System.Com.IDataObject.Interface.DAdvise(global::Windows.Win32.System.Com.FORMATETC* pformatetc, uint advf, global::Windows.Win32.System.Com.IAdviseSink* pAdvSink, uint* pdwConnection) { System.Runtime.InteropServices.ComTypes.IAdviseSink adviseSink = (System.Runtime.InteropServices.ComTypes.IAdviseSink)ComHelpers.GetObjectForIUnknown<global::Windows.Win32.System.Com.IAdviseSink>(pAdvSink); return (HRESULT)DAdvise(ref *(System.Runtime.InteropServices.ComTypes.FORMATETC*)pformatetc, (ADVF)advf, adviseSink, out *(int*)pdwConnection); } HRESULT global::Windows.Win32.System.Com.IDataObject.Interface.DUnadvise(uint dwConnection) { try { DUnadvise((int)dwConnection); } catch (Exception ex) { return (HRESULT)ex.HResult; } return HRESULT.S_OK; } unsafe HRESULT global::Windows.Win32.System.Com.IDataObject.Interface.EnumDAdvise(global::Windows.Win32.System.Com.IEnumSTATDATA** ppenumAdvise) { if (ppenumAdvise == null) return HRESULT.E_POINTER; *ppenumAdvise = null; System.Runtime.InteropServices.ComTypes.IEnumSTATDATA enumAdvise; HRESULT result = (HRESULT)EnumDAdvise(out enumAdvise); if (result.Failed) return result; *ppenumAdvise = ComHelpers.TryGetComPointer<global::Windows.Win32.System.Com.IEnumSTATDATA>((object)enumAdvise, out result); if (!result.Succeeded) return HRESULT.E_NOINTERFACE; return result; } unsafe HRESULT global::Windows.Win32.System.Com.IDataObject.Interface.EnumFormatEtc(uint dwDirection, global::Windows.Win32.System.Com.IEnumFORMATETC** ppenumFormatEtc) { if (ppenumFormatEtc == null) return HRESULT.E_POINTER; System.Runtime.InteropServices.ComTypes.IEnumFORMATETC object = EnumFormatEtc((DATADIR)dwDirection); *ppenumFormatEtc = ComHelpers.TryGetComPointer<global::Windows.Win32.System.Com.IEnumFORMATETC>((object)object, out HRESULT result); if (!result.Succeeded) return HRESULT.E_NOINTERFACE; return HRESULT.S_OK; } unsafe HRESULT global::Windows.Win32.System.Com.IDataObject.Interface.GetCanonicalFormatEtc(global::Windows.Win32.System.Com.FORMATETC* pformatectIn, global::Windows.Win32.System.Com.FORMATETC* pformatetcOut) { return (HRESULT)GetCanonicalFormatEtc(ref *(System.Runtime.InteropServices.ComTypes.FORMATETC*)pformatectIn, out *(System.Runtime.InteropServices.ComTypes.FORMATETC*)pformatetcOut); } unsafe HRESULT global::Windows.Win32.System.Com.IDataObject.Interface.GetData(global::Windows.Win32.System.Com.FORMATETC* pformatetcIn, global::Windows.Win32.System.Com.STGMEDIUM* pmedium) { if (pmedium != null) try { GetData(ref *(System.Runtime.InteropServices.ComTypes.FORMATETC*)pformatetcIn, out System.Runtime.InteropServices.ComTypes.STGMEDIUM medium); *pmedium = (global::Windows.Win32.System.Com.STGMEDIUM)medium; return HRESULT.S_OK; } catch (Exception ex) { return (HRESULT)ex.HResult; } return HRESULT.E_POINTER; } unsafe HRESULT global::Windows.Win32.System.Com.IDataObject.Interface.GetDataHere(global::Windows.Win32.System.Com.FORMATETC* pformatetc, global::Windows.Win32.System.Com.STGMEDIUM* pmedium) { if (pmedium == null) return HRESULT.E_POINTER; System.Runtime.InteropServices.ComTypes.STGMEDIUM medium = (System.Runtime.InteropServices.ComTypes.STGMEDIUM)(*pmedium); try { GetDataHere(ref *(System.Runtime.InteropServices.ComTypes.FORMATETC*)pformatetc, ref medium); } catch (Exception ex) { return (HRESULT)ex.HResult; } *pmedium = (global::Windows.Win32.System.Com.STGMEDIUM)medium; return HRESULT.S_OK; } unsafe HRESULT global::Windows.Win32.System.Com.IDataObject.Interface.QueryGetData(global::Windows.Win32.System.Com.FORMATETC* pformatetc) { return (HRESULT)QueryGetData(ref *(System.Runtime.InteropServices.ComTypes.FORMATETC*)pformatetc); } unsafe HRESULT global::Windows.Win32.System.Com.IDataObject.Interface.SetData(global::Windows.Win32.System.Com.FORMATETC* pformatetc, global::Windows.Win32.System.Com.STGMEDIUM* pmedium, BOOL fRelease) { if (pmedium == null) return HRESULT.E_POINTER; System.Runtime.InteropServices.ComTypes.STGMEDIUM medium = (System.Runtime.InteropServices.ComTypes.STGMEDIUM)(*pmedium); try { SetData(ref *(System.Runtime.InteropServices.ComTypes.FORMATETC*)pformatetc, ref medium, fRelease); } catch (Exception ex) { return (HRESULT)ex.HResult; } return HRESULT.S_OK; } } private const global::Windows.Win32.System.Com.TYMED AllowedTymeds = (global::Windows.Win32.System.Com.TYMED)21; private static readonly byte[] s_serializedObjectID = new byte[16] { 150, 167, 158, 253, 19, 59, 112, 67, 166, 121, 86, 16, 107, 178, 136, 251 }; private readonly global::Windows.Win32.System.Com.IDataObject.Interface _nativeDataObject; private readonly System.Runtime.InteropServices.ComTypes.IDataObject _runtimeDataObject; internal IDataObjectInternal ManagedDataObject { get; } private Composition(IDataObjectInternal managedDataObject, global::Windows.Win32.System.Com.IDataObject.Interface nativeDataObject, System.Runtime.InteropServices.ComTypes.IDataObject runtimeDataObject) { ManagedDataObject = managedDataObject; _nativeDataObject = nativeDataObject; _runtimeDataObject = runtimeDataObject; } internal static Composition<TOleServices, TNrbfSerializer, TDataFormat> Create() { return Create(new DataStore<TOleServices>()); } internal static Composition<TOleServices, TNrbfSerializer, TDataFormat> Create<TDataObject, TIDataObject>(object data) where TDataObject : class, IDataObjectInternal<TDataObject, TIDataObject>, TIDataObject where TIDataObject : class { IDataObjectInternal dataObjectInternal = data as IDataObjectInternal; if (dataObjectInternal != null) return Composition<TOleServices, TNrbfSerializer, TDataFormat>.Create(dataObjectInternal); TIDataObject val = data as TIDataObject; if (val != null) return Composition<TOleServices, TNrbfSerializer, TDataFormat>.Create(((IDataObjectInternal<TDataObject, TIDataObject>)).Wrap(val)); System.Runtime.InteropServices.ComTypes.IDataObject dataObject = data as System.Runtime.InteropServices.ComTypes.IDataObject; if (dataObject != null) return Composition<TOleServices, TNrbfSerializer, TDataFormat>.Create(dataObject); Composition<TOleServices, TNrbfSerializer, TDataFormat> composition = Composition<TOleServices, TNrbfSerializer, TDataFormat>.Create((IDataObjectInternal)new DataStore<TOleServices>()); composition.SetData(data); return composition; } internal unsafe static Composition<TOleServices, TNrbfSerializer, TDataFormat> Create(IDataObjectInternal data) { ManagedToNativeAdapter managedToNativeAdapter = new ManagedToNativeAdapter(data); NativeToRuntimeAdapter runtimeDataObject = new NativeToRuntimeAdapter(ComHelpers.GetComPointer<global::Windows.Win32.System.Com.IDataObject>((object)managedToNativeAdapter)); return new Composition<TOleServices, TNrbfSerializer, TDataFormat>(data, managedToNativeAdapter, runtimeDataObject); } [NullableContext(0)] [return: Nullable(1)] internal unsafe static Composition<TOleServices, TNrbfSerializer, TDataFormat> Create(global::Windows.Win32.System.Com.IDataObject* nativeDataObject) { nativeDataObject->AddRef(); nativeDataObject->AddRef(); NativeToManagedAdapter nativeToManagedAdapter = new NativeToManagedAdapter(nativeDataObject); NativeToRuntimeAdapter runtimeDataObject = new NativeToRuntimeAdapter(nativeDataObject); return new Composition<TOleServices, TNrbfSerializer, TDataFormat>(nativeToManagedAdapter, nativeToManagedAdapter, runtimeDataObject); } internal unsafe static Composition<TOleServices, TNrbfSerializer, TDataFormat> Create(System.Runtime.InteropServices.ComTypes.IDataObject runtimeDataObject) { RuntimeToNativeAdapter runtimeToNativeAdapter = new RuntimeToNativeAdapter(runtimeDataObject); return new Composition<TOleServices, TNrbfSerializer, TDataFormat>(new NativeToManagedAdapter(ComHelpers.GetComPointer<global::Windows.Win32.System.Com.IDataObject>((object)runtimeToNativeAdapter)), runtimeToNativeAdapter, runtimeDataObject); } [RequiresUnreferencedCode("Uses default System.Text.Json behavior which is not trim-compatible.")] public void SetDataAsJson<[Nullable(2)] T, [Nullable(0)] TDataObject>(T data, [Nullable(2)] string format = null) where TDataObject : IComVisibleDataObject { if (format == null) format = ArgumentValidation.OrThrowIfNull<string>(typeof(T).FullName, "typeof(T).FullName"); this.SetData(format, false, (object)DataObjectCore<TDataObject>.TryJsonSerialize(format, data)); } [return: Nullable(2)] public object GetData(string format, bool autoConvert) { object data = ManagedDataObject.GetData(format, autoConvert); IJsonData jsonData = data as IJsonData; if (jsonData == null) return data; return jsonData.Deserialize(); } [return: Nullable(2)] public object GetData(string format) { return ManagedDataObject.GetData(format); } [return: Nullable(2)] public object GetData(Type format) { return ManagedDataObject.GetData(format); } public bool GetDataPresent(string format, bool autoConvert) { return ManagedDataObject.GetDataPresent(format, autoConvert); } public bool GetDataPresent(string format) { return ManagedDataObject.GetDataPresent(format); } public bool GetDataPresent(Type format) { return ManagedDataObject.GetDataPresent(format); } public string[] GetFormats(bool autoConvert) { return ManagedDataObject.GetFormats(autoConvert); } public string[] GetFormats() { return ManagedDataObject.GetFormats(); } public void SetData(string format, bool autoConvert, [Nullable(2)] object data) { ManagedDataObject.SetData(format, autoConvert, data); } public void SetData(string format, [Nullable(2)] object data) { ManagedDataObject.SetData(format, data); } public void SetData(Type format, [Nullable(2)] object data) { ManagedDataObject.SetData(format, data); } [NullableContext(2)] public void SetData(object data) { ManagedDataObject.SetData(data); } public bool TryGetData<[Nullable(2)] [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(string format, [Nullable(new byte[] { 1, 1, 2 })] Func<TypeName, Type> resolver, bool autoConvert, [NotNullWhen(true)] [MaybeNullWhen(false)] out T data) { return this.ManagedDataObject.TryGetData<T>(format, resolver, autoConvert, out data); } public bool TryGetData<[Nullable(2)] [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(string format, bool autoConvert, [NotNullWhen(true)] [MaybeNullWhen(false)] out T data) { return this.ManagedDataObject.TryGetData<T>(format, autoConvert, out data); } public bool TryGetData<[Nullable(2)] [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>(string format, [NotNullWhen(true)] [MaybeNullWhen(false)] out T data) { return this.ManagedDataObject.TryGetData<T>(format, out data); } public bool TryGetData<[Nullable(2)] [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T>([NotNullWhen(true)] [MaybeNullWhen(false)] out T data) { return this.ManagedDataObject.TryGetData<T>(ArgumentValidation.OrThrowIfNull<string>(typeof(T).FullName, "typeof(T).FullName"), out data); } [NullableContext(0)] public unsafe HRESULT DAdvise(global::Windows.Win32.System.Com.FORMATETC* pformatetc, uint advf, global::Windows.Win32.System.Com.IAdviseSink* pAdvSink, uint* pdwConnection) { return _nativeDataObject.DAdvise(pformatetc, advf, pAdvSink, pdwConnection); } public HRESULT DUnadvise(uint dwConnection) { return _nativeDataObject.DUnadvise(dwConnection); } [NullableContext(0)] public unsafe HRESULT EnumDAdvise(global::Windows.Win32.System.Com.IEnumSTATDATA** ppenumAdvise) { return _nativeDataObject.EnumDAdvise(ppenumAdvise); } [NullableContext(0)] public unsafe HRESULT EnumFormatEtc(uint dwDirection, global::Windows.Win32.System.Com.IEnumFORMATETC** ppenumFormatEtc) { return _nativeDataObject.EnumFormatEtc(dwDirection, ppenumFormatEtc); } [NullableContext(0)] public unsafe HRESULT GetCanonicalFormatEtc(global::Windows.Win32.System.Com.FORMATETC* pformatectIn, global::Windows.Win32.System.Com.FORMATETC* pformatetcOut) { return _nativeDataObject.GetCanonicalFormatEtc(pformatectIn, pformatetcOut); } [NullableContext(0)] public unsafe HRESULT GetData(global::Windows.Win32.System.Com.FORMATETC* pformatetcIn, global::Windows.Win32.System.Com.STGMEDIUM* pmedium) { return _nativeDataObject.GetData(pformatetcIn, pmedium); } [NullableContext(0)] public unsafe HRESULT GetDataHere(global::Windows.Win32.System.Com.FORMATETC* pformatetc, global::Windows.Win32.System.Com.STGMEDIUM* pmedium) { return _nativeDataObject.GetDataHere(pformatetc, pmedium); } [NullableContext(0)] public unsafe HRESULT QueryGetData(global::Windows.Win32.System.Com.FORMATETC* pformatetc) { return _nativeDataObject.QueryGetData(pformatetc); } [NullableContext(0)] public unsafe HRESULT SetData(global::Windows.Win32.System.Com.FORMATETC* pformatetc, global::Windows.Win32.System.Com.STGMEDIUM* pmedium, BOOL fRelease) { return _nativeDataObject.SetData(pformatetc, pmedium, fRelease); } public int DAdvise(ref System.Runtime.InteropServices.ComTypes.FORMATETC pFormatetc, ADVF advf, System.Runtime.InteropServices.ComTypes.IAdviseSink adviseSink, out int connection) { return _runtimeDataObject.DAdvise(ref pFormatetc, advf, adviseSink, out connection); } public void DUnadvise(int connection) { _runtimeDataObject.DUnadvise(connection); } [NullableContext(2)] public int EnumDAdvise(out System.Runtime.InteropServices.ComTypes.IEnumSTATDATA enumAdvise) { return _runtimeDataObject.EnumDAdvise(out enumAdvise); } public System.Runtime.InteropServices.ComTypes.IEnumFORMATETC EnumFormatEtc(DATADIR direction) { return _runtimeDataObject.EnumFormatEtc(direction); } public int GetCanonicalFormatEtc(ref System.Runtime.InteropServices.ComTypes.FORMATETC formatIn, out System.Runtime.InteropServices.ComTypes.FORMATETC formatOut) { return _runtimeDataObject.GetCanonicalFormatEtc(ref formatIn, out formatOut); } public void GetData(ref System.Runtime.InteropServices.ComTypes.FORMATETC format, out System.Runtime.InteropServices.ComTypes.STGMEDIUM medium) { _runtimeDataObject.GetData(ref format, out medium); } public void GetDataHere(ref System.Runtime.InteropServices.ComTypes.FORMATETC format, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium) { _runtimeDataObject.GetDataHere(ref format, ref medium); } public int QueryGetData(ref System.Runtime.InteropServices.ComTypes.FORMATETC format) { return _runtimeDataObject.QueryGetData(ref format); } public void SetData(ref System.Runtime.InteropServices.ComTypes.FORMATETC formatIn, ref System.Runtime.InteropServices.ComTypes.STGMEDIUM medium, bool release) { _runtimeDataObject.SetData(ref formatIn, ref medium, release); } } }