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

摘要:
Vixclass。cs//使用System定义通电和断电等功能;使用System.Collections。通用的使用系统。Linq;使用System.Runtime。InteropServices;使用系统。文本使用System.Threading。任务;使用VixCOM;namespacedome{classvixc

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

免责声明:文章转载自《C#窗体实现打开关闭VM虚拟机》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇扫码登录功能如何实现?一文搞懂主流的扫码登录技术原理C语言结构体指针(指向结构体的指针)详解下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

openstack知识---hypervisor

hypervisor Hypervisor是一种运行在物理服务器和操作系统之间的中间软件层,可允许多个操作系统和应用共享一套基础物理硬件,因此也可以看作是虚拟环境中的“元”操作系统,它可以协调访问服务器上的所有物理设备和虚拟机,也叫虚拟机监视器(Virtual Machine Monitor)。Hypervisor是所有虚拟化技术的核心。非中断地支持多工作...

(100%成功超详细图文教程)虚拟机VM ware中centos7无法上网及Xshell配置正确但是连接不上本地虚拟机问题汇总

前言: 作为linux新手,想必一定会遇到各种各样的网络连接问题,菜鸟阶段总感觉自己的错误网上找不到,一度怀疑自己犯的错别人都没犯过,我晕,折腾一天后终于解决了,前来帮助小伙伴解决心中的疑惑。 如果你的目的是从Xshell连接本地的Linux虚拟机,那么请往下看,当然我会顺带着将虚拟机没网的问题说清楚: 前几天做了几个项目,然后不知为何,Xshell就莫名...

Mac-VM迁移注意事项

1.迁移之后首先查看mac的网关是多少: 先cd /Library/Preferences/VMware Fusion/vmnet8 然后 cat nat.conf ip 172.16.190.2就是我的网关,你的和我的应该不一致 2.配置虚拟机中的网络 点击系统-首选项-网络连接。 配置你的网卡,我的是eth1. 地址就是你的这台虚拟机ip,网关和dn...

redis 参数配置总结

redis.conf 配置项说明如下 1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程   daemonize no 2. 当Redis以守护进程方式运行时,Redis默认会把pid写入 /var/run/redis.pid 文件,可以通过pidfile指定pidfile /var/run/redis.pi...

hyper-v简介及安装使用

前言:作为IT界的巨头,微软自己的虚拟化技术,也是微软第一个采用Vmware与CitrixXen一样基于hypervisor的虚拟化技术,有着自己可圈可点的地方,微软自己的虚拟化技术嘛,对windows的支持基本是没有问题的,而根据官方的资料介绍,它甚至可以安装专门为Linux设计的Integrated Components(里面包含磁盘和网络适配器的VM...

使用pipenv

环境:pipenv-2020.11.15.0 以前经常使用virtualenv来创建虚拟环境,通过pip freeze生成requirements.txt文件,然后通过pip install -r requirements.txt进行项目模块的管理与安装。这样的安装存在很多问题,比如每次更新模块后,需要手动的重新生成依赖文件. 最新在做新项目,所以试用一下...