Beispiel #1
0
        private void UnRegisterCancelAutoPlay(CancelAutoPlayEventHandler capDelegate)
        {
            System.Runtime.InteropServices.ComTypes.IRunningObjectTable objTbl;
            if (NativeMethods.GetRunningObjectTable(0, out objTbl) == NativeMethods.S_OK)
            {
                if (_CancelAutoPlayCookie != 0)
                {
                    objTbl.Revoke(_CancelAutoPlayCookie);
                    _CancelAutoPlayCookie = 0;
                }

                if (_HWEventHandlerCookie != 0)
                {
                    objTbl.Revoke(_HWEventHandlerCookie);
                    _HWEventHandlerCookie = 0;

                    // Remove our do-nothing handler to the list of MTPMediaPlayerArrival handlers so it
                    // does not show up as a choice in the AutoPlay Dialog.
                    using (RegistryKey hKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\EventHandlers\MTPMediaPlayerArrival", true))
                    {
                        try
                        {
                            hKey.DeleteValue("SgtlMtpAutoPlayHandler");
                        }
                        catch (Exception e)
                        {
                            Trace.WriteLine(e.Message);
                        }
                    }
                }

                _CancelAutoPlay -= capDelegate;
            }
        }
Beispiel #2
0
        private bool RegisterCancelAutoPlay(CancelAutoPlayEventHandler capDelegate)
        {
            // Must be Windows XP or later
            if (Environment.OSVersion.Version < new Version(5, 1))
            {
                return(false); // false means failed
            }
            if (CreateAutoPlayHandlerRegistryEntries())
            {
                System.Runtime.InteropServices.ComTypes.IRunningObjectTable objTbl;
                if (NativeMethods.GetRunningObjectTable(0, out objTbl) == NativeMethods.S_OK)
                {
                    ///
                    /// Volume-based AutoPlay Cancelation
                    ///

                    if (_CancelAutoPlayCookie == 0)
                    {
                        // Create the volume-based moniker that we'll put in the ROT
                        System.Runtime.InteropServices.ComTypes.IMoniker vbMoniker;
                        if (NativeMethods.CreateClassMoniker(ref _CLSID_QueryCancelAutoPlay, out vbMoniker) == NativeMethods.S_OK)
                        {
                            _CancelAutoPlayCookie = objTbl.Register(NativeMethods.ROTFLAGS_REGISTRATIONKEEPSALIVE, this, vbMoniker);
                        }
                    }
                    ///
                    /// Non-Volume-based AutoPlay Cancelation
                    ///

                    if (_HWEventHandlerCookie == 0)
                    {
                        // Create the non-volume-based moniker that we'll put in the ROT
                        System.Runtime.InteropServices.ComTypes.IMoniker nvbMoniker;
                        if (NativeMethods.CreateHardwareEventMoniker(ref _CLSID_HWEventHandler, "MTPMediaPlayerArrival", out nvbMoniker) == NativeMethods.S_OK)
                        {
                            _HWEventHandlerCookie = objTbl.Register(NativeMethods.ROTFLAGS_REGISTRATIONKEEPSALIVE | NativeMethods.ROTFLAGS_ALLOWANYCLIENT, this, nvbMoniker);
                        }

                        if (_HWEventHandlerCookie != 0)
                        {
                            // Add our do-nothing handler to the list of MTPMediaPlayerArrival handlers.
                            // Note this will have to be removed when we unregister for AutoPlay cancelation so the
                            // do-nothing handler does not show up as a choice in the AutoPlay Dialog.
                            Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\EventHandlers\MTPMediaPlayerArrival", "SgtlMtpAutoPlayHandler", String.Empty);
                        }
                    }
                } // if (NativeMethods.GetRunningObjectTable(0, out objTbl) == NativeMethods.S_OK)
            }

            if (_CancelAutoPlayCookie != 0 && _HWEventHandlerCookie != 0)
            {
                _CancelAutoPlay += capDelegate;
                return(true);
            }
            else
            {
                return(false); // false means failed
            }
        }