private async void InstallVBusOnClick()

in ScpDriverInstaller/MainWindow.xaml.cs [374:477]


        private async void InstallVBusOnClick(object sender, RoutedEventArgs e)
        {
            MainBusyIndicator.IsBusy = !MainBusyIndicator.IsBusy;
            var failed = false;
            var rebootRequired = false;

            await Task.Run(() =>
            {
                string devPath = string.Empty, instanceId = string.Empty;

                try
                {
                    var busInfPath = Path.Combine(
                        GlobalConfiguration.AppDirectory,
                        "ScpVBus",
                        Environment.Is64BitOperatingSystem ? "amd64" : "x86",
                        "ScpVBus.inf");
                    Log.DebugFormat("ScpVBus.inf path: {0}", busInfPath);

                    // check for existence of Scp VBus
                    if (!Devcon.Find(Settings.Default.VirtualBusClassGuid, ref devPath, ref instanceId))
                    {
                        MainBusyIndicator.SetContentThreadSafe(Properties.Resources.VirtualBusSetupAddingDriverStore);

                        // if not detected, install Inf-file in Windows Driver Store
                        if (Devcon.Install(busInfPath, ref rebootRequired))
                        {
                            Log.Info("Virtual Bus Driver pre-installed in Windows Driver Store successfully");

                            MainBusyIndicator.SetContentThreadSafe(Properties.Resources.VirtualBusSetupCreating);

                            // create pseudo-device so the bus driver can attach to it later
                            if (Devcon.Create("System", new Guid("{4D36E97D-E325-11CE-BFC1-08002BE10318}"),
                                "root\\ScpVBus\0\0"))
                            {
                                Log.Info("Virtual Bus Created");
                            }
                            else
                            {
                                Log.Fatal("Virtual Bus Device creation failed");
                                failed = true;
                            }
                        }
                        else
                        {
                            Log.FatalFormat("Virtual Bus Driver pre-installation failed with Win32 error {0}",
                                (uint)Marshal.GetLastWin32Error());
                            failed = true;
                        }
                    }

                    MainBusyIndicator.SetContentThreadSafe(Properties.Resources.VirtualBusSetupInstalling);

                    // install Virtual Bus driver
                    var result = Difx.Instance.Install(busInfPath,
                        DifxFlags.DRIVER_PACKAGE_ONLY_IF_DEVICE_PRESENT | DifxFlags.DRIVER_PACKAGE_FORCE,
                        out rebootRequired);

                    if (result != 0)
                    {
                        failed = true;
                    }
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Error during installation: {0}", ex);
                }
            });

            MainBusyIndicator.IsBusy = !MainBusyIndicator.IsBusy;

            // display error message
            if (failed)
            {
                ExtendedMessageBox.Show(this,
                    Properties.Resources.SetupFailedTitle,
                    Properties.Resources.SetupFailedInstructions,
                    Properties.Resources.SetupFailedContent,
                    string.Format(Properties.Resources.SetupFailedVerbose,
                        new Win32Exception(Marshal.GetLastWin32Error()), Marshal.GetLastWin32Error()),
                    Properties.Resources.SetupFailedFooter,
                    TaskDialogIcon.Error);
                return;
            }

            // display success message
            ExtendedMessageBox.Show(this,
                Properties.Resources.SetupSuccessTitle,
                Properties.Resources.VirtualBusSetupSuccessInstruction,
                Properties.Resources.SetupSuccessContent,
                string.Empty,
                string.Empty,
                TaskDialogIcon.Information);

            // display reboot required message
            if (rebootRequired)
            {
                MessageBox.Show(this,
                    Properties.Resources.RebootRequiredContent,
                    Properties.Resources.RebootRequiredTitle,
                    MessageBoxButton.OK,
                    MessageBoxImage.Warning);
            }
        }