|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Runtime.CompilerServices; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using Windows.Win32.Foundation; |
| 7 | +using Windows.Win32.System.Com; |
| 8 | +using Lifetime = Windows.Win32.System.Com.Lifetime<Windows.Win32.System.Com.IDataObject.Vtbl, System.Private.Windows.Ole.DataObjectProxy>; |
| 9 | + |
| 10 | +namespace System.Private.Windows.Ole; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Emulates an OLE proxy for unit testing purposes. |
| 14 | +/// </summary> |
| 15 | +internal unsafe class DataObjectProxy : IDataObject.Interface, IDisposable |
| 16 | +{ |
| 17 | + // Agile for ensured cleanup |
| 18 | + private readonly AgileComPointer<IDataObject> _agileOriginal; |
| 19 | + private readonly IDataObject* _original; |
| 20 | + private readonly AgileComPointer<IDataObject> _agileProxy; |
| 21 | + public IDataObject* Proxy { get; } |
| 22 | + |
| 23 | + public DataObjectProxy(IDataObject* original) |
| 24 | + { |
| 25 | + // Don't track disposal, we depend on finalization for testing. |
| 26 | + |
| 27 | + _original = original; |
| 28 | + _agileOriginal = new( |
| 29 | +#if DEBUG |
| 30 | + original, takeOwnership: true, trackDisposal: false |
| 31 | +#else |
| 32 | + original, takeOwnership: true |
| 33 | +#endif |
| 34 | + ); |
| 35 | + |
| 36 | + Proxy = CCW.Create(this); |
| 37 | + |
| 38 | + _agileProxy = new( |
| 39 | +#if DEBUG |
| 40 | + Proxy, takeOwnership: true, trackDisposal: false |
| 41 | +#else |
| 42 | + Proxy, takeOwnership: true |
| 43 | +#endif |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + public HRESULT GetData(FORMATETC* pformatetcIn, STGMEDIUM* pmedium) => _original->GetData(pformatetcIn, pmedium); |
| 48 | + public HRESULT GetDataHere(FORMATETC* pformatetc, STGMEDIUM* pmedium) => _original->GetDataHere(pformatetc, pmedium); |
| 49 | + public HRESULT QueryGetData(FORMATETC* pformatetc) => _original->QueryGetData(pformatetc); |
| 50 | + public HRESULT GetCanonicalFormatEtc(FORMATETC* pformatectIn, FORMATETC* pformatetcOut) => _original->GetCanonicalFormatEtc(pformatectIn, pformatetcOut); |
| 51 | + public HRESULT SetData(FORMATETC* pformatetc, STGMEDIUM* pmedium, BOOL fRelease) => _original->SetData(pformatetc, pmedium, fRelease); |
| 52 | + public HRESULT EnumFormatEtc(uint dwDirection, IEnumFORMATETC** ppenumFormatEtc) => _original->EnumFormatEtc(dwDirection, ppenumFormatEtc); |
| 53 | + public HRESULT DAdvise(FORMATETC* pformatetc, uint advf, IAdviseSink* pAdvSink, uint* pdwConnection) => _original->DAdvise(pformatetc, advf, pAdvSink, pdwConnection); |
| 54 | + public HRESULT DUnadvise(uint dwConnection) => _original->DUnadvise(dwConnection); |
| 55 | + public HRESULT EnumDAdvise(IEnumSTATDATA** ppenumAdvise) => _original->EnumDAdvise(ppenumAdvise); |
| 56 | + |
| 57 | + public void Dispose() |
| 58 | + { |
| 59 | + _agileOriginal.Dispose(); |
| 60 | + _agileProxy.Dispose(); |
| 61 | + } |
| 62 | + |
| 63 | + internal static class CCW |
| 64 | + { |
| 65 | + private static readonly IDataObject.Vtbl* s_vtable = AllocateVTable(); |
| 66 | + |
| 67 | + private static unsafe IDataObject.Vtbl* AllocateVTable() |
| 68 | + { |
| 69 | + // Allocate and create a singular VTable for this type projection. |
| 70 | + IDataObject.Vtbl* vtable = (IDataObject.Vtbl*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(CCW), sizeof(IDataObject.Vtbl)); |
| 71 | + |
| 72 | + // IUnknown |
| 73 | + vtable->QueryInterface_1 = &QueryInterface; |
| 74 | + vtable->AddRef_2 = &AddRef; |
| 75 | + vtable->Release_3 = &Release; |
| 76 | + vtable->GetData_4 = &GetData; |
| 77 | + vtable->GetDataHere_5 = &GetDataHere; |
| 78 | + vtable->QueryGetData_6 = &QueryGetData; |
| 79 | + vtable->GetCanonicalFormatEtc_7 = &GetCanonicalFormatEtc; |
| 80 | + vtable->SetData_8 = &SetData; |
| 81 | + vtable->EnumFormatEtc_9 = &EnumFormatEtc; |
| 82 | + vtable->DAdvise_10 = &DAdvise; |
| 83 | + vtable->DUnadvise_11 = &DUnadvise; |
| 84 | + vtable->EnumDAdvise_12 = &EnumDAdvise; |
| 85 | + return vtable; |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Creates a manual COM Callable Wrapper for the given <paramref name="object"/>. |
| 90 | + /// </summary> |
| 91 | + public static unsafe IDataObject* Create(DataObjectProxy @object) => |
| 92 | + (IDataObject*)Lifetime.Allocate(@object, s_vtable); |
| 93 | + |
| 94 | + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] |
| 95 | + private static unsafe HRESULT QueryInterface(IDataObject* @this, Guid* iid, void** ppObject) |
| 96 | + { |
| 97 | + if (iid is null || ppObject is null) |
| 98 | + { |
| 99 | + return HRESULT.E_POINTER; |
| 100 | + } |
| 101 | + |
| 102 | + if (iid->Equals(IDataObject.IID_Guid) || iid->Equals(IUnknown.IID_Guid)) |
| 103 | + { |
| 104 | + *ppObject = @this; |
| 105 | + Lifetime.AddRef(@this); |
| 106 | + return HRESULT.S_OK; |
| 107 | + } |
| 108 | + |
| 109 | + *ppObject = null; |
| 110 | + DataObjectProxy? proxy = Lifetime.GetObject(@this); |
| 111 | + if (proxy is null) |
| 112 | + { |
| 113 | + return HRESULT.E_NOINTERFACE; |
| 114 | + } |
| 115 | + |
| 116 | + // Unwrap our "proxy" object by calling the the original object. This should roughly match the |
| 117 | + // OLE proxy behavior which returns it's own pointer for the IID_IUnknown and IID_IDataObject interfaces. |
| 118 | + return proxy._original->QueryInterface(iid, ppObject); |
| 119 | + } |
| 120 | + |
| 121 | + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] |
| 122 | + private static unsafe uint AddRef(IDataObject* @this) => Lifetime.AddRef(@this); |
| 123 | + |
| 124 | + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] |
| 125 | + private static unsafe uint Release(IDataObject* @this) => Lifetime.Release(@this); |
| 126 | + |
| 127 | + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] |
| 128 | + private static unsafe HRESULT GetData(IDataObject* @this, FORMATETC* pFormatetc, STGMEDIUM* pMedium) => |
| 129 | + Lifetime.GetObject(@this)?.GetData(pFormatetc, pMedium) ?? HRESULT.COR_E_OBJECTDISPOSED; |
| 130 | + |
| 131 | + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] |
| 132 | + private static unsafe HRESULT GetDataHere(IDataObject* @this, FORMATETC* pFormatetc, STGMEDIUM* pMedium) => |
| 133 | + Lifetime.GetObject(@this)?.GetDataHere(pFormatetc, pMedium) ?? HRESULT.COR_E_OBJECTDISPOSED; |
| 134 | + |
| 135 | + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] |
| 136 | + private static unsafe HRESULT QueryGetData(IDataObject* @this, FORMATETC* pFormatetc) => |
| 137 | + Lifetime.GetObject(@this)?.QueryGetData(pFormatetc) ?? HRESULT.COR_E_OBJECTDISPOSED; |
| 138 | + |
| 139 | + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] |
| 140 | + private static unsafe HRESULT GetCanonicalFormatEtc(IDataObject* @this, FORMATETC* pFormatetcIn, FORMATETC* pFormatetcOut) => |
| 141 | + Lifetime.GetObject(@this)?.GetCanonicalFormatEtc(pFormatetcIn, pFormatetcOut) ?? HRESULT.COR_E_OBJECTDISPOSED; |
| 142 | + |
| 143 | + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] |
| 144 | + private static unsafe HRESULT SetData(IDataObject* @this, FORMATETC* pFormatetc, STGMEDIUM* pMedium, BOOL fRelease) => |
| 145 | + Lifetime.GetObject(@this)?.SetData(pFormatetc, pMedium, fRelease) ?? HRESULT.COR_E_OBJECTDISPOSED; |
| 146 | + |
| 147 | + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] |
| 148 | + private static unsafe HRESULT EnumFormatEtc(IDataObject* @this, uint dwDirection, IEnumFORMATETC** ppEnumFormatEtc) => |
| 149 | + Lifetime.GetObject(@this)?.EnumFormatEtc(dwDirection, ppEnumFormatEtc) ?? HRESULT.COR_E_OBJECTDISPOSED; |
| 150 | + |
| 151 | + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] |
| 152 | + private static unsafe HRESULT DAdvise(IDataObject* @this, FORMATETC* pFormatetc, uint advf, IAdviseSink* pAdvSink, uint* pdwConnection) => |
| 153 | + Lifetime.GetObject(@this)?.DAdvise(pFormatetc, advf, pAdvSink, pdwConnection) ?? HRESULT.COR_E_OBJECTDISPOSED; |
| 154 | + |
| 155 | + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] |
| 156 | + private static unsafe HRESULT DUnadvise(IDataObject* @this, uint dwConnection) => |
| 157 | + Lifetime.GetObject(@this)?.DUnadvise(dwConnection) ?? HRESULT.COR_E_OBJECTDISPOSED; |
| 158 | + |
| 159 | + [UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])] |
| 160 | + private static unsafe HRESULT EnumDAdvise(IDataObject* @this, IEnumSTATDATA** ppEnumAdvise) => |
| 161 | + Lifetime.GetObject(@this)?.EnumDAdvise(ppEnumAdvise) ?? HRESULT.COR_E_OBJECTDISPOSED; |
| 162 | + } |
| 163 | +} |
0 commit comments