| 
 | 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;  | 
 | 5 | +using System.Collections;  | 
 | 6 | +using System.Runtime.CompilerServices;  | 
 | 7 | +using System.Runtime.InteropServices;  | 
 | 8 | +using TestLibrary;  | 
 | 9 | +using static VariantNative;  | 
 | 10 | +using ComTypes = System.Runtime.InteropServices.ComTypes;  | 
 | 11 | + | 
 | 12 | +#pragma warning disable CS0612, CS0618  | 
 | 13 | +partial class Test  | 
 | 14 | +{  | 
 | 15 | +    public static int Main()  | 
 | 16 | +    {  | 
 | 17 | +        bool testComMarshal=true;  | 
 | 18 | +        ComWrappers.RegisterForMarshalling(new ComWrappersImpl());  | 
 | 19 | +        try  | 
 | 20 | +        {  | 
 | 21 | +            TestByValue(testComMarshal);  | 
 | 22 | +            TestByRef(testComMarshal);  | 
 | 23 | +            TestOut();  | 
 | 24 | +            TestFieldByValue(testComMarshal);  | 
 | 25 | +            TestFieldByRef(testComMarshal);  | 
 | 26 | +        }  | 
 | 27 | +        catch (Exception e)  | 
 | 28 | +        {  | 
 | 29 | +            Console.WriteLine($"Test failed: {e}");  | 
 | 30 | +            return 101;  | 
 | 31 | +        }  | 
 | 32 | +        return 100;  | 
 | 33 | +    }  | 
 | 34 | +}  | 
 | 35 | + | 
 | 36 | +internal unsafe class ComWrappersImpl : ComWrappers  | 
 | 37 | +{  | 
 | 38 | +    private static readonly ComInterfaceEntry* wrapperEntry;  | 
 | 39 | + | 
 | 40 | +    static ComWrappersImpl()  | 
 | 41 | +    {  | 
 | 42 | +        var vtblRaw = (IntPtr*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(IDispatchVtbl), sizeof(IntPtr) * 7);  | 
 | 43 | +        GetIUnknownImpl(out vtblRaw[0], out vtblRaw[1], out vtblRaw[2]);  | 
 | 44 | + | 
 | 45 | +        vtblRaw[3] = (IntPtr)(delegate* unmanaged<IntPtr, IntPtr, int>)&IDispatchVtbl.GetTypeInfoCountInternal;  | 
 | 46 | +        vtblRaw[4] = (IntPtr)(delegate* unmanaged<IntPtr, int, int, IntPtr, int>)&IDispatchVtbl.GetTypeInfoInternal;  | 
 | 47 | +        vtblRaw[5] = (IntPtr)(delegate* unmanaged<IntPtr, IntPtr, IntPtr, int, int, IntPtr, int>)&IDispatchVtbl.GetIDsOfNamesInternal;  | 
 | 48 | +        vtblRaw[6] = (IntPtr)(delegate* unmanaged<IntPtr, int, IntPtr, int, ComTypes.INVOKEKIND, IntPtr, IntPtr, IntPtr, IntPtr, int>)&IDispatchVtbl.InvokeInternal;  | 
 | 49 | + | 
 | 50 | +        wrapperEntry = (ComInterfaceEntry*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(IDispatchVtbl), sizeof(ComInterfaceEntry));  | 
 | 51 | +        wrapperEntry->IID = IDispatchVtbl.IID_IDispatch;  | 
 | 52 | +        wrapperEntry->Vtable = (IntPtr)vtblRaw;  | 
 | 53 | +    }  | 
 | 54 | + | 
 | 55 | +    protected override unsafe ComInterfaceEntry* ComputeVtables(object obj, CreateComInterfaceFlags flags, out int count)  | 
 | 56 | +    {  | 
 | 57 | +        // Always return the same table mappings.  | 
 | 58 | +        count = 1;  | 
 | 59 | +        return wrapperEntry;  | 
 | 60 | +    }  | 
 | 61 | + | 
 | 62 | +    protected override object CreateObject(IntPtr externalComObject, CreateObjectFlags flags)  | 
 | 63 | +    {  | 
 | 64 | +        throw new NotImplementedException();  | 
 | 65 | +    }  | 
 | 66 | + | 
 | 67 | +    protected override void ReleaseObjects(IEnumerable objects)  | 
 | 68 | +    {  | 
 | 69 | +        throw new NotImplementedException();  | 
 | 70 | +    }  | 
 | 71 | +}  | 
 | 72 | +public struct IDispatchVtbl  | 
 | 73 | +{  | 
 | 74 | +    internal static readonly Guid IID_IDispatch = new Guid("00020400-0000-0000-C000-000000000046");  | 
 | 75 | + | 
 | 76 | +    [UnmanagedCallersOnly]  | 
 | 77 | +    public static int GetTypeInfoCountInternal(IntPtr thisPtr, IntPtr i)  | 
 | 78 | +    {  | 
 | 79 | +        return 0; // S_OK;  | 
 | 80 | +    }  | 
 | 81 | + | 
 | 82 | +    [UnmanagedCallersOnly]  | 
 | 83 | +    public static int GetTypeInfoInternal(IntPtr thisPtr, int itinfo, int lcid, IntPtr i)  | 
 | 84 | +    {  | 
 | 85 | +        return 0; // S_OK;  | 
 | 86 | +    }  | 
 | 87 | + | 
 | 88 | +    [UnmanagedCallersOnly]  | 
 | 89 | +    public static int GetIDsOfNamesInternal(  | 
 | 90 | +        IntPtr thisPtr,  | 
 | 91 | +        IntPtr iid,  | 
 | 92 | +        IntPtr names,  | 
 | 93 | +        int namesCount,  | 
 | 94 | +        int lcid,  | 
 | 95 | +        IntPtr dispIds)  | 
 | 96 | +    {  | 
 | 97 | +        return 0; // S_OK;  | 
 | 98 | +    }  | 
 | 99 | + | 
 | 100 | +    [UnmanagedCallersOnly]  | 
 | 101 | +    public static int InvokeInternal(  | 
 | 102 | +        IntPtr thisPtr,  | 
 | 103 | +        int dispIdMember,  | 
 | 104 | +        IntPtr riid,  | 
 | 105 | +        int lcid,  | 
 | 106 | +        ComTypes.INVOKEKIND wFlags,  | 
 | 107 | +        IntPtr pDispParams,  | 
 | 108 | +        IntPtr VarResult,  | 
 | 109 | +        IntPtr pExcepInfo,  | 
 | 110 | +        IntPtr puArgErr)  | 
 | 111 | +    {  | 
 | 112 | +        return 0; // S_OK;  | 
 | 113 | +    }  | 
 | 114 | +}  | 
0 commit comments