Example #1
0
        private void CheckForDevices(object sender, EventArgs e)
        {
            try
            {
                var regex = new Regex(_DeviceIDRegex);

                using (var set = new DeviceInformationSet())
                {
                    var devices = set.GetAllDevices().Where(d => regex.IsMatch(d.HardwareID)).ToArray();
                    if (devices.Length == 1)
                    {
                        Close();
                    }
                }
            }
            catch
            {
            }
        }
        private void FixDriversIfNeeded()
        {
            var regex = new Regex(DeviceConnectionRequestWindow.GetDeviceIDRegex());

            var paragraph = new Paragraph();

            paragraph.Inlines.Add(new Run("Checking whether the driver needs updating...\r\n")
            {
                Foreground = Brushes.DarkBlue
            });
            txtLog.Document.Blocks.Add(paragraph);

            for (int pass = 0; pass < 2; pass++)
            {
                using (var set = new DeviceInformationSet())
                {
                    var devices = set.GetAllDevices().Where(d => regex.IsMatch(d.HardwareID)).ToArray();
                    if (devices.Length != 1)
                    {
                        return;
                    }

                    var device = devices[0];
                    paragraph.Inlines.Add(new Run($"Found {device.DeviceID}...\r\n")
                    {
                        Foreground = Brushes.DarkBlue
                    });

                    if (device.UserFriendlyName == "STM32 Bootloader")
                    {
                        return;
                    }

                    Controller.StatusText = "Installing STM32 bootloader drivers...";

                    paragraph.Inlines.Add(new Run($"Bootloader driver not installed for {device.UserFriendlyName}\r\n")
                    {
                        Foreground = Brushes.DarkBlue
                    });

                    var driver = set.GetCompatibleDrivers(device).FirstOrDefault(d => d.Description == "STM32 Bootloader");
                    if (driver.Description == null)
                    {
                        if (pass == 0)
                        {
                            string driverDir = System.IO.Path.Combine(Controller.DataDirectory, "Driver");
                            paragraph.Inlines.Add(new Run($"Copying drivers from {driverDir}...\r\n")
                            {
                                Foreground = Brushes.DarkBlue
                            });

                            if (!DeviceInformationSet.SetupCopyOEMInf(System.IO.Path.Combine(driverDir, "STM32Bootloader.inf"),
                                                                      driverDir,
                                                                      DeviceInformationSet.OemSourceMediaType.SPOST_PATH,
                                                                      DeviceInformationSet.OemCopyStyle.Default,
                                                                      null,
                                                                      0,
                                                                      IntPtr.Zero,
                                                                      null))
                            {
                                throw new LastWin32ErrorException("Failed to install the STM32 Bootloader driver");
                            }

                            continue;
                        }
                        else
                        {
                            throw new Exception("STM32 Bootloader Driver Not Found");
                        }
                    }

                    paragraph.Inlines.Add(new Run($"Installing the STM32 bootloader driver...\r\n")
                    {
                        Foreground = Brushes.DarkBlue
                    });
                    set.InstallSpecificDriverForDevice(device, driver, IntPtr.Zero);
                }
            }
        }