Skip to content
Permalink
Browse files

Replaced the old VirtualKD installer with the newer VirtualBox integr…

…ation tool
  • Loading branch information
sysprogs committed Sep 30, 2015
1 parent 52a690d commit 49fd47a8bd29f2795d81329acec20aade1995662
Showing with 551 additions and 2,925 deletions.
  1. +8 −0 VirtualBoxIntegration/App.xaml
  2. +88 −0 VirtualBoxIntegration/App.xaml.cs
  3. +37 −0 VirtualBoxIntegration/MainWindow.xaml
  4. +189 −0 VirtualBoxIntegration/MainWindow.xaml.cs
  5. +55 −0 VirtualBoxIntegration/Properties/AssemblyInfo.cs
  6. +27 −19 {VirtualKDSetup → VirtualBoxIntegration}/Properties/Resources.Designer.cs
  7. 0 {VirtualKDSetup → VirtualBoxIntegration}/Properties/Resources.resx
  8. +14 −10 {VirtualKDSetup → VirtualBoxIntegration}/Properties/Settings.Designer.cs
  9. +7 −0 VirtualBoxIntegration/Properties/Settings.settings
  10. +113 −0 VirtualBoxIntegration/VirtualBoxIntegration.csproj
  11. +13 −26 VirtualKD.sln
  12. +0 −137 VirtualKDSetup/BatchBuildForm.Designer.cs
  13. +0 −90 VirtualKDSetup/BatchBuildForm.cs
  14. +0 −120 VirtualKDSetup/BatchBuildForm.resx
  15. +0 −114 VirtualKDSetup/DownloadProgressForm.Designer.cs
  16. +0 −237 VirtualKDSetup/DownloadProgressForm.cs
  17. +0 −120 VirtualKDSetup/DownloadProgressForm.resx
  18. BIN VirtualKDSetup/ICSharpCode.SharpZipLib.dll
  19. +0 −169 VirtualKDSetup/MainForm.Designer.cs
  20. +0 −134 VirtualKDSetup/MainForm.cs
  21. +0 −120 VirtualKDSetup/MainForm.resx
  22. +0 −36 VirtualKDSetup/NewVBoxClient.cs
  23. +0 −23 VirtualKDSetup/Program.cs
  24. +0 −36 VirtualKDSetup/Properties/AssemblyInfo.cs
  25. +0 −7 VirtualKDSetup/Properties/Settings.settings
  26. +0 −177 VirtualKDSetup/VBoxBuildForm.Designer.cs
  27. +0 −263 VirtualKDSetup/VBoxBuildForm.cs
  28. +0 −123 VirtualKDSetup/VBoxBuildForm.resx
  29. +0 −241 VirtualKDSetup/VirtualBoxClient.cs
  30. +0 −304 VirtualKDSetup/VirtualBoxIntegrationForm.Designer.cs
  31. +0 −118 VirtualKDSetup/VirtualBoxIntegrationForm.cs
  32. +0 −139 VirtualKDSetup/VirtualBoxIntegrationForm.resx
  33. +0 −142 VirtualKDSetup/VirtualKDSetup.csproj
  34. +0 −20 VirtualKDSetup/VirtualKDSetup.sln
@@ -0,0 +1,8 @@
<Application x:Class="VirtualBoxIntegration.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Windows;

namespace VirtualBoxIntegration
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool lpSystemInfo);

public static bool Is64Bit()
{
try
{
if (IntPtr.Size == 8)
return true;
bool retVal;
IsWow64Process(Process.GetCurrentProcess().Handle, out retVal);
return retVal;
}
catch (System.Exception)
{
return false;
}
}

internal static bool IsVirtualBoxRunning()
{
return Process.GetProcesses().Where(p => StringComparer.InvariantCultureIgnoreCase.Compare(Path.GetFileNameWithoutExtension(p.ProcessName), "VirtualBox") == 0).Count() != 0;
}

static bool IsSystemOwned(string fn)
{
return (File.GetAccessControl(fn).GetOwner(typeof(SecurityIdentifier)) as SecurityIdentifier) == new SecurityIdentifier(WellKnownSidType.LocalSystemSid, null);
}

static void ChownToSystem(string fn)
{
if (!IsSystemOwned(fn))
RunAsAdmin("icacls", "\"" + fn + "\" /setowner system", true);
}


internal static void CheckKDClientPermissions(string myDir)
{
if (!IsSystemOwned(Path.Combine(myDir, "kdclient64.dll")) || !IsSystemOwned(Path.Combine(myDir, "kdclient.dll")))
{
if (MessageBox.Show("VirtualBox won't load KDCLIENT.DLL/KDCLIENT64.DLL unless it is owned by the System acoount. Do you want to fix it now?", "VirtualBoxIntegration", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
ChownToSystem(Path.Combine(myDir, "kdclient64.dll"));
ChownToSystem(Path.Combine(myDir, "kdclient.dll"));
}
}
}

public static void RunAsAdmin(string cmd, string args, bool wait = false)
{
try
{
var p = new Process();
p.StartInfo.FileName = cmd;
p.StartInfo.Arguments = args;
if (System.Environment.OSVersion.Version.Major >= 6)
p.StartInfo.Verb = "runas";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
if (wait)
p.WaitForExit();
}
catch
{

}
}
}
}
@@ -0,0 +1,37 @@
<Window x:Class="VirtualBoxIntegration.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="VirtualBox Integration" Height="350" Width="525">
<Grid>
<Label Content="Detected VirtualBox version:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
<Label x:Name="lblVersion" Content="unknown" HorizontalAlignment="Left" Margin="174,10,0,0" VerticalAlignment="Top"/>
<Label Content="Virtual machines:" HorizontalAlignment="Left" Margin="10,36,0,0" VerticalAlignment="Top"/>
<TextBlock Margin="10,283,274,10">

<Hyperlink NavigateUri="http://virtualkd.sysprogs.org/tutorials/install/" RequestNavigate="Hyperlink_RequestNavigate" >
Open VirtualKD quick start tutorial
</Hyperlink>
</TextBlock>
<Button Content="Refresh" Margin="0,0,10,10" Click="Button_Click" Height="22" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75"/>
<ListView Margin="10,67,10,37" ItemsSource="{Binding Machines}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="VirtualKD integration" DisplayMemberBinding="{Binding Status}"/>
<GridViewColumn Header="">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Command="{Binding EnableCommand}" Content="Enable"/>
<Button Command="{Binding DisableCommand}" Content="Disable"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button Content="Launch VirtualBox" Margin="0,0,90,10" Height="22" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="116" Click="Button_Click_1"/>

</Grid>
</Window>
@@ -0,0 +1,189 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace VirtualBoxIntegration
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public List<MachineWrapper> Machines {get;set;}

static string MyDir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

public MainWindow()
{
InitializeComponent();

this.DataContext = this;

_VirtualBox = new VirtualBox.VirtualBox();
lblVersion.Content = _VirtualBox.Version;

var is64Bit = App.Is64Bit();
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Oracle\VirtualBox");
string dir;
if (key != null)
dir = key.GetValue("InstallDir") as string;
else
dir = Environment.GetEnvironmentVariable(is64Bit ? "ProgramW6432" : "ProgramFiles") + "\\Oracle\\VirtualBox";
if (dir == null || !Directory.Exists(dir))
throw new Exception("VirtualBox directory not found");
VirtualBoxPath = dir.TrimEnd('\\');

Refresh();
}
const string VirtualKDConfigEntry = "VBoxInternal/Devices/VirtualKD/0/Config/Path";


public class MachineWrapper : INotifyPropertyChanged
{
private VirtualBox.IMachine _Machine;
private MainWindow _Window;

public MachineWrapper(MainWindow w, VirtualBox.IMachine m)
{
_Machine = m;
_Window = w;
EnableCommand = new EnableDisableCommand(this, true);
DisableCommand = new EnableDisableCommand(this, false);
}

public string Name
{
get
{
return _Machine.Name;
}
}


public class EnableDisableCommand : ICommand
{
private MachineWrapper _Wrapper;
public EnableDisableCommand(MachineWrapper wrp, bool enable)
{
_Enable = enable;
_Wrapper = wrp;
}

public bool CanExecute(object parameter)
{
return _Enable != _Wrapper.Integrated;
}

public event EventHandler CanExecuteChanged;
private bool _Enable;

public void Execute(object parameter)
{
if (App.IsVirtualBoxRunning())
{
MessageBox.Show("Please close ALL VirtualBox instances before changing the settings.", "VirtualBoxIntegration", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

ProcessStartInfo info = new ProcessStartInfo(System.IO.Path.Combine(_Wrapper._Window.VirtualBoxPath, "VBoxManage.exe"), "setextradata \"" + _Wrapper._Machine.Name + "\" " + VirtualKDConfigEntry) { CreateNoWindow = true, UseShellExecute = false };
if (_Enable)
{
info.Arguments += " \"" + MyDir + "\"";
App.CheckKDClientPermissions(MyDir);
}

var proc = Process.Start(info);
proc.WaitForExit();
if (proc.ExitCode != 0)
{
MessageBox.Show("Failed to update machine properties", "VirtualBoxIntegration", MessageBoxButton.OK, MessageBoxImage.Error);
}

_Wrapper.InvalidateStatus();
}

public void InvalidateStatus()
{
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
}

public EnableDisableCommand EnableCommand { get; set; }
public EnableDisableCommand DisableCommand { get; set; }

public bool Integrated
{
get
{
return _Machine.GetExtraData(VirtualKDConfigEntry) == MyDir;
}
}

public string Status
{
get
{
return Integrated ? "Enabled" : "Disabled";
}
}


internal void InvalidateStatus()
{
EnableCommand.InvalidateStatus();
DisableCommand.InvalidateStatus();
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Status"));
PropertyChanged(this, new PropertyChangedEventArgs("Integrated"));
}
}

public event PropertyChangedEventHandler PropertyChanged;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
Refresh();
}

private void Refresh()
{
Machines = _VirtualBox.Machines.Cast<VirtualBox.IMachine>().Select(m => new MachineWrapper(this, m)).ToList();
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Machines"));
}


public event PropertyChangedEventHandler PropertyChanged;
private VirtualBox.VirtualBox _VirtualBox;
private string VirtualBoxPath;

private void Button_Click_1(object sender, RoutedEventArgs e)
{
Process.Start(System.IO.Path.Combine(VirtualBoxPath, "VirtualBox.exe"));
}

private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
}
}
@@ -0,0 +1,55 @@
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("VirtualBoxIntegration")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("VirtualBoxIntegration")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

//In order to begin building localizable applications, set
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
//inside a <PropertyGroup>. For example, if you are using US english
//in your source files, set the <UICulture> to en-US. Then uncomment
//the NeutralResourceLanguage attribute below. Update the "en-US" in
//the line below to match the UICulture setting in the project file.

//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]


[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]


// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

0 comments on commit 49fd47a

Please sign in to comment.