C#:调试器中的 comctl32.dll 版本 6

2024-01-04

我正在使用WindowsAPI代码包 http://code.msdn.microsoft.com/WindowsAPICodePack对于任务对话框。当我尝试显示该对话框时,它说需要加载 comctl32.dll 的版本 6。所以我将版本 6 添加到 app.manifest 并尝试运行它。还是没有运气。我进入 Debug 文件夹并在没有 Visual Studio 的情况下运行该程序,它工作正常。我猜测 Visual Studio 没有使用清单文件...我想知道是否有办法让它做到这一点。


Robpol86,您的代码抛出 SEHExceptions,因为 ActivateActCtx 和 DeactivateActCtx 的签名不正确。你必须使用UIntPtr代替uint对于 lpCookie。

因此,EnableThemingInScope.cs 的正确代码是:

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Windows.Forms;

namespace Microsoft.WindowsAPICodePack.Dialogs
{
    /// http://support.microsoft.com/kb/830033
    /// <devdoc>
    ///     This class is intended to use with the C# 'using' statement in
    ///     to activate an activation context for turning on visual theming at
    ///     the beginning of a scope, and have it automatically deactivated
    ///     when the scope is exited.
    /// </devdoc>

    [SuppressUnmanagedCodeSecurity]
    internal class EnableThemingInScope : IDisposable
    {
        // Private data
        private UIntPtr cookie;
        private static ACTCTX enableThemingActivationContext;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
        private static IntPtr hActCtx;
        private static bool contextCreationSucceeded = false;

        public EnableThemingInScope(bool enable)
        {
            cookie = UIntPtr.Zero;
            if (enable && OSFeature.Feature.IsPresent(OSFeature.Themes))
            {
                if (EnsureActivateContextCreated())
                {
                    if (!ActivateActCtx(hActCtx, out cookie))
                    {
                        // Be sure cookie always zero if activation failed
                        cookie = UIntPtr.Zero;
                    }
                }
            }
        }

        ~EnableThemingInScope()
        {
            Dispose();
        }

        void IDisposable.Dispose()
        {
            Dispose();
            GC.SuppressFinalize(this);
        }

