BlindAggregatorFactory.cs 7.5 KB
Newer Older
1 2 3 4 5 6
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Runtime.InteropServices;
using System.Threading;

7
namespace Microsoft.CodeAnalysis.Test.Utilities
8 9 10 11
{
    /// <summary>
    /// This factory creates COM "blind aggregator" instances in managed code.
    /// </summary>
12
    public static class BlindAggregatorFactory
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    {
        public static unsafe IntPtr CreateWrapper()
        {
            return (IntPtr)BlindAggregator.CreateInstance();
        }

        public static unsafe void SetInnerObject(IntPtr wrapperUnknown, IntPtr innerUnknown, IntPtr managedObjectGCHandlePtr)
        {
            BlindAggregator* pWrapper = (BlindAggregator*)wrapperUnknown;
            pWrapper->SetInnerObject(innerUnknown, managedObjectGCHandlePtr);
        }

        /// <summary>
        /// A blind aggregator instance. It is allocated in native memory.
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        private struct BlindAggregator
        {
            private IntPtr _vfPtr;           // Pointer to the virtual function table
            private int _refCount;           // COM reference count
            private IntPtr _innerUnknown;    // CCW for the managed object supporting aggregation
            private IntPtr _gcHandle;        // The GC Handle to the managed object (the non aggregated object)

            public static unsafe BlindAggregator* CreateInstance()
            {
                BlindAggregator* pResult = (BlindAggregator*)Marshal.AllocCoTaskMem(sizeof(BlindAggregator));
                if (pResult != null)
                {
                    pResult->Construct();
                }

                return pResult;
            }

            private void Construct()
            {
                _vfPtr = VTable.AddressOfVTable;
                _refCount = 1;
                _innerUnknown = IntPtr.Zero;
                _gcHandle = IntPtr.Zero;
            }

            public void SetInnerObject(IntPtr innerUnknown, IntPtr gcHandle)
            {
                _innerUnknown = innerUnknown;
                Marshal.AddRef(_innerUnknown);
                _gcHandle = gcHandle;
            }

            private void FinalRelease()
            {
                Marshal.Release(_innerUnknown);

                if (_gcHandle != IntPtr.Zero)
                {
                    GCHandle.FromIntPtr(_gcHandle).Free();
                    _gcHandle = IntPtr.Zero;
                }
            }

            private unsafe delegate int QueryInterfaceDelegateType(BlindAggregator* pThis, [In] ref Guid riid, out IntPtr pvObject);
            private unsafe delegate uint AddRefDelegateType(BlindAggregator* pThis);
            private unsafe delegate uint ReleaseDelegateType(BlindAggregator* pThis);
            private unsafe delegate int GetGCHandlePtrDelegateType(BlindAggregator* pThis, out IntPtr pResult);

            [StructLayout(LayoutKind.Sequential)]
            private struct VTable
            {
                // Need these to keep the delegates alive
                private static unsafe readonly QueryInterfaceDelegateType s_queryInterface = BlindAggregator.QueryInterface;
                private static unsafe readonly AddRefDelegateType s_addRef = BlindAggregator.AddRef;
                private static unsafe readonly ReleaseDelegateType s_release = BlindAggregator.Release;
                private static unsafe readonly GetGCHandlePtrDelegateType s_get_GCHandlePtr = BlindAggregator.GetGCHandlePtr;

                private IntPtr _queryInterfacePtr;
                private IntPtr _addRefPtr;
                private IntPtr _releasePtr;
                private IntPtr _getGCHandlePtr;

                private void Construct()
                {
                    _queryInterfacePtr = Marshal.GetFunctionPointerForDelegate(VTable.s_queryInterface);
                    _addRefPtr = Marshal.GetFunctionPointerForDelegate(VTable.s_addRef);
                    _releasePtr = Marshal.GetFunctionPointerForDelegate(VTable.s_release);
                    _getGCHandlePtr = Marshal.GetFunctionPointerForDelegate(VTable.s_get_GCHandlePtr);
                }

                /// <summary>
                /// A 'holder' for a native memory allocation. The allocation is freed in the finalizer.
                /// </summary>
                private class CoTaskMemPtr
                {
                    public readonly IntPtr VTablePtr;

                    public unsafe CoTaskMemPtr()
                    {
                        var ptr = Marshal.AllocCoTaskMem(sizeof(VTable));
                        this.VTablePtr = ptr;
                        ((VTable*)ptr)->Construct();
                    }

                    ~CoTaskMemPtr()
                    {
                        Marshal.FreeCoTaskMem(this.VTablePtr);
                    }
                }

                // Singleton instance of the VTable allocated in native memory. Since it's static, the
                // underlying native memory will be freed when finalizers run at shutdown.
                private static readonly CoTaskMemPtr s_instance = new CoTaskMemPtr();

                public static IntPtr AddressOfVTable { get { return s_instance.VTablePtr; } }
            }

            private const int S_OK = 0;
            private const int E_NOINTERFACE = unchecked((int)0x80004002);

            // 00000000-0000-0000-C000-000000000046
            private static readonly Guid s_IUnknownInterfaceGuid = new Guid(0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);

            // 00000003-0000-0000-C000-000000000046
            private static readonly Guid s_IMarshalInterfaceGuid = new Guid(0x00000003, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);

136 137 138
            // CBD71F2C-6BC5-4932-B851-B93EB3151386
            private static readonly Guid s_IComWrapperGuid = new Guid("CBD71F2C-6BC5-4932-B851-B93EB3151386");

139 140
            private static unsafe int QueryInterface(BlindAggregator* pThis, [In] ref Guid riid, out IntPtr pvObject)
            {
141
                if (riid == s_IUnknownInterfaceGuid || riid == s_IComWrapperGuid)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
                {
                    AddRef(pThis);
                    pvObject = (IntPtr)pThis;
                    return S_OK;
                }
                else if (riid == s_IMarshalInterfaceGuid)
                {
                    pvObject = IntPtr.Zero;
                    return E_NOINTERFACE;
                }
                else
                {
                    // We don't know what the interface is, so aggregate blindly from here
                    return Marshal.QueryInterface(pThis->_innerUnknown, ref riid, out pvObject);
                }
            }

            private static unsafe uint AddRef(BlindAggregator* pThis)
            {
                return unchecked((uint)Interlocked.Increment(ref pThis->_refCount));
            }

            private static unsafe uint Release(BlindAggregator* pThis)
            {
                uint result = unchecked((uint)Interlocked.Decrement(ref pThis->_refCount));
                if (result == 0u)
                {
                    pThis->FinalRelease();
                    Marshal.FreeCoTaskMem((IntPtr)pThis);
                }

                return result;
            }

            private static unsafe int GetGCHandlePtr(BlindAggregator* pThis, out IntPtr pResult)
            {
                pResult = pThis->_gcHandle;
                return S_OK;
            }
        }
    }
}
184