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();
}
}
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