C#窗体实现打开关闭VM虚拟机

简介: vixclass.cs//定义开机、关机等函数 using System; using System.Collections.Generic; using System.Linq; using System.

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

遗失的拂晓
目录
相关文章
|
30天前
|
Java 数据库 C#
C#winforms实现windows窗体人脸识别
C#winforms实现windows窗体人脸识别
29 0
|
1月前
|
数据安全/隐私保护 虚拟化 Windows
如何在 VM 虚拟机中安装 Windows Server 2012 操作系统保姆级教程(附链接)
如何在 VM 虚拟机中安装 Windows Server 2012 操作系统保姆级教程(附链接)
60 0
|
1月前
|
数据安全/隐私保护 虚拟化 Windows
如何在 VM 虚拟机中安装 Windows 7 操作系统保姆级教程(附链接)
如何在 VM 虚拟机中安装 Windows 7 操作系统保姆级教程(附链接)
81 0
如何在 VM 虚拟机中安装 Windows 7 操作系统保姆级教程(附链接)
|
1月前
|
数据安全/隐私保护 虚拟化 Windows
如何在 VM 虚拟机中安装 Windows XP 操作系统保姆级教程(附链接)
如何在 VM 虚拟机中安装 Windows XP 操作系统保姆级教程(附链接)
109 0
|
1月前
|
Linux 网络安全 数据安全/隐私保护
如何在 VM 虚拟机中安装 CentOS Linux 9 操作系统保姆级教程(附链接)
如何在 VM 虚拟机中安装 CentOS Linux 9 操作系统保姆级教程(附链接)
144 0
|
1月前
|
安全 Linux 网络安全
如何在 VM 虚拟机中安装 Red Hat Enterprise Linux 9.3 操作系统保姆级教程(附链接)
如何在 VM 虚拟机中安装 Red Hat Enterprise Linux 9.3 操作系统保姆级教程(附链接)
89 0
|
5月前
VM 挂起虚拟机后 主机无法ping通虚拟机
VM 挂起虚拟机后 主机无法ping通虚拟机
58 0
|
6月前
|
关系型数据库 MySQL C#
C# winform 一个窗体需要调用自定义用户控件的控件名称
给用户控件ucQRCode增加属性: //二维码图片 private PictureBox _pictureBoxFSHLQrCode; public PictureBox PictureBoxFSHLQrCode {   get { return _pictureBoxFSHLQrCode; }   set { this.pictureBoxFSHLQrCode = value; } } 在Form1窗体直接调用即可: ucQRCode uQRCode=new ucQRCode(); ucQRCode.PictureBoxFSHLQrCode.属性= 要复制或传给用户控件上的控件的值
36 0
|
6月前
|
Windows
[闻缺陷则喜]win10家庭版vm打开虚拟机本机蓝屏
[闻缺陷则喜]win10家庭版vm打开虚拟机本机蓝屏
|
30天前
如何在vm虚拟机里面安装win10操作系统
如何在vm虚拟机里面安装win10操作系统
11 0