【问题标题】:Stop SetVolumeMountPoint from opening file explorer停止 SetVolumeMountPoint 打开文件资源管理器
【发布时间】:2019-05-30 12:03:26
【问题描述】:

我正在使用 SetVolumeMountPoint 将 vhd 挂载到我选择的驱动器号。问题是当 vhd 被挂载时,文件资源管理器会自动在新的驱动器目录中打开。这对我来说是个问题,因为我需要我的程序保持在前台,有时生成的文件资源管理器会成为前台的一部分。

https://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-setvolumemountpointa

想法?

更新:

在挂载我的 vhd 之前,我使用这两种方法以编程方式设置 noautorun 注册表项:

        /// <summary>
        /// Removing file explorer auto run for the given DriveLetter so that when a vhd is mounted file explorer doesn't open
        /// </summary>
        /// <param name="DriveLetter"></param>
        private void RemoveFileExplorerAutoRun(char DriveLetter)
        {
            var KeyPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer";
            RegistryKey AutoRunKey = Registry.CurrentUser.OpenSubKey(KeyPath, true);
            var DriveLetterValue = DriveLetter - 'A';

            if (AutoRunKey != null)
            {
                RemoveFileExplorerAutoRun(AutoRunKey, DriveLetterValue);
            }
            else // create key as it does not exist
            {
                AutoRunKey = Registry.CurrentUser.CreateSubKey(KeyPath);
                RemoveFileExplorerAutoRun(AutoRunKey, DriveLetterValue);
            }
        }

        private void RemoveFileExplorerAutoRun(RegistryKey AutoRunKey, int DriveLetterValue)
        {
            if (AutoRunKey != null)
            {
                AutoRunKey.SetValue("NoDriveTypeAutoRun", DriveLetterValue);
                AutoRunKey.Close();
            }
        }

【问题讨论】:

    标签: c# c++ winapi kernel32 vhd


    【解决方案1】:

    最干净的方法似乎是通过您的前台窗口捕获 RegisterWindowMessage("QueryCancelAutoPlay") 消息并从您的窗口过程返回 TRUE。

    https://docs.microsoft.com/en-us/windows/desktop/shell/autoplay-reg

    编辑: 如果前台窗口不是您的应用程序窗口,那么我建议您不要编辑注册表,因为它是全局状态,而您只需要临时自动运行绕过。

    除了其他答案中提到的 windows 钩子外,我建议您在 running object table 中注册 IQueryCancelAutoPlay 接口的实现

    【讨论】:

    • 看起来我可以为注册表中的特定驱动器号禁用自动运行,这对我的应用程序来说是一个更好的解决方案,因为我总是将 vhds 安装在特定的驱动器号上,而我的应用程序并非设计为前台窗口,但它会生成其他作为前台窗口的应用程序。
    • 我建议不要更改注册表,我已经编辑了答案
    【解决方案2】:

    另一种方法是使用注册表。

    请参考“Using the Registry to Disable AutoRun”“How to disable the Autorun functionality in Windows

    注意

    NoDriveAutoRun 和 NoDriveTypeAutoRun 值只能是 由系统管理员修改以更改整个值 用于测试或管理目的的系统。应用程序不应 修改这些值,因为没有办法可靠地将它们恢复到 它们的原始值。

    第三种方法是基于@Alexander Gutenev 指出的,注册一个“QueryCancelAutoPlay”窗口消息,然后从您的应用程序中安装一个全局hook 来监控此消息。

    注意

    您应该仅将全局挂钩用于调试目的;否则, 你应该避免它们。全局挂钩会损害系统性能并导致 与实现相同类型的其他应用程序冲突 全局钩子。

    钩子往往会减慢系统速度,因为它们会增加 处理系统必须为每条消息执行。你应该 仅在必要时安装挂钩,并尽快将其移除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      相关资源
      最近更新 更多