Beispiel #1
0
            /// <summary>Allocates memory for a <see cref="Guid"/> value and initializes it.</summary>
            /// <returns>A pointer to memory holding the <see cref="Guid"/> value for the current type.</returns>
            private static Guid *CreateRIID()
            {
                var p = (Guid *)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(T), sizeof(Guid));

                *p = typeof(T).GUID;
                return(p);
            }
        /// <summary>
        /// Builds the custom method table pointer for <see cref="ID3DIncludeForD2DHelpers"/>.
        /// </summary>
        /// <returns>The method table pointer for <see cref="ID3DIncludeForD2DHelpers"/>.</returns>
        private static void **InitVtbl()
        {
            void **lpVtbl = (void **)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(ID3DIncludeForD2DHelpers), sizeof(void *) * 2);

#if NET6_0_OR_GREATER
            lpVtbl[0] = (delegate * unmanaged <ID3DIncludeForD2DHelpers *, D3D_INCLUDE_TYPE, sbyte *, void *, void **, uint *, int>) & Open;
            lpVtbl[1] = (delegate * unmanaged <ID3DIncludeForD2DHelpers *, void *, int>) & Close;
#else
            lpVtbl[0] = (void *)Marshal.GetFunctionPointerForDelegate(OpenWrapper);
            lpVtbl[1] = (void *)Marshal.GetFunctionPointerForDelegate(CloseWrapper);
#endif

            return(lpVtbl);
        }
Beispiel #3
0
    /// <summary>
    /// Initializes the shared state for <see cref="PixelShaderEffect"/>.
    /// </summary>
    static PixelShaderEffect()
    {
        void **lpVtbl = (void **)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(PixelShaderEffect), sizeof(void *) * 14);

        // ID2D1EffectImpl
#if NET6_0_OR_GREATER
        lpVtbl[0] = (delegate * unmanaged <PixelShaderEffect *, Guid *, void **, int>) & ID2D1EffectImplMethods.QueryInterface;
        lpVtbl[1] = (delegate * unmanaged <PixelShaderEffect *, uint>) & ID2D1EffectImplMethods.AddRef;
        lpVtbl[2] = (delegate * unmanaged <PixelShaderEffect *, uint>) & ID2D1EffectImplMethods.Release;
        lpVtbl[3] = (delegate * unmanaged <PixelShaderEffect *, ID2D1EffectContext *, ID2D1TransformGraph *, int>) & ID2D1EffectImplMethods.Initialize;
        lpVtbl[4] = (delegate * unmanaged <PixelShaderEffect *, D2D1_CHANGE_TYPE, int>) & ID2D1EffectImplMethods.PrepareForRender;
        lpVtbl[5] = (delegate * unmanaged <PixelShaderEffect *, ID2D1TransformGraph *, int>) & ID2D1EffectImplMethods.SetGraph;
#else
        lpVtbl[0] = (void *)Marshal.GetFunctionPointerForDelegate(ID2D1EffectImplMethods.QueryInterfaceWrapper);
        lpVtbl[1] = (void *)Marshal.GetFunctionPointerForDelegate(ID2D1EffectImplMethods.AddRefWrapper);
        lpVtbl[2] = (void *)Marshal.GetFunctionPointerForDelegate(ID2D1EffectImplMethods.ReleaseWrapper);
        lpVtbl[3] = (void *)Marshal.GetFunctionPointerForDelegate(ID2D1EffectImplMethods.InitializeWrapper);
        lpVtbl[4] = (void *)Marshal.GetFunctionPointerForDelegate(ID2D1EffectImplMethods.PrepareForRenderWrapper);
        lpVtbl[5] = (void *)Marshal.GetFunctionPointerForDelegate(ID2D1EffectImplMethods.SetGraphWrapper);
#endif

        // ID2D1DrawTransform
