vixclass.cs//定义开机、关机等函数
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using VixCOM; namespace dome { class vixclass { public VixCOM.IVixLib IvixLib; public ulong m_vixError; public VixCOM.IHost m_hostHandle; public VixCOM.IVM m_vmHandle; // public VixCOM.IJob jobHandle; public vixclass() { IvixLib = new VixCOM.VixLibClass(); m_vixError=0; m_hostHandle = null; m_vmHandle = null; //jobHandle = null; } public ulong GetError() { return m_vixError; } /// <summary> /// 创建链接 /// </summary> public bool Connect(string _hostname,string _username, string _password) { int hostType = VixCOM.Constants.VIX_SERVICEPROVIDER_VMWARE_WORKSTATION; int vixVersion = VixCOM.Constants.VIX_API_VERSION; vixVersion = -1; int[] propertyIds = new int[1] { VixCOM.Constants.VIX_PROPERTY_JOB_RESULT_HANDLE }; object results = new object(); IJob jobHandle = IvixLib.Connect(vixVersion, hostType, _hostname, 0, _username, _password, 0, null, null); //jobHandle = IvixLib.Connect(vixVersion, hostType, hostname, 0, user, password, 0, null, null); m_vixError = jobHandle.Wait(propertyIds, ref results); if (m_vixError == VixCOM.Constants.VIX_OK) { object[] objectArray = (object[])results; m_hostHandle = (VixCOM.IHost)objectArray[0]; return true; } return false; } /// <summary> ///打开vmxPath的虚拟机 /// </summary> public bool OpenVm(string vmxPath) { IJob jobHandle = m_hostHandle.OpenVM(vmxPath, null); int[] propertyIds = new int[1] { VixCOM.Constants.VIX_PROPERTY_JOB_RESULT_HANDLE }; object results = new object(); m_vixError = jobHandle.Wait(propertyIds, ref results); if (m_vixError == VixCOM.Constants.VIX_OK) { object[] objectArray = (object[])results; m_vmHandle = (VixCOM.IVM)objectArray[0]; return true; } return false; } /// <summary> /// 启动虚拟机 /// </summary> public bool PowerOn() { IJob jobHandle = m_vmHandle.PowerOn(VixCOM.Constants.VIX_VMPOWEROP_LAUNCH_GUI, null, null); m_vixError = jobHandle.WaitWithoutResults(); if (m_vixError == VixCOM.Constants.VIX_OK) { // jobHandle = m_vmHandle.WaitForToolsInGuest(300, null); m_vixError = jobHandle.WaitWithoutResults(); } return (m_vixError == VixCOM.Constants.VIX_OK); } /// <summary> /// 关闭虚拟机 /// </summary> public bool PowerOff() { IJob jobHandle = m_vmHandle.PowerOff(VixCOM.Constants.VIX_VMPOWEROP_NORMAL, null); m_vixError = jobHandle.WaitWithoutResults(); return (m_vixError == VixCOM.Constants.VIX_OK); } /// <summary> /// 重启虚拟机 /// </summary> public bool Restart() { IJob jobHandle = m_vmHandle.Reset(VixCOM.Constants.VIX_VMPOWEROP_NORMAL, null); m_vixError = jobHandle.WaitWithoutResults(); return (m_vixError == VixCOM.Constants.VIX_OK); } } }
Form1.cs//主窗体,
textbox1//记录选择的虚拟机的路径
btnselect//选择路径
btnstart//打开虚拟机
btnclose//关闭虚拟机
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace dome { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnselect_Click(object sender, EventArgs e) { OpenFileDialog loSaveFile = new OpenFileDialog(); loSaveFile.Filter = ".vmx文件(*.vmx)|*.vmx"; if (loSaveFile.ShowDialog() == DialogResult.OK) { this.textBox1.Text = loSaveFile.FileName; } } private void btnstart_Click(object sender, EventArgs e) { try { vixclass vix = new vixclass(); string vmxpath = textBox1.Text; vix.Connect(null, "Administrator", null); vix.OpenVm(@vmxpath); vix.PowerOn(); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } } private void btnclose_Click(object sender, EventArgs e) { try { vixclass vix = new vixclass(); string vmxpath = textBox1.Text; vix.Connect(null , "Administrator", null); vix.OpenVm(@vmxpath); vix.PowerOff(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void Form1_Load(object sender, EventArgs e) { } } }
注:添加引用:VixCOM.DLL