From 504c75692a997791c1ed89d2d40478b0476ca8e6 Mon Sep 17 00:00:00 2001 From: Johnny Shaw Date: Wed, 3 May 2023 19:05:37 -0600 Subject: [PATCH] better missing feature handling --- VMPlex/App.xaml.cs | 59 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/VMPlex/App.xaml.cs b/VMPlex/App.xaml.cs index 2010b72..f7eea39 100644 --- a/VMPlex/App.xaml.cs +++ b/VMPlex/App.xaml.cs @@ -30,25 +30,70 @@ static private void CheckCapability() // Before we show any other windows check that we can access the // management service, if we can't then exit. // - bool capable = false; + var state = GetVsmsState(); + if (state == VsmsState.NoPermission) + { + UI.MessageBox.Show( + System.Windows.MessageBoxImage.Error, + "Virtual System Management", + "VMPlex is unable to interact with the Virtual Machine Management Service. " + + "Please run as administrator or add your user to the Hyper-V Administrators group."); + Environment.Exit(0xdead); + } + else if (state == VsmsState.NotInstalled) + { + var res = UI.MessageBox.Show( + System.Windows.MessageBoxImage.Error, + "Virtual System Management", + "VMPlex is unable to interact with the Virtual Machine Management Service. " + + "Please ensure that Windows Hyper-V is enabled on this machine. " + + "Open Windows Optional Features now?", + MessageBoxButton.YesNo); + if (res == MessageBoxResult.Yes) + { + LaunchOptionalFeaturesDialog(); + } + Environment.Exit(0xdead); + } + } + + private enum VsmsState + { + Ok, + NoPermission, + NotInstalled, + } + + static private VsmsState GetVsmsState() + { try { var vsms = new WmiScope(@"root\virtualization\v2") .GetInstance(); - capable = (vsms != null); + return vsms != null ? VsmsState.Ok : VsmsState.NoPermission; } catch { + return VsmsState.NotInstalled; } + } - if (!capable) + static private void LaunchOptionalFeaturesDialog() + { + try + { + var p = new Process(); + p.StartInfo.FileName = "OptionalFeatures.exe"; + p.StartInfo.UseShellExecute = true; + p.Start(); + } + catch (Exception ex) { UI.MessageBox.Show( System.Windows.MessageBoxImage.Error, - "Virtual System Management", - "VMPlex is unable to interact with the Virtual Machine Management Service. " + - "Please run as administrator or add your user to the Hyper-V Administrators group."); - Environment.Exit(0xdead); + "VMPlex Workstation", + "Failed to launch OptionalFeatures.exe\n\n" + + ex.Message); } }