#if NET6_0_OR_GREATER
        lpVtbl[6 + 0] = (delegate * unmanaged <PixelShaderEffect *, Guid *, void **, int>) & ID2D1DrawTransformMethods.QueryInterface;
        lpVtbl[6 + 1] = (delegate * unmanaged <PixelShaderEffect *, uint>) & ID2D1DrawTransformMethods.AddRef;
        lpVtbl[6 + 2] = (delegate * unmanaged <PixelShaderEffect *, uint>) & ID2D1DrawTransformMethods.Release;
        lpVtbl[6 + 3] = (delegate * unmanaged <PixelShaderEffect *, uint>) & ID2D1DrawTransformMethods.GetInputCount;
        lpVtbl[6 + 4] = (delegate * unmanaged <PixelShaderEffect *, RECT *, RECT *, uint, int>) & ID2D1DrawTransformMethods.MapOutputRectToInputRects;
        lpVtbl[6 + 5] = (delegate * unmanaged <PixelShaderEffect *, RECT *, RECT *, uint, RECT *, RECT *, int>) & ID2D1DrawTransformMethods.MapInputRectsToOutputRect;
        lpVtbl[6 + 6] = (delegate * unmanaged <PixelShaderEffect *, uint, RECT, RECT *, int>) & ID2D1DrawTransformMethods.MapInvalidRect;
        lpVtbl[6 + 7] = (delegate * unmanaged <PixelShaderEffect *, ID2D1DrawInfo *, int>) & ID2D1DrawTransformMethods.SetDrawInfo;
#else
        lpVtbl[6 + 0] = (void *)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.QueryInterfaceWrapper);
        lpVtbl[6 + 1] = (void *)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.AddRefWrapper);
        lpVtbl[6 + 2] = (void *)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.ReleaseWrapper);
        lpVtbl[6 + 3] = (void *)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.GetInputCountWrapper);
        lpVtbl[6 + 4] = (void *)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.MapOutputRectToInputRectsWrapper);
        lpVtbl[6 + 5] = (void *)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.MapInputRectsToOutputRectWrapper);
        lpVtbl[6 + 6] = (void *)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.MapInvalidRectWrapper);
        lpVtbl[6 + 7] = (void *)Marshal.GetFunctionPointerForDelegate(ID2D1DrawTransformMethods.SetDrawInfoWrapper);
#endif

        VtblForID2D1EffectImpl    = lpVtbl;
        VtblForID2D1DrawTransform = &lpVtbl[6];
    }
Beispiel #4
0
        /// <summary>
        /// Builds the custom method table pointer for <see cref="IDXGIFactory4As6Backcompat"/>.
        /// </summary>
        /// <returns>The method table pointer for <see cref="IDXGIFactory4As6Backcompat"/>.</returns>
        private static void **InitVtbl()
        {
            void **lpVtbl = (void **)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(IDXGIFactory4As6Backcompat), sizeof(void *) * 30);

            new Span <IntPtr>(lpVtbl, 30).Clear();

#if NET6_0_OR_GREATER
            lpVtbl[2]  = (delegate * unmanaged <IDXGIFactory4As6Backcompat *, uint>) & Release;
            lpVtbl[27] = (delegate * unmanaged <IDXGIFactory4As6Backcompat *, Guid *, void **, int>) & EnumWarpAdapter;
            lpVtbl[29] = (delegate * unmanaged <IDXGIFactory4As6Backcompat *, uint, DXGI_GPU_PREFERENCE, Guid *, void **, int>) & EnumAdapterByGpuPreference;
#else
            lpVtbl[2]  = (void *)Marshal.GetFunctionPointerForDelegate(ReleaseWrapper);
            lpVtbl[27] = (void *)Marshal.GetFunctionPointerForDelegate(EnumWarpAdapterWrapper);
            lpVtbl[29] = (void *)Marshal.GetFunctionPointerForDelegate(EnumAdapterByGpuPreferenceWrapper);