        private void Dispose()
        {
            if (cookie != UIntPtr.Zero)
            {
                try
                {
                    if (DeactivateActCtx(0, cookie))
                    {
                        // deactivation succeeded...
                        cookie = UIntPtr.Zero;
                    }
                }
                catch (SEHException)
                {
                    //Hopefully solved this exception
                }
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2002:DoNotLockOnObjectsWithWeakIdentity")]
        private static bool EnsureActivateContextCreated()
        {
            lock (typeof(EnableThemingInScope))
            {
                if (!contextCreationSucceeded)
                {
                    // Pull manifest from the .NET Framework install
                    // directory

                    string assemblyLoc = null;

                    FileIOPermission fiop = new FileIOPermission(PermissionState.None);
                    fiop.AllFiles = FileIOPermissionAccess.PathDiscovery;
                    fiop.Assert();
                    try
                    {
                        assemblyLoc = typeof(Object).Assembly.Location;
                    }
                    finally
                    {
                        CodeAccessPermission.RevertAssert();
                    }

                    string manifestLoc = null;
                    string installDir = null;
                    if (assemblyLoc != null)
                    {
                        installDir = Path.GetDirectoryName(assemblyLoc);
                        const string manifestName = "XPThemes.manifest";
                        manifestLoc = Path.Combine(installDir, manifestName);
                    }

                    if (manifestLoc != null && installDir != null)
                    {
                        enableThemingActivationContext = new ACTCTX();
                        enableThemingActivationContext.cbSize = Marshal.SizeOf(typeof(ACTCTX));
                        enableThemingActivationContext.lpSource = manifestLoc;

                        // Set the lpAssemblyDirectory to the install
                        // directory to prevent Win32 Side by Side from
                        // looking for comctl32 in the application
                        // directory, which could cause a bogus dll to be
                        // placed there and open a security hole.
                        enableThemingActivationContext.lpAssemblyDirectory = installDir;
                        enableThemingActivationContext.dwFlags = ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID;

                        // Note this will fail gracefully if file specified
                        // by manifestLoc doesn't exist.
                        hActCtx = CreateActCtx(ref enableThemingActivationContext);
                        contextCreationSucceeded = (hActCtx != new IntPtr(-1));
                    }
                }

                // If we return false, we'll try again on the next call into
                // EnsureActivateContextCreated(), which is fine.
                return contextCreationSucceeded;
            }
        }

        // All the pinvoke goo...
        [DllImport("Kernel32.dll")]
        private extern static IntPtr CreateActCtx(ref ACTCTX actctx);
        [DllImport("Kernel32.dll")]
        private extern static bool ActivateActCtx(IntPtr hActCtx, out UIntPtr lpCookie);
        [DllImport("Kernel32.dll")]
        private extern static bool DeactivateActCtx(uint dwFlags, UIntPtr lpCookie);

        private const int ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID = 0x004;

        private struct ACTCTX
        {
            public int cbSize;
            public uint dwFlags;
            public string lpSource;
            public ushort wProcessorArchitecture;
            public ushort wLangId;
            public string lpAssemblyDirectory;
            public string lpResourceName;
            public string lpApplicationName;
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C#:调试器中的 comctl32.dll 版本 6 的相关文章

随机推荐

  • Apollo 客户端 - 使用对象列表中的缓存结果来响应单个对象的查询

    是否可以将 Apollo 客户端配置为从返回项目列表的查询中获取单个缓存的项目 以便在查询单个项目时预取数据 Schema type Item id ID name String type Query items Item itemById
  • Sqlalchemy:更新...限制 1,不可能吗?

    在 MySQL 中 可以限制更新查询影响的记录数量 在理想的情况下 这应该是没有必要的 但在某些情况下 有这样的限制确实有助于节省你的培根 我本以为在 SQLAlchemy 中它可以通过以下方式实现 tgt meta tables ps p
  • 什么 powershell 动词适合重新加载用户配置文件?

    我的配置文件中有一个命令行开关 可以重新加载我的用户配置文件 每当我编辑我的个人资料时 VSCode 都会警告我 我使用的名字 Reload Profile 使用未经批准的动词 While Reload似乎是一个理智 明显且易于搜索的动词来
  • 将 Flask 应用程序部署到 godaddy 托管

    我正在使用 Python Flask 微框架 http flask pocoo org http flask pocoo org 来构建一个小应用程序 在本地测试了该应用程序并将其部署到 godaddy 当我转到 godaddy 时 我看到
  • 如何让 Page.ClientScript.RegisterClientScriptIninclude 包含在头部中?

    包含的脚本引用 特别是 jQuery 在视图状态之后呈现 有没有办法把它放在 中 Page ClientScript RegisterClientScriptInclude jQuery scripts jquery js 我正在尝试在用户
  • Spring AOP忽略Hessian Service的一些方法

    我有一个具有以下切入点定义的方面 Pointcut execution public de company project 以及包含以下内容的弹簧配置
  • 如何通过iOS模拟器发送邮件?

    我想知道是否可以通过 iPhone 模拟器发送电子邮件 我见过 通过iphone发送邮件的教程如下 http www edumobile org iphone iphone programming tutorials compose mai
  • Scanf 函数在查找 %d 匹配项时是否忽略 Enter 键?

    我是 C 语言新手 正在阅读 Kim N King 的书来学习它 它说scanf 查找忽略空格的数字模式 但我认为它也会跳过 Enter 键 如果它寻找字符 它显然也会包含空格 因此在这个示例代码中我必须使用getchar 在第二个之前清除
  • 在 tmux 2.4 上如何一次性进入复制模式并开始搜索?

    我正在尝试将 F1 键绑定到 进入复制模式 开始反向搜索 我在网上唯一找到的是 bind key F1 copy mode send key 然而 这似乎不适用于 tmux 2 4 有没有办法让它在所有 tmux 版本上工作 这有效 bin
  • 这个 git smudge/clean 过滤器有什么问题?

    我想在我的 git 存储库中应用此过滤器 以便在签出期间从解决方案文件中删除一个部分 并在提交期间添加此部分 这是我要删除或添加的部分 GlobalSection SubversionScc preSolution Svn Managed
  • Heroku Ruby 版本无法升级?

    我正在努力更改 Heroku 上的 Ruby 版本 我使用 Ruby 2 0 0 和 Rails 4 我的 Gemfile 有 source https rubygems org ruby 2 0 0 Heroku 中的路径指向 herok
  • 如何处理 Swing 中图像显示的错误文件选择

    我正在学习 Swing 并编写了一个应用程序 可以让用户选择图像文件并将其显示在JPanel 它有效 但我想处理以下情况 用户没有选择任何文件 用户选择非图像文件 在这些情况下我想清除JPanel并在文本区域显示错误消息 我尝试按如下方式执
  • Android 地图上类似 iphone 的注释

    Inside my android map I have three overlays looking like this 我想要的是当我点击每个覆盖层以获得类似 iPhone 标注的内容时 与您在第二张图片 覆盖层顶部 上看到的内容类似
  • 如何根据每个项目在 IntelliJ 中配置编辑器的右边距

    有没有办法在每个项目的基础上配置右边距 列 在 代码样式 gt 常规 下 OR AND 是否有一种方法可以在每种语言的基础上配置相同的值 例如 我希望我的 Java 代码以 90 列结束 但我的 HTML 以 120 列结束 Thanks
  • FCM 安排推送通知的发送日期或时间

    我已经使用 FCM 控制台发送推送通知 它有一个选项来安排交付日期 但在参考资料中 此 API 并未记录为选项 我需要知道是否可以通过 POST 请求推送具有预定义交付日期的通知 如果您正在寻找用于计划推送的 FCM 公共 API 或可在其
  • 在 IIS 上发布时, 出现错误

    我有一个使用默认方法的测试 wcf 服务 并且 Web 配置是
  • Flask:如果路径是目录或文件,则处理捕获所有不同的 url

    如何制作一条捕获所有路径 仅处理目录和处理文件的路径 下面是一个简单的例子 from flask import Flask app Flask name app route foo def foo file return Queried f
  • 如何使用 C++ 将不同长度的整个 .txt 文件读取到数组中?

    我正在制作一个移位密码 从文件中读取文本并对其进行解码 解密工作正常 但是我无法弄清楚如何找到文件的长度而不将其硬编码为字符数组的大小 它还只读取一行 任何带有换行符的内容都会损坏 任何帮助将不胜感激 我省略了主要的代码块 因为它在读入数组
  • 使用 PHP 进行简单的分页

    我想用PHP实现分页 我有一些来自数据库的结果集 让它成为一个数组 我需要一个每页显示 4 条记录的分页 页码应如下 gt 当您选择第 2 页时 格式应为以下 gt 你们能给我推荐一些分页概念来实现这个吗 您实际上可以使用LIMITSQL
  • C#:调试器中的 comctl32.dll 版本 6

    我正在使用WindowsAPI代码包 http code msdn microsoft com WindowsAPICodePack对于任务对话框 当我尝试显示该对话框时 它说需要加载 comctl32 dll 的版本 6 所以我将版本 6