1

I am using SetVolumeMountPoint to mount a vhd to a drive letter of my choosing. The problem is that when the vhd is mounted file explorer automatically opens at the new drive directory. This is a problem for me as I need my programs to remain on the foreground and sometimes the spawned file explorer becomes part of the foreground.

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

Thoughts?

UPDATE:

I programmatically set the noautorun registry key using these two methods before mounting my vhd:

        /// <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 Answers2

4

Cleanest way seem to be to catch RegisterWindowMessage("QueryCancelAutoPlay") message by your foreground window and return TRUE from your window procedure.

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

EDIT: If foreground window is not your application window, then I would recommend against editing registry, since it is global state, whereas you need just temporary autorun bypass.

Besides windows hook, mentioned in other answer, I would suggest registering your implementation of IQueryCancelAutoPlay interface in running object table

Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
  • It looks like I can disable autorun for specific drive letters from the registry which is a better solution for my application since I always mount the vhds on specific drive letters and my application is not designed to be the foreground window, but it spawns other applications that are the foreground window. – Edgar Arakelyan Jan 03 '19 at 22:32
  • 1
    I suggest against registry change, I've edited my answer – Alex Guteniev Jan 04 '19 at 19:42
0

Another way is using Registry.

Please reference "Using the Registry to Disable AutoRun" "How to disable the Autorun functionality in Windows"

Note

The NoDriveAutoRun and NoDriveTypeAutoRun values should only be modified by system administrators to change the value for the entire system for testing or administrative purposes. Applications should not modify these values, as there is no way to reliably restore them to their original values.

The third way is based on @Alexander Gutenev have pointed out that register a "QueryCancelAutoPlay" window message and then install a global hook from your application to monitor this message.

Note

You should use global hooks only for debugging purposes; otherwise, you should avoid them. Global hooks hurt system performance and cause conflicts with other applications that implement the same type of global hook.

Hooks tend to slow down the system because they increase the amount of processing the system must perform for each message. You should install a hook only when necessary, and remove it as soon as possible.

Rita Han
  • 9,574
  • 1
  • 11
  • 24