#endif

            return(lpVtbl);
        }
        /// <summary>
        /// Initializes the <see cref="For{T}"/> shared state.
        /// </summary>
        /// <param name="factory">The factory of <see cref="ID2D1TransformMapper{T}"/> instances to use for each created effect.</param>
        /// <exception cref="InvalidOperationException">Thrown if initialization is attempted with a mismatched transform factory.</exception>
        public static void Initialize(Func <ID2D1TransformMapper <T> >?factory)
        {
            // This conceptually acts as a static constructor, and this type is
            // internal, so in this very specific case locking on the type is fine.
            lock (typeof(For <T>))
            {
                if (isInitialized)
                {
                    // If the factory is already initialized, ensure the draw transform mapper is the same
                    if (d2D1DrawTransformMapperFactory != factory)
                    {
                        ThrowHelper.ThrowInvalidOperationException(
                            "Cannot initialize an ID2D1Effect factory for the same shader type with two different transform mappings. " +
                            "Make sure to only ever register a pixel shader effect with either no transform, or the same transform type.");
                    }
                }
                else
                {
                    // Load all shader properties
                    Guid guid       = typeof(T).GUID;
                    int  inputCount = (int)D2D1InteropServices.GetPixelShaderInputCount <T>();
                    ReadOnlyMemory <byte> buffer = D2D1InteropServices.LoadPixelShaderBytecode <T>();
                    byte *typeAssociatedMemory   = (byte *)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For <T>), buffer.Length);

                    // Copy the bytecode to the target buffer
                    buffer.Span.CopyTo(new Span <byte>(typeAssociatedMemory, buffer.Length));

                    // Set the shared state and mark the type as initialized
                    shaderId       = guid;
                    numberOfInputs = inputCount;
                    bytecode       = typeAssociatedMemory;
                    bytecodeSize   = buffer.Length;
                    d2D1DrawTransformMapperFactory = factory;

                    isInitialized = true;
                }
            }
        }
