Пример #1
0
        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="WinUsbManager"/>,
        /// and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing"><b>true</b> to release both managed and
        /// unmanaged resources; <b>false</b> to release only unmanaged resources.</param>
        /// <remarks>
        /// This method is called by the public <see cref="Dispose()">Dispose</see>
        /// method and the <see cref="Finalize">Finalize</see> method.
        /// <see cref="Dispose()">Dispose</see> invokes the
        /// protected <b>Dispose</b> method with the <paramref name="disposing"/>
        /// parameter set to <b>true</b>. <see cref="Finalize">Finalize</see> invokes
        /// <b>Dispose</b> with <paramref name="disposing"/> set to <b>false</b>.
        /// </remarks>
        protected virtual void Dispose(bool disposing)
        {
            if (m_hNotify != IntPtr.Zero)
            {
                DeviceManagement.UnregisterDeviceNotification(m_hNotify);
                m_hNotify = IntPtr.Zero;

                if (disposing)
                {
                    foreach (UsbDevice dev in m_collUsbRW)
                    {
                        dev.Dispose();
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WinUsbManager"/> class.
        /// </summary>
        /// <param name="guid">GUID specified in the device driver installation file (INF).</param>
        /// <remarks>
        /// <para>
        /// Each USB device that uses the generic USB device driver WinUSB.sys
        /// is assigned a GUID in the device driver installation file (INF).
        /// To create an instance of <see cref="WinUsbManager"/>, this GUID
        /// must be passed to the constructor.</para>
        /// <para>
        /// One instance of <see cref="WinUsbManager"/> handles all attached
        /// USB devices with the assigned GUID. To handle another USB device with
        /// a different GUID, another instance of <see cref="WinUsbManager"/>
        /// must be created.</para>
        /// </remarks>
        public WinUsbManager(Guid guid)
        {
            // Set up notification of attach/remove
            m_ctlNotify = new DeviceNotifyControl();
            m_hNotify   = DeviceManagement.RegisterForDeviceNotifications(m_ctlNotify.Handle, guid);
            if (m_hNotify != IntPtr.Zero)
            {
                m_ctlNotify.DeviceChangeEvent += DeviceChangeEventHandler;

                m_collUsbRW     = new Collection <UsbDevice>();
                m_collUsbDevice = new UsbDeviceCollection(m_collUsbRW);

                // See if any matching devices are already attached
                foreach (string strDeviceName in DeviceManagement.GetDeviceNames(guid))
                {
                    m_collUsbRW.Add(new UsbDevice(strDeviceName));
                }
            }
        }