views:

226

answers:

3

Does anybody know a way to deactivate the autoplay function of windows using c#/.NET?

+1  A: 

RegisterWindowMessage is a Win32 API call. So you will need to use PInvoke to make it work..

using System.Runtime.InteropServices;

class Win32Call
{
[DllImport("user32.dll")]
   public static extern int RegisterWindowMessage(String strMessage);
}

// In your application you will call

Win32Call.RegisterWindowMessage("QueryCancelAutoPlay");

From here (The Experts-Exchange link at the top). There is additional help on that site with some more examples that may be a little more comprehensive than the above. The above does however solve the problem.

Kyle Rozendo
@Kyle - you can't see the content from e-e via a direct link, unless you join. The content only shows up when you link from a Google search.
ChrisF
@ChrisF - That I did not know, thanks, I've updated the link to a google link of the site.
Kyle Rozendo
You can't scroll to the bottom of the page anymore? E-E used to work that way when you didn't have a login. But at least there's Google Cache...
ewall
Thanks this helped!
isamux
Very important: This event is only sent to the current foreground window!http://www.codeproject.com/Messages/2313454/Important-point-missing.aspx
isamux
A: 

Some additional links that might be helpful:

isamux
+1  A: 

A little summary, for all the others looking for a good way to disable/supress autoplay. So far I've found 3 methods to disable autoplay programatically:

  1. Intercepting the QueryCancelAutoPlay message
  2. Using the Registry
  3. Implementing the COM Interface IQueryCancelAutoPlay

In the end I chose the 3rd method and used the IQueryCancelAutoPlay interface because the others had some signifcant disadvantages:

  • The first method (QueryCancelAutoPlay) was only able to suppress autoplay if the application window was in the foreground, cause only the foreground window receives the message
  • Configuring autoplay in the registry worked even if the application window was in the background. The downside: It required a restart of the currently running explorer.exe to take effect...so this was no solution to temporarily disable autoplay.

Examples for the implementation

1. QueryCancelAutoPlay

Note: If your application is using a dialog box you need to call SetWindowLong (signature) instead of just returning false. See here for more details)

2. Registry

Using the registry you can disables AutoRun for specified drive letters (NoDriveAutoRun) or for a class of drives (NoDriveTypeAutoRun)

3. IQueryCancelAutoPlay

Some other links:

isamux