Beispiel #6
0
    public static byte[] CreateSelfSignCertificatePfx(
        string x500,
        DateTime startTime,
        DateTime endTime,
        SecureString password)
    {
        byte[] pfxData;

        if (x500 == null)
        {
            x500 = "";
        }

        SystemTime startSystemTime = ToSystemTime(startTime);
        SystemTime endSystemTime   = ToSystemTime(endTime);
        string     containerName   = Guid.NewGuid().ToString();

        GCHandle dataHandle       = new GCHandle();
        IntPtr   providerContext  = IntPtr.Zero;
        IntPtr   cryptKey         = IntPtr.Zero;
        IntPtr   certContext      = IntPtr.Zero;
        IntPtr   certStore        = IntPtr.Zero;
        IntPtr   storeCertContext = IntPtr.Zero;
        IntPtr   passwordPtr      = IntPtr.Zero;

        RuntimeHelpers.PrepareConstrainedRegions();
        try
        {
            Check(NativeMethods.CryptAcquireContextW(
                      out providerContext,
                      containerName,
                      null,
                      1,   // PROV_RSA_FULL
                      8)); // CRYPT_NEWKEYSET

            Check(NativeMethods.CryptGenKey(
                      providerContext,
                      1, // AT_KEYEXCHANGE
                      1, // CRYPT_EXPORTABLE
                      out cryptKey));

            IntPtr errorStringPtr;
            int    nameDataLength = 0;
            byte[] nameData;

            // errorStringPtr gets a pointer into the middle of the x500 string,
            // so x500 needs to be pinned until after we've copied the value
            // of errorStringPtr.
            dataHandle = GCHandle.Alloc(x500, GCHandleType.Pinned);

            if (!NativeMethods.CertStrToNameW(
                    0x00010001, // X509_ASN_ENCODING | PKCS_7_ASN_ENCODING
                    dataHandle.AddrOfPinnedObject(),
                    3,          // CERT_X500_NAME_STR = 3
                    IntPtr.Zero,
                    null,
                    ref nameDataLength,
                    out errorStringPtr))
            {
                string error = Marshal.PtrToStringUni(errorStringPtr);
                throw new ArgumentException(error);
            }

            nameData = new byte[nameDataLength];

            if (!NativeMethods.CertStrToNameW(
                    0x00010001, // X509_ASN_ENCODING | PKCS_7_ASN_ENCODING
                    dataHandle.AddrOfPinnedObject(),
                    3,          // CERT_X500_NAME_STR = 3
                    IntPtr.Zero,
                    nameData,
                    ref nameDataLength,
                    out errorStringPtr))
            {
                string error = Marshal.PtrToStringUni(errorStringPtr);
                throw new ArgumentException(error);
            }

            dataHandle.Free();

            dataHandle = GCHandle.Alloc(nameData, GCHandleType.Pinned);
            CryptoApiBlob nameBlob = new CryptoApiBlob(
                nameData.Length,
                dataHandle.AddrOfPinnedObject());

            CryptKeyProviderInformation kpi = new CryptKeyProviderInformation();
            kpi.ContainerName = containerName;
            kpi.ProviderType  = 1; // PROV_RSA_FULL
            kpi.KeySpec       = 1; // AT_KEYEXCHANGE

            certContext = NativeMethods.CertCreateSelfSignCertificate(
                providerContext,
                ref nameBlob,
                0,
                ref kpi,
                IntPtr.Zero, // default = SHA1RSA
                ref startSystemTime,
                ref endSystemTime,
                IntPtr.Zero);
            Check(certContext != IntPtr.Zero);
            dataHandle.Free();

            certStore = NativeMethods.CertOpenStore(
                "Memory", // sz_CERT_STORE_PROV_MEMORY
                0,
                IntPtr.Zero,
                0x2000, // CERT_STORE_CREATE_NEW_FLAG
                IntPtr.Zero);
            Check(certStore != IntPtr.Zero);

            Check(NativeMethods.CertAddCertificateContextToStore(
                      certStore,
                      certContext,
                      1, // CERT_STORE_ADD_NEW
                      out storeCertContext));

            NativeMethods.CertSetCertificateContextProperty(
                storeCertContext,
                2, // CERT_KEY_PROV_INFO_PROP_ID
                0,
                ref kpi);

            if (password != null)
            {
                passwordPtr = Marshal.SecureStringToCoTaskMemUnicode(password);
            }

            CryptoApiBlob pfxBlob = new CryptoApiBlob();
            Check(NativeMethods.PFXExportCertStoreEx(
                      certStore,
                      ref pfxBlob,
                      passwordPtr,
                      IntPtr.Zero,
                      7)); // EXPORT_PRIVATE_KEYS | REPORT_NO_PRIVATE_KEY | REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY

            pfxData      = new byte[pfxBlob.DataLength];
            dataHandle   = GCHandle.Alloc(pfxData, GCHandleType.Pinned);
            pfxBlob.Data = dataHandle.AddrOfPinnedObject();
            Check(NativeMethods.PFXExportCertStoreEx(
                      certStore,
                      ref pfxBlob,
                      passwordPtr,
                      IntPtr.Zero,
                      7)); // EXPORT_PRIVATE_KEYS | REPORT_NO_PRIVATE_KEY | REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY
            dataHandle.Free();
        }
        finally
        {
            if (passwordPtr != IntPtr.Zero)
            {
                Marshal.ZeroFreeCoTaskMemUnicode(passwordPtr);
            }

            if (dataHandle.IsAllocated)
            {
                dataHandle.Free();
            }

            if (certContext != IntPtr.Zero)
            {
                NativeMethods.CertFreeCertificateContext(certContext);
            }

            if (storeCertContext != IntPtr.Zero)
            {
                NativeMethods.CertFreeCertificateContext(storeCertContext);
            }

            if (certStore != IntPtr.Zero)
            {
                NativeMethods.CertCloseStore(certStore, 0);
            }

            if (cryptKey != IntPtr.Zero)
            {
                NativeMethods.CryptDestroyKey(cryptKey);
            }

            if (providerContext != IntPtr.Zero)
            {
                NativeMethods.CryptReleaseContext(providerContext, 0);
                NativeMethods.CryptAcquireContextW(
                    out providerContext,
                    containerName,
                    null,
                    1,     // PROV_RSA_FULL
                    0x10); // CRYPT_DELETEKEYSET
            }
        }

        return(pfxData);
    }
 internal ExecuteWithLockHelper(object lockObject, RuntimeHelpers.TryCode userCode, object userState)
 {
     this.m_lockObject = lockObject;
     this.m_userCode = userCode;
     this.m_userState = userState;
 }
 public static void ExecuteCodeWithGuaranteedCleanup(RuntimeHelpers.TryCode code, RuntimeHelpers.CleanupCode backoutCode, Object userData)
 {
 }
Beispiel #9
0
        /// <summary>
        /// Initializes the <see cref="For{T}"/> shared state.
        /// </summary>
        /// <param name="d2D1DrawTransformMapperFactory">The factory of <see cref="ID2D1TransformMapper{T}"/> instances to use for each created effect.</param>
        /// <exception cref="InvalidOperationException">Thrown if initialization is attempted with a mismatched transform factory.</exception>
        public static void Initialize(Func <ID2D1TransformMapper <T> >?d2D1DrawTransformMapperFactory)
        {
            // This conceptually acts as a static constructor, and this type is
            // internal, so in this very specific case locking on the type is fine.
            lock (typeof(For <T>))
            {
                if (isInitialized)
                {
                    // If the factory is already initialized, ensure the draw transform mapper is the same
                    if (For <T> .d2D1DrawTransformMapperFactory != d2D1DrawTransformMapperFactory)
                    {
                        ThrowHelper.ThrowInvalidOperationException(
                            "Cannot initialize an ID2D1Effect factory for the same shader type with two different transform mappings. " +
                            "Make sure to only ever register a pixel shader effect with either no transform, or the same transform type.");
                    }
                }
                else
                {
                    // Load all shader properties
                    Guid shaderId = typeof(T).GUID;
                    ReadOnlyMemory <byte> bytecodeInfo = D2D1PixelShader.LoadBytecode <T>();
                    int   bytecodeSize = bytecodeInfo.Length;
                    byte *bytecode     = (byte *)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For <T>), bytecodeSize);
                    D2D1BufferPrecision bufferPrecision = D2D1PixelShader.GetOutputBufferPrecision <T>();
                    D2D1ChannelDepth    channelDepth    = D2D1PixelShader.GetOutputBufferChannelDepth <T>();
                    D2D1PixelOptions    pixelOptions    = D2D1PixelShader.GetPixelOptions <T>();

                    // Prepare the inputs info
                    int inputCount = D2D1PixelShader.GetInputCount <T>();
                    D2D1PixelShaderInputType *inputTypes = (D2D1PixelShaderInputType *)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For <T>), sizeof(D2D1PixelShaderInputType) * inputCount);

                    for (int i = 0; i < inputCount; i++)
                    {
                        inputTypes[i] = D2D1PixelShader.GetInputType <T>(i);
                    }

                    // Prepare the input descriptions
                    ReadOnlyMemory <D2D1InputDescription> inputDescriptionsInfo = D2D1PixelShader.GetInputDescriptions <T>();
                    int inputDescriptionCount = inputDescriptionsInfo.Length;
                    D2D1InputDescription *inputDescriptions = (D2D1InputDescription *)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(For <T>), sizeof(D2D1InputDescription) * inputDescriptionCount);

                    inputDescriptionsInfo.Span.CopyTo(new Span <D2D1InputDescription>(inputDescriptions, inputDescriptionCount));

                    // Copy the bytecode to the target buffer
                    bytecodeInfo.Span.CopyTo(new Span <byte>(bytecode, bytecodeSize));

                    // Set the shared state and mark the type as initialized
                    For <T> .shaderId                       = shaderId;
                    For <T> .inputCount                     = inputCount;
                    For <T> .inputTypes                     = inputTypes;
                    For <T> .inputDescriptionCount          = inputDescriptionCount;
                    For <T> .inputDescriptions              = inputDescriptions;
                    For <T> .pixelOptions                   = pixelOptions;
                    For <T> .bytecode                       = bytecode;
                    For <T> .bytecodeSize                   = bytecodeSize;
                    For <T> .bufferPrecision                = bufferPrecision;
                    For <T> .channelDepth                   = channelDepth;
                    For <T> .d2D1DrawTransformMapperFactory = d2D1DrawTransformMapperFactory;

                    isInitialized = true;
                }
            }
        }