Can I disable window autoplay function programatically with C#/.NET?

前端 未结 4 1868
無奈伤痛
無奈伤痛 2020-12-09 06:46

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

相关标签:
4条回答
  • 2020-12-09 07:20

    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.

    0 讨论(0)
  • 2020-12-09 07:22

    Some additional links that might be helpful:

    • Preventing a CD from Auto-Playing shows some example vb.net code, showing the usage of "QueryCancelAutoPlay" on CodeProject.
    • Enabling and Disabling AutoRun on MSDN.
    0 讨论(0)
  • 2020-12-09 07:22

    Try this code work for me :) For more info check out this reference link : http://www.pinvoke.net/default.aspx/user32.registerwindowmessage

    using System.Runtime.InteropServices;
    
    //provide a private internal message id
    private UInt32 queryCancelAutoPlay = 0;
    
    [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
    static extern uint RegisterWindowMessage(string lpString);
    
    /* only needed if your application is using a dialog box and needs to 
    * respond to a "QueryCancelAutoPlay" message, it cannot simply return TRUE or FALSE.
    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    */
    
    protected override void WndProc(ref Message m)
    {
        //calling the base first is important, otherwise the values you set later will be lost
        base.WndProc (ref m);
    
        //if the QueryCancelAutoPlay message id has not been registered...
        if (queryCancelAutoPlay == 0)
            queryCancelAutoPlay = RegisterWindowMessage("QueryCancelAutoPlay");
    
        //if the window message id equals the QueryCancelAutoPlay message id
        if ((UInt32)m.Msg == queryCancelAutoPlay)
        {
            /* only needed if your application is using a dialog box and needs to
            * respond to a "QueryCancelAutoPlay" message, it cannot simply return TRUE or FALSE.
            SetWindowLong(this.Handle, 0, 1);
            */
            m.Result = (IntPtr)1;
        }
    } //WndProc
    
    0 讨论(0)
  • 2020-12-09 07:28

    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

    • Suppressing AutoRun Programmatically (MSDN article)
    • CodeProject: Preventing a CD from Auto-Playing
    • Canceling AutoPlay from C#

    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)

    • Using the Registry to Disable AutoRun (MSDN article)
    • How to Enable / Disable Autorun for a Drive (using Registry)
    • Windows 7 AutoPlay Enable | Disable

    3. IQueryCancelAutoPlay

    • Reference for the IQueryCancelAutoPlay interface on MSDN
    • IQueryCancelAutoPlay only called once? (Example implementatio, also read comments)
    • AutoPlayController (another implementation, not tested)

    Some other links:

    • Enabling and Disabling AutoRun (MSDN article)
    • Autoplay in Windows XP: Automatically Detect and React to New Devices on a System (an old but extensive article on Autoplay)
    0 讨论(0)
提交回复
热议问题