Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WindowsDeviceNotifier.cs « DeviceNotify « LibWinUsb - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 275f0c2a2ab9d0d81dbf0b3662c4d56be21dc880 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright © 2006-2010 Travis Robinson. All rights reserved.
// 
// website: http://sourceforge.net/projects/libusbdotnet
// e-mail:  libusbdotnet@gmail.com
// 
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or 
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
// 
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. or 
// visit www.gnu.org.
// 
// 
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using LibUsbDotNet.DeviceNotify.Internal;

namespace LibUsbDotNet.DeviceNotify
{
    /// <summary>
    /// Notifies an application of a change to the hardware Configuration of a device or 
    /// the computer. See <see cref="IDeviceNotifier"/> or <see cref="DeviceNotifier.OpenDeviceNotifier"/> interface for more information
    /// </summary>
    /// <remarks>
    /// This is the windows implementation of the device notifier.
    /// </remarks>
    public class WindowsDeviceNotifier : IDeviceNotifier
    {
        private readonly DevBroadcastDeviceinterface mDevInterface = new DevBroadcastDeviceinterface(new Guid("A5DCBF10-6530-11D2-901F-00C04FB951ED"));

        private SafeNotifyHandle mDevInterfaceHandle;
        private bool mEnabled = true;
        private DevNotifyNativeWindow mNotifyWindow;

        ///<summary>
        /// Creates an instance of the <see cref="WindowsDeviceNotifier"/> class.
        /// See the <see cref="IDeviceNotifier"/> interface or <see cref="DeviceNotifier.OpenDeviceNotifier"/> method for more information
        ///</summary>
        ///<remarks>
        ///To make your code platform-independent use the <see cref="DeviceNotifier.OpenDeviceNotifier"/> method for creating instances.
        ///</remarks>
        public WindowsDeviceNotifier() { mNotifyWindow = new DevNotifyNativeWindow(OnHandleChange, OnDeviceChange); }

        #region IDeviceNotifier Members

        ///<summary>
        /// Enables/Disables notification events.
        ///</summary>
        public bool Enabled
        {
            get { return mEnabled; }
            set { mEnabled = value; }
        }


        /// <summary>
        /// Main Notify event for all device notifications.
        /// </summary>
        public event EventHandler<DeviceNotifyEventArgs> OnDeviceNotify;

        #endregion

        [DllImport("user32.dll", SetLastError = true, EntryPoint = "RegisterDeviceNotificationA", CharSet = CharSet.Ansi)]
        private static extern SafeNotifyHandle RegisterDeviceNotification(IntPtr hRecipient,
                                                                          [MarshalAs(UnmanagedType.AsAny), In] object notificationFilter,
                                                                          int flags);

        [DllImport("user32.dll", SetLastError = true)]
        internal static extern bool UnregisterDeviceNotification(IntPtr handle);

        ///<summary>
        ///Releases the resources associated with this window. 
        ///</summary>
        ///
        ~WindowsDeviceNotifier()
        {
            if (mNotifyWindow != null) mNotifyWindow.DestroyHandle();
            mNotifyWindow = null;

            if (mDevInterfaceHandle != null) mDevInterfaceHandle.Dispose();
            mDevInterfaceHandle = null;
        }

        internal bool RegisterDeviceInterface(IntPtr windowHandle)
        {
            if (mDevInterfaceHandle != null)
            {
                mDevInterfaceHandle.Dispose();
                mDevInterfaceHandle = null;
            }
            if (windowHandle != IntPtr.Zero)
            {
                mDevInterfaceHandle = RegisterDeviceNotification(windowHandle, mDevInterface, 0);
                if (mDevInterfaceHandle != null && !mDevInterfaceHandle.IsInvalid)
                    return true;
                return false;
            }
            return false;
        }


        private void OnDeviceChange(ref Message m)
        {
            if (!mEnabled) return;
            if (m.LParam.ToInt32() != 0)
            {
                EventHandler<DeviceNotifyEventArgs> temp = OnDeviceNotify;
                if (!ReferenceEquals(temp, null))
                {
                    DeviceNotifyEventArgs args;
                    DevBroadcastHdr hdr = new DevBroadcastHdr();
                    Marshal.PtrToStructure(m.LParam, hdr);
                    switch (hdr.DeviceType)
                    {
                        case DeviceType.Port:
                        case DeviceType.Volume:
                        case DeviceType.DeviceInterface:
                            args = new WindowsDeviceNotifyEventArgs(hdr, m.LParam, (EventType) m.WParam.ToInt32());
                            break;
                        default:
                            args = null;
                            break;
                    }

                    if (!ReferenceEquals(args, null)) temp(this, args);
                }
            }
        }

        private void OnHandleChange(IntPtr newWindowHandle)
        {
            bool bSuccess = RegisterDeviceInterface(newWindowHandle);
            Debug.Print("RegisterDeviceInterface:" + bSuccess);
        }
    }
}