protected override void Dispose(bool disposing)
        {
            // Nothing will be done differently based on whether we are disposing vs. finalizing.
            lock (this)
            {
                if (m_handle != null && !m_handle.IsInvalid)
                {
                    if (disposing)
                    {
                        CancelPendingIO();
                    }

                    m_winUsbDevice.CloseDeviceHandle();

                    //if (m_winUsbDevice.DeviceInfo.winUsbHandle != IntPtr.Zero)
                    //{
                    //    IntPtr h = m_winUsbDevice.DeviceInfo.winUsbHandle;
                    //    m_winUsbDevice.DeviceInfo.winUsbHandle = IntPtr.Zero;
                    //    WinUsbDevice.WinUsb_Free(h);
                    //}

                    m_handle.SetHandleAsInvalid();
                }
            }

            base.Dispose(disposing);
        }
        private static void FindMyDevice()
        {
            var devicePathName = "";

            if (!(DeviceDetected))
            {
                //  This GUID must match the GUID in the device's INF file.
                //  Convert the device interface GUID String to a GUID object:

                if (_deviceManager.FindDeviceFromGuid(WinUsbDemoGuid, ref devicePathName))
                {
                    var success = _usbDevice.GetDeviceHandle(devicePathName);
                    if (success)
                    {
                        DeviceDetected = true;

                        // Save DevicePathName so OnDeviceChange() knows which name is my device.
                        _devicePathName = devicePathName;
                    }
                    else
                    {
                        // There was a problem in retrieving the information.
                        DeviceDetected = false;
                        _usbDevice.CloseDeviceHandle();
                    }
                }
                if (DeviceDetected)
                {
                    _usbDevice.InitializeDevice();
                }
            }
        }
        protected override void Dispose(bool disposing)
        {
            // Nothing will be done differently based on whether we are disposing vs. finalizing.
            lock ( syncRW )
            {
                if (m_handle != null && !m_handle.IsInvalid)
                {
                    if (disposing)
                    {
                        CancelPendingIO( );
                    }

                    m_winUsbDevice.CloseDeviceHandle( );
                    m_handle.SetHandleAsInvalid( );
                }
            }

            base.Dispose(disposing);
        }
Exemple #4
0
        /// <summary>
        /// Taken from Jan Axelson's USB Bulk Transfer implementation.
        /// Finds the device if it exists, retrieves the handle to it. Subscribes the main
        /// form to receive notifications from
        /// the device. This function should only be called from the main code until the deivice is initially
        /// </summary>
        /// <returns></returns>
        public bool FindMyDevice()
        {
            Boolean deviceFound = false;
            String  devPathName = "";
            Boolean success     = false;

            isDeviceDetected = false;
            try
            {
                if (!(isDeviceDetected))
                {
                    //  Convert the device interface GUID String to a GUID object:
                    System.Guid winusb_device_guid =
                        new System.Guid(deviceGUID);

                    // Fill an array with the device path names of all attached devices with matching GUIDs.
                    deviceFound = deviceManager.FindDeviceFromGuid
                                      (winusb_device_guid,
                                      ref devPathName);

                    if (deviceFound == true)
                    {
                        success = device.GetDeviceHandle(devPathName);

                        if (success)
                        {
                            isDeviceDetected = true;

                            // Save DevicePathName so OnDeviceChange() knows which name is my device.
                            devicePathName = devPathName;
                        }
                        else
                        {
                            // There was a problem in retrieving the information.
                            isDeviceDetected = false;
                            device.CloseDeviceHandle();
                        }
                    }

                    if (isDeviceDetected)
                    {
                        // The device was detected.
                        // Register to receive notifications if the device is removed or attached.
                        if (deviceNotificationHandle == IntPtr.Zero)
                        {
                            success = deviceManager.RegisterForDeviceNotifications
                                          (devicePathName,
                                          formReference.Handle,
                                          winusb_device_guid,
                                          ref deviceNotificationHandle);
                        }

                        if (success)
                        {
                            device.InitializeDevice();
                        }
                    }
                }

                return(isDeviceDetected);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #5
0
        private static Boolean FindBootloader_WIN()
        {
            Boolean deviceFound;
            String  devicePathName = "";
            Boolean success;

            try
            {
                if (!(HID.BootloaderDetected))
                {
                    //  Convert the device interface GUID String to a GUID object:

                    System.Guid winUsbDemoGuid =
                        new System.Guid(variables.BOOTLOADER_GUID_STRING);

                    // Fill an array with the device path names of all attached devices with matching GUIDs.

                    deviceFound = BootloaderManagement.FindDeviceFromGuid
                                      (winUsbDemoGuid,
                                      ref devicePathName);
                    if (variables.debugme)
                    {
                        Console.WriteLine("DemoN - {0}", devicePathName);
                    }
                    if (deviceFound == true)
                    {
                        success = Bootloader.GetDeviceHandle(devicePathName);

                        if (success)
                        {
                            if (variables.debugme)
                            {
                                Console.WriteLine("Bootloader attached -85");
                            }
                            HID.BootloaderDetected = true;

                            // Save DevicePathName so OnDeviceChange() knows which name is my device.

                            BootloaderPathName = devicePathName;
                        }
                        else
                        {
                            // There was a problem in retrieving the information.

                            HID.BootloaderDetected = false;
                            Bootloader.CloseDeviceHandle();
                        }
                    }

                    if (HID.BootloaderDetected)
                    {
                        // The device was detected.
                        // Register to receive notifications if the device is removed or attached.
                        if (variables.debugme)
                        {
                            Console.WriteLine("Bootloader attached.-106");
                        }
                        success = BootloaderManagement.RegisterForDeviceNotifications
                                      (BootloaderPathName,
                                      MainForm.frmMy.Handle,
                                      winUsbDemoGuid,
                                      ref BootloaderNotificationHandle);
                        if (success)
                        {
                            if (Bootloader.InitializeDevice())
                            {
                                if (variables.debugme)
                                {
                                    Console.WriteLine("Bootloader attached.-117");
                                }
                            }
                            else
                            {
                                if (variables.debugme)
                                {
                                    Console.WriteLine("failed 121");
                                }
                            }
                        }
                        else
                        {
                            if (variables.debugme)
                            {
                                Console.WriteLine("failed 126");
                            }
                        }
                    }
                    else
                    {
                        if (variables.debugme)
                        {
                            Console.WriteLine("Bootloader not found.");
                        }
                    }
                }
                else
                {
                    if (variables.debugme)
                    {
                        Console.WriteLine("Bootloader attached.-128");
                    }
                }


                return(HID.BootloaderDetected);
            }
            catch (Exception ex)
            {
                if (variables.debugme)
                {
                    Console.WriteLine(ex.ToString());
                }
                return(false);
            }
        }