Пример #1
0
        public unsafe static bool CreateActivationContext(string dllPath, int nativeResourceManifestID)
        {
            lock (typeof(ThemingScope))
            {
                if (!s_contextCreationSucceeded)
                {
                    fixed(char *pDllPath = dllPath)
                    {
                        s_enableThemingActivationContext = new Kernel32.ACTCTXW
                        {
                            cbSize         = (uint)Marshal.SizeOf <Kernel32.ACTCTXW>(),
                            lpSource       = pDllPath,
                            lpResourceName = (IntPtr)nativeResourceManifestID,
                            dwFlags        = Kernel32.ACTCTX_FLAG.RESOURCE_NAME_VALID
                        };

                        s_hActCtx = Kernel32.CreateActCtxW(ref s_enableThemingActivationContext);
                    }

                    s_contextCreationSucceeded = (s_hActCtx != new IntPtr(-1));
                }

                return(s_contextCreationSucceeded);
            }
        }
Пример #2
0
        public unsafe static bool CreateActivationContext(Stream manifest)
        {
            lock (typeof(ThemingScope))
            {
                if (!s_contextCreationSucceeded)
                {
                    string tempFilePath = Path.Join(Path.GetTempPath(), Path.GetRandomFileName());
                    using FileStream tempFileStream = new FileStream(
                              tempFilePath,
                              FileMode.CreateNew,
                              FileAccess.ReadWrite,
                              FileShare.Delete | FileShare.ReadWrite);

                    manifest.CopyTo(tempFileStream);

                    // CreateActCtxW gives a sharing violation if we have the handle open
                    tempFileStream.Close();

                    fixed(char *p = tempFilePath)
                    {
                        s_enableThemingActivationContext = new Kernel32.ACTCTXW
                        {
                            cbSize   = (uint)sizeof(Kernel32.ACTCTXW),
                            lpSource = p
                        };

                        s_hActCtx = Kernel32.CreateActCtxW(ref s_enableThemingActivationContext);
                    }

                    s_contextCreationSucceeded = (s_hActCtx != new IntPtr(-1));

                    try
                    {
                        File.Delete(tempFilePath);
                    }
                    catch (Exception e) when(e is UnauthorizedAccessException or IOException)
                    {
                        // Don't want to take down WinForms if we can't delete is file
                    }
                }

                return(s_contextCreationSucceeded);
            }
        }
    }
Пример #3
0
    private unsafe void ExecuteWithActivationContext(string applicationManifest, Action action)
    {
        var    context = new Kernel32.ACTCTXW();
        IntPtr handle;

        fixed(char *p = applicationManifest)
        {
            context.cbSize   = (uint)sizeof(Kernel32.ACTCTXW);
            context.lpSource = p;

            handle = Kernel32.CreateActCtxW(ref context);
        }

        if (handle == IntPtr.Zero)
        {
            throw new Win32Exception();
        }

        try
        {
            if (Kernel32.ActivateActCtx(handle, out var cookie).IsFalse())
            {
                throw new Win32Exception();
            }

            try
            {
                action();
            }
            finally
            {
                if (Kernel32.DeactivateActCtx(0, cookie).IsFalse())
                {
                    throw new Win32Exception();
                }
            }
        }
        finally
        {
            ReleaseActCtx(handle);
        }
    }
Пример #4
0
        public unsafe static bool CreateActivationContext(IntPtr module, int nativeResourceManifestID)
        {
            lock (typeof(ThemingScope))
            {
                if (!s_contextCreationSucceeded)
                {
                    s_enableThemingActivationContext = new Kernel32.ACTCTXW
                    {
                        cbSize         = (uint)sizeof(Kernel32.ACTCTXW),
                        lpResourceName = (IntPtr)nativeResourceManifestID,
                        dwFlags        = Kernel32.ACTCTX_FLAG.HMODULE_VALID | Kernel32.ACTCTX_FLAG.RESOURCE_NAME_VALID,
                        hModule        = module
                    };

                    s_hActCtx = Kernel32.CreateActCtxW(ref s_enableThemingActivationContext);

                    s_contextCreationSucceeded = (s_hActCtx != new IntPtr(-1));
                }

                return(s_contextCreationSucceeded);
            }
        }