从打开文件资源管理器中停止SetVolumeMountPoint

时间:2019-01-03 21:31:31

标签: c# c++ winapi kernel32 vhd

我正在使用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();
            }
        }

2 个答案:

答案 0 :(得分:4)

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

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

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

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

答案 1 :(得分:0)

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

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

注意

  

NoDriveAutoRun和NoDriveTypeAutoRun值应仅为   由系统管理员修改以更改整个值   用于测试或管理目的的系统。应用程序不应   修改这些值,因为无法可靠地将其还原到   它们的原始值。

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

注意

  

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

     

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