zoukankan      html  css  js  c++  java
  • C#窗体实现打开关闭VM虚拟机

    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

  • 相关阅读:
    (四)自定义多个Realm以及Authenticator与AuthenticationStrategy
    (三)自定义Realm
    (二)shiro之jsp标签
    (一)shiro简介和用户登录demo及角色管理
    解决Cannot change version of project facet Dynamic web module to 2.5(转)
    (十二)easyUI之表单和验证完成登录页面
    (十一)springmvc和spring的整合
    (十)springmvc之文件的处理
    (九)springmvc之json的数据请求(客户端发送json数据到服务端)
    (九)springmvc之json的处理(服务端发送json数据到客户端)
  • 原文地址:https://www.cnblogs.com/lossingdawn/p/4083918.html
Copyright © 2011-2022 走看看