添加引用->VixCOM.dll (在vix文件夹下)

VixWrapper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using VixCOM; namespace VMHelper
{
class VixWrapper
{
VixCOM.IVixLib vixLib = null; ulong m_vixError;
VixCOM.IHost m_hostHandle = null;
VixCOM.IVM m_vmHandle = null; public ulong GetError()
{
return m_vixError;
} public VixWrapper()
{
try
{
vixLib = new VixCOM.VixLibClass();
}
catch (COMException comExc)
{
System.Diagnostics.Trace.WriteLine(comExc.Message + "\n");
throw;
}
} /// <summary>
/// Creates a host handle
/// </summary>
/// <returns>true if succeeded, otherwise false</returns>
public bool Connect(string hostName, string userName, string password)
{
int hostType = string.IsNullOrEmpty(hostName) ? VixCOM.Constants.VIX_SERVICEPROVIDER_VMWARE_WORKSTATION : VixCOM.Constants.VIX_SERVICEPROVIDER_VMWARE_SERVER; int vixVersion = VixCOM.Constants.VIX_API_VERSION; vixVersion = -1; VixCOM.IJob jobHandle = vixLib.Connect(vixVersion, hostType, null, 0, userName, password, 0, null, 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_hostHandle = (VixCOM.IHost)objectArray[0];
return true;
} return false;
} /// <summary>
/// Opens the virtual machine specified in vmxFilePath
/// </summary>
/// <param name=”vmxFilePath”>The virtual machine vmx file to open</param>
/// <returns>true if succeeded, otherwise false</returns>
public bool OpenVm(string vmxFilePath)
{
IJob jobHandle = m_hostHandle.OpenVM(vmxFilePath, 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>
/// Power on the virtual machine
/// </summary>
/// <returns>true if succeeded, otherwise false</returns> 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>
/// Starts a snapshot of a virtual machine
/// </summary>
/// <param name=”snapshot_name”>The name of the snapshot to start</param>
/// <returns>true if succeeded, otherwise false</returns>
public bool RevertToLastSnapshot()
{
ISnapshot snapshot = null;
m_vixError = m_vmHandle.GetRootSnapshot(0, out snapshot); if (m_vixError == VixCOM.Constants.VIX_OK)
{
IJob jobHandle = m_vmHandle.RevertToSnapshot(snapshot, 0, null, null); m_vixError = jobHandle.WaitWithoutResults();
} return (m_vixError == VixCOM.Constants.VIX_OK);
} /// <summary>
/// Login to the virtual machine
/// </summary>
/// <returns>true if succeeded, otherwise false</returns>
public bool LogIn(string username, string password)
{
IJob jobHandle = m_vmHandle.LoginInGuest(username, password, 0, null);
m_vixError = jobHandle.WaitWithoutResults(); return (m_vixError == VixCOM.Constants.VIX_OK);
} /// <summary>
/// Creates the directory in the Virtual Machine
/// </summary>
/// <param name=”pathName”></param>
/// <returns></returns>
public bool CreateDirectoryInVm(string pathName)
{
IJob jobHandle = m_vmHandle.CreateDirectoryInGuest(pathName, null, null);
m_vixError = jobHandle.WaitWithoutResults(); return (m_vixError == VixCOM.Constants.VIX_OK);
} /// <summary>
/// Copies a file from the host machine to the virtual machine
/// </summary>
/// <param name=”sourceFile”>The source file on the host machine</param>
/// <param name=”destinationFile”>The destination on the VM</param>
/// <returns>true if succeeded, otherwise false</returns>
public bool CopyFileToVm(string sourceFile, string destinationFile)
{
//
// Copy files from host to guest
//
IJob jobHandle = m_vmHandle.CopyFileFromHostToGuest(sourceFile, destinationFile,
0, null, null);
m_vixError = jobHandle.WaitWithoutResults(); return (m_vixError == VixCOM.Constants.VIX_OK);
} /// <summary>
/// Copies a file from the virtual machine to the host machine
/// </summary>
/// <param name=”sourceFile”>The source file on the virtual machine</param>
/// <param name=”destinationFile”>The destination on the host machine</param>
/// <returns>true if succeeded, otherwise false</returns>
public bool CopyFileFromVm(string sourceFile, string destinationFile)
{
//
// Copy files from host to guest
//
IJob jobHandle = m_vmHandle.CopyFileFromGuestToHost(sourceFile, destinationFile,
0, null, null);
m_vixError = jobHandle.WaitWithoutResults(); return (m_vixError == VixCOM.Constants.VIX_OK);
} /// <summary>
/// Runs a program on the virtual machine
/// </summary>
/// <param name=”exePath”>The path of the program on the virtual machine</param>
/// <param name=”parameters”>The parameters to pass to the executable</param>
/// <param name=”resultCode”>The result code returned from the program that ran on the VM</param>
/// <returns>true if succeeded, otherwise false</returns>
public bool RunProgram(string exePath, string parameters, out int resultCode)
{
resultCode = -1; IJob jobHandle = m_vmHandle.RunProgramInGuest(exePath,
parameters, VixCOM.Constants.VIX_RUNPROGRAM_ACTIVATE_WINDOW, null, null); // clientData int[] propertyIds = new int[1] { VixCOM.Constants.VIX_PROPERTY_JOB_RESULT_GUEST_PROGRAM_EXIT_CODE };
object results = new object();
m_vixError = jobHandle.Wait(propertyIds, ref results); if (m_vixError == VixCOM.Constants.VIX_OK)
{
object[] objectArray = (object[])results;
resultCode = (int)objectArray[0];
return true;
} return false;
} /// <summary>
/// Power off the virtual machine
/// </summary>
/// <returns>true if succeeded, otherwise false</returns>
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>
/// Restart the virtual machine
/// </summary>
/// <returns>true if succeeded, otherwise false</returns>
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); } }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using VMHelper; namespace VixWrapperTest
{
class Program
{
static void Main(string[] args)
{
try
{
//VixWrapper.VixWrapper vix = new VixWrapper.VixWrapper();
VixWrapper wrapper = new VixWrapper(); wrapper.Connect(null, "Administrator", null); wrapper.OpenVm(@"E:\win xp\Windows XP Professional.vmx");//安装好的虚拟机.vmx的实际路径 wrapper.PowerOn(); wrapper.PowerOff(); }
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
} }
}

本程序实现了通过VS控制台打开vm虚拟机的功能,有兴趣的可以去调用剩下的函数,做个时延函数,然后完成其他任务。

C#控制台打开VM虚拟机的更多相关文章

  1. 批处理启动vm虚拟机服务 vm12启动无界面启动vm虚拟机系统 windows上如何操作服务 sc net启动关闭服务

    windows(win10)批处理脚本 打开vm虚拟机的服务,并且开启无界面虚拟机 @echo off net start "vds" net start "VMAuth ...

  2. Linux关于vm虚拟机复制后无法启动网卡

    1.一个月前由于自己一直在开发PHP站点,所以把Linux抛出去很长时间没有碰,最近几天把Linux的一些捡起来, 但在我设置vm虚拟机由于在家里做的实验未做完,复制到U盘想到公司接着做没成像,系统是 ...

  3. VM 虚拟机 Error 1324. The path My Documents contains a invalid chara 。

    当安装VM(虚拟机)时,安装到一半时,提示:Error 1324. The path My Documents contains a invalid chara . 就是提示路径无效. 按下面的路径: ...

  4. VM虚拟机安装苹果雪豹操作系统

    1.win xp虚拟机安装Mac OSX 一.用VM8安装mac os x10.6 ,然后升级到的10.6.8,如何安装vm大家自己百度吧.这里指列出了如何安装雪豹操作系统. DMG是mac os x ...

  5. VM虚拟机的配置文件(.vmx)损坏修复

    来源://http://blog.csdn.net/houffee/article/details/18398603 VM虚拟机中使用.vmx文件保存虚拟机的所有软硬件配置,如果意外损坏的话将会出现不 ...

  6. Linux centOS的vm虚拟机配置详细 中文版

    这里以安装cenOS6.6 为例 如果想要需要cenos 6.6 ios文件的朋友看我的另一篇关于cenos6.6版本的下载详细 文中内容是摘抄自老男孩老师的<linux 跟老男孩学Linux运 ...

  7. VM虚拟机扩展硬盘容量

    VM虚拟机扩展硬盘容量 第一步,关闭系统,给虚拟机硬盘增加空间. 第二步,启动系统.查看硬盘大小和分区情况. 第三步,分区. 第四步,格式化分区. 第五步,挂载. 第六步,开机自动挂载. 第一步: 当 ...

  8. VM虚拟机的配置文件(.vmx)损坏

    为了禁用时间同步,使用sublime修改vmx文件 文件第一行为.encoding = "GBK" 修改完毕,无法打开虚拟机,报 VM虚拟机的配置文件(.vmx)损坏错误 因为su ...

  9. VM虚拟机 安装linux系统

    首先需要下载VMware10 和CentOS-6.4,我这边提供了百度网盘,可供下载链接:https://pan.baidu.com/s/1vrJUK167xnB2JInLH890fw 密码:r4jj ...

随机推荐

  1. 中国2017 Google 开发者大会第一天简单回顾

    昨天有幸参加了中国2017 Google 开发者大会,在这第一天就收获满满,昨天太忙了,今天早晨来一起简单回顾一下,可以让没有参加的童鞋们感受一下现场的温度. 早早就来到了会议现场,外面看不出什么特别 ...

  2. window下搭建qt开发环境编译、引用ace

    工作中经常用到ace.tao等,在windwo下的c++开发工具基本上就是vs20xx这些工具,还有些就是类似编辑工具例如:source insight等,前者比较大,打开.编译运行比较慢,二期针对a ...

  3. 性别年龄的模块封装类 IDSGenderLeviNamedView

    1 IDSGenderLeviNamedView 的实现效果     2 类的封装方法:   IDSGenderLeviNamedView.h   @interface IDSGenderLeviNa ...

  4. Kafka笔记3

    向Kafka写入消息从创建一个ProducerRecord对象开始,ProducerRecord需要包含目标主题和要发送的内容,我们还可以指定键或分区,在发送ProducerRecord对象时,生产者 ...

  5. 深入理解JVM(一)虚拟机内存

    一 .前言 JVM是什么,我想诸位肯定都清楚. 好吧,我还是简答说一下JVM即Java虚拟机(够简单吧 233333). 虽然说,所有抛开操作系统,讲虚拟机的内容,都是耍流氓.但是,贫僧不修善果,就爱 ...

  6. Flutter学习笔记(2)--Dart语言简介

    Dart简介: Dart诞生于2011年10月10日,Dart是一种"结构化的web编程"语言,Dart虽然是谷歌开发的计算机编程语言,但后来被ECMA认定位标准,这门语言用于We ...

  7. 使用 Python 识别并提取图像中的文字

    1. 介绍 介绍使用 python 进行图像的文字识别,将图像中的文字提取出来,可以帮助我们完成很多有趣的事情. 2. 必备工具 tesseract-ocr 下载地址: https://github. ...

  8. HBase —— 单机环境搭建

    一.安装前置条件说明 1.1 JDK版本说明 HBase 需要依赖JDK环境,同时HBase 2.0+ 以上版本不再支持JDK 1.7 ,需要安装JDK 1.8+ .JDK 安装方式见本仓库: Lin ...

  9. RT-Thread的CPU占用率查看

    今天看到朋友的博客,他在描述RT-Thread钩子函数时,简单提了下RT-Thread中CPU占用,没有具体描述,所以我在这里做下补充. RT-Thread查看CPU使用率时,我知道的有这种方法. 大 ...

  10. HDU 1286:找新朋友(欧拉函数)

    http://acm.hdu.edu.cn/showproblem.php?pid=1286 题意:中文. 思路:求欧拉函数. #include <cstdio> #include < ...