171 lines
5.9 KiB
C#
171 lines
5.9 KiB
C#
using System;
|
|
using System.Windows.Forms;
|
|
using System.IO;
|
|
using System.Diagnostics;
|
|
using System.Threading;
|
|
using System.ServiceProcess;
|
|
using Microsoft.Win32;
|
|
using System.Linq;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace VmwareLauncher
|
|
{
|
|
public partial class Startup : Form
|
|
{
|
|
readonly string[] vmServices = new string[] { "VMnetDHCP", "VMUSBArbService", "VMware NAT Service", "VMwareHostd", "VMAuthdService" }; //Array of services to close
|
|
readonly string vmwarePath = (string) Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\VMware, Inc.\VMware Workstation", "InstallPath", null) + "vmware.exe";
|
|
bool canRun = true;
|
|
|
|
BackgroundWorker PerformInitialChecks = new BackgroundWorker();
|
|
BackgroundWorker RunProcess = new BackgroundWorker();
|
|
public Startup()
|
|
{
|
|
InitializeComponent();
|
|
|
|
PerformInitialChecks.DoWork += PerformInitialChecks_DoWork;
|
|
PerformInitialChecks.RunWorkerCompleted += PerformInitialChecks_RunWorkerCompleted;
|
|
PerformInitialChecks.RunWorkerAsync();
|
|
|
|
RunProcess.DoWork += RunProcess_DoWork;
|
|
}
|
|
|
|
private void PerformInitialChecks_DoWork(object sender, DoWorkEventArgs e)
|
|
{
|
|
// Check Vmware Workstation has been installed.
|
|
if (vmwarePath == null & !File.Exists(vmwarePath))
|
|
{
|
|
ErrorHandler.ShowError(ErrorHandler.ErrorCode.VmwareNotInstalled);
|
|
Application.Exit();
|
|
}
|
|
|
|
// Run standalone instance of VMware if VML is already running.
|
|
Process[] proc = Process.GetProcessesByName("vmwarelauncher");
|
|
if (proc.Count() != 1)
|
|
{
|
|
//ErrorHandler.ShowError(ErrorHandler.ErrorCode.TooManyVMLInstances);
|
|
Process process = Process.Start(vmwarePath);
|
|
Environment.Exit(0);
|
|
}
|
|
|
|
// Determine if VML has ever run before;
|
|
string vmlPath = @"HKEY_LOCAL_MACHINE\SOFTWARE\VmwareLauncher";
|
|
string vmlKey = "HasBeenSetup";
|
|
|
|
if (Registry.GetValue(vmlPath, vmlKey, null) == null)
|
|
{
|
|
Registry.SetValue(vmlPath, vmlKey, 1);
|
|
MessageBox.Show(@"Welcome to VMwareLauncher! This utility is designed to automatically start and stop VMware Workstation services " +
|
|
"as needed when running the application. Since you've never run this tool before, your services will be configured " +
|
|
"automatically.",
|
|
"VMwareLauncher",
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Information);
|
|
}
|
|
|
|
ServiceConfig.ForceServicesManual(vmServices);
|
|
LoadServices();
|
|
}
|
|
|
|
private void PerformInitialChecks_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
|
|
{
|
|
RunProcess.RunWorkerAsync();
|
|
}
|
|
|
|
private void RunProcess_DoWork(object sender, DoWorkEventArgs e)
|
|
{
|
|
if (canRun)
|
|
{
|
|
Process process = Process.Start(vmwarePath);
|
|
MinimizeToTray();
|
|
|
|
bool isVmwareRunning = true;
|
|
while (isVmwareRunning)
|
|
{
|
|
Thread.Sleep(5000);
|
|
|
|
isVmwareRunning = false; // Default value.
|
|
foreach (Process instance in Process.GetProcessesByName("vmware"))
|
|
isVmwareRunning = true;
|
|
}
|
|
StopServices();
|
|
}
|
|
}
|
|
|
|
private void LoadServices()
|
|
{
|
|
for (int i = 0; i < vmServices.Length; i++)
|
|
{
|
|
foreach (string service in vmServices)
|
|
{
|
|
ServiceController sc = new ServiceController(service, Environment.MachineName.ToString());
|
|
|
|
try
|
|
{
|
|
|
|
if (sc.Status.Equals(ServiceControllerStatus.Stopped))
|
|
{
|
|
sc.Start();
|
|
sc.WaitForStatus(ServiceControllerStatus.Running);
|
|
}
|
|
if (sc.Status.Equals(ServiceControllerStatus.Running))
|
|
{
|
|
StepProgBar();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
ErrorHandler.ShowError(ErrorHandler.ErrorCode.NoAdminRights);
|
|
canRun = false;
|
|
Application.Exit();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void StopServices()
|
|
{
|
|
try
|
|
{
|
|
for (int i = 0; i < vmServices.Length; i++)
|
|
{
|
|
ServiceController service = new ServiceController(vmServices[i], Environment.MachineName.ToString());
|
|
|
|
if (service.Status.Equals(ServiceControllerStatus.Running))
|
|
{
|
|
service.Stop();
|
|
service.WaitForStatus(ServiceControllerStatus.Stopped);
|
|
}
|
|
}
|
|
|
|
Process[] proc = Process.GetProcessesByName("vmware-tray");
|
|
if (proc.Length > 1) { proc[0].Kill(); }
|
|
|
|
Application.Exit();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
void StepProgBar()
|
|
{
|
|
float stepPercentage = vmServices.Length / 100f * vmServices.Length * 100f;
|
|
|
|
progressBar.Increment(25);
|
|
progressBar.PerformStep();
|
|
Thread.Sleep(250);
|
|
}
|
|
|
|
private void MinimizeToTray()
|
|
{
|
|
Visible = false;
|
|
notifyIcon.Visible = true;
|
|
notifyIcon.ShowBalloonTip(10);
|
|
}
|
|
}
|
|
}
|