Skip to content

Commit

Permalink
fix packaging for hv integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jxy-s committed Oct 24, 2022
1 parent 3ae1914 commit 7a2b4c0
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 16 deletions.
1 change: 1 addition & 0 deletions VMPlex/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public partial class App : Application

protected override void OnStartup(StartupEventArgs e)
{
Utility.TryExtractHVIntegrate();
base.OnStartup(e);
Utility.CreateSelfJob();
}
Expand Down
13 changes: 12 additions & 1 deletion VMPlex/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,18 @@ public void OpenInEditor()
FileName = UserSettingsFile,
UseShellExecute = true
};
process.Start();
try
{
process.Start();
}
catch (Exception exc)
{
MessageBox.Show(
$"Failed to open settings file \"{UserSettingsFile}\"\n{exc.Message}",
"VMPlex Settings Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}

public Settings Get()
Expand Down
38 changes: 33 additions & 5 deletions VMPlex/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

using System;
using System.Windows;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO;
using Windows.Storage.Streams;

namespace VMPlex
{
Expand Down Expand Up @@ -128,15 +131,40 @@ static public void CreateSelfJob()
SetInformationJobObject(selfJobObject, JOBOBJECTINFOCLASS.ExtendedLimitInformation, jobInfoPtr, (uint)Marshal.SizeOf(jobInfo));
}

static public void LaunchHvintegrateInJob(string[] args)
{
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
private static string HVIntegrateFileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\hvintegrate.exe";

static public void LaunchHVIntegrateInJob(string[] args)
{
Process hvintegrate = new Process();
hvintegrate.StartInfo.FileName = path + "\\hvintegrate.exe";
hvintegrate.StartInfo.FileName = HVIntegrateFileName;
hvintegrate.StartInfo.Arguments = String.Join(" ", args);
hvintegrate.Start();
bool f = AssignProcessToJobObject(selfJobObject, hvintegrate.Handle);
_ = AssignProcessToJobObject(selfJobObject, hvintegrate.Handle);
}

static public void ExtractResource(string Name, string Path)
{
var assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream(Name);
if ((stream == null) || (stream.Length == 0))
{
throw new Exception($"Resource {Name} not found!");
}
var bytes = new byte[stream.Length];
stream.Read(bytes);
File.WriteAllBytes(Path, bytes);
}

static public void TryExtractHVIntegrate()
{
//
// If we fail to extract or use an older version, we will fail cleanly downstream.
// Try with both the assembly name (for publishing) and the root name for debug.
//
var name = Assembly.GetExecutingAssembly().GetName().Name + ".HVIntegrate";
try { ExtractResource(name, HVIntegrateFileName); } catch(Exception) { }
name = "HVIntegrate";
try { ExtractResource(name, HVIntegrateFileName); } catch(Exception) { }
}
}
}
11 changes: 8 additions & 3 deletions VMPlex/VMPlex.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<UseWindowsForms>true</UseWindowsForms>
<UseWPF>true</UseWPF>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<ApplicationManifest>app.manifest</ApplicationManifest>
<ApplicationIcon>Resources\VMPlex.ico</ApplicationIcon>
Expand Down Expand Up @@ -53,6 +54,10 @@
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\VMPlex.ico" />
<EmbeddedResource Include="$(OutputPath)hvintegrate.exe">
<LogicalName>HVIntegrate</LogicalName>
<Visible>false</Visible>
</EmbeddedResource>
<Reference Include="MSTSCLib">
<HintPath>..\MSTSCLib.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes>
Expand All @@ -67,9 +72,9 @@
</ItemGroup>
<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
<ItemGroup>
<DataModelFiles Include="$(OutputPath)..\hvintegrate.exe" />
<DataModelFiles Include="$(OutputPath)..\hvintegrate.pdb" />
<DependentFiles Include="$(OutputPath)hvintegrate.exe" />
<DependentFiles Include="$(OutputPath)hvintegrate.pdb" />
</ItemGroup>
<Copy SourceFiles="@(DataModelFiles)" DestinationFolder="$(PublishDir)" SkipUnchangedFiles="false" />
<Copy SourceFiles="@(DependentFiles)" DestinationFolder="$(PublishDir)" SkipUnchangedFiles="false" />
</Target>
</Project>
34 changes: 27 additions & 7 deletions VMPlex/VirtualMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public VirtualMachine(Msvm_ComputerSystem vm)

public static void OpenNewVmWizard()
{
Utility.LaunchHvintegrateInJob(new string[] { "av" });
TryLaunchHVIntegrateInJob(new string[] { "av" });
}

public void DeleteFromServer()
{
Utility.LaunchHvintegrateInJob(new string[] { "dv", Guid });
TryLaunchHVIntegrateInJob(new string[] { "dv", Guid });
}

public VmConfig GetVmUserSettings()
Expand Down Expand Up @@ -117,27 +117,35 @@ public void OpenDebugger()
Arguments = vm.DebuggerArguments,
UseShellExecute = true
};
process.Start();

try
{
process.Start();
}
catch (Exception exc)
{
Utility.ErrorPopup($"Failed to start debugger.\n{exc.Message}");
}
}

public void OpenSettingsDialog()
{
Utility.LaunchHvintegrateInJob(new string[] { "vm", Guid });
TryLaunchHVIntegrateInJob(new string[] { "vm", Guid });
}

public static void OpenSwitchManagerDialog()
{
Utility.LaunchHvintegrateInJob(new string[] { "vs" });
TryLaunchHVIntegrateInJob(new string[] { "vs" });
}

public static void OpenEditDiskWizard()
{
Utility.LaunchHvintegrateInJob(new string[] { "ed" });
TryLaunchHVIntegrateInJob(new string[] { "ed" });
}

public static void OpenHyperVSettingsDialog()
{
Utility.LaunchHvintegrateInJob(new string[] { "hv" });
TryLaunchHVIntegrateInJob(new string[] { "hv" });
}

public uint RequestStateChange(StateChange state)
Expand Down Expand Up @@ -350,6 +358,18 @@ public void UpdateSummaryInformation(ManagementBaseObject summary)
}
}

private static void TryLaunchHVIntegrateInJob(string[] args)
{
try
{
Utility.LaunchHVIntegrateInJob(args);
}
catch (Exception exc)
{
Utility.ErrorPopup($"Failed to start Hyper-V Integration\n{exc.Message}");
}
}

private Msvm_ComputerSystem Msvm { get; set; }
public VirtualMachine Self { get { return this; } }
public string Name { get; set; }
Expand Down

0 comments on commit 7a2b4c0

Please sign in to comment.