C#控制台打开VM虚拟机
添加引用->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虚拟机的更多相关文章
- 批处理启动vm虚拟机服务 vm12启动无界面启动vm虚拟机系统 windows上如何操作服务 sc net启动关闭服务
windows(win10)批处理脚本 打开vm虚拟机的服务,并且开启无界面虚拟机 @echo off net start "vds" net start "VMAuth ...
- Linux关于vm虚拟机复制后无法启动网卡
1.一个月前由于自己一直在开发PHP站点,所以把Linux抛出去很长时间没有碰,最近几天把Linux的一些捡起来, 但在我设置vm虚拟机由于在家里做的实验未做完,复制到U盘想到公司接着做没成像,系统是 ...
- VM 虚拟机 Error 1324. The path My Documents contains a invalid chara 。
当安装VM(虚拟机)时,安装到一半时,提示:Error 1324. The path My Documents contains a invalid chara . 就是提示路径无效. 按下面的路径: ...
- VM虚拟机安装苹果雪豹操作系统
1.win xp虚拟机安装Mac OSX 一.用VM8安装mac os x10.6 ,然后升级到的10.6.8,如何安装vm大家自己百度吧.这里指列出了如何安装雪豹操作系统. DMG是mac os x ...
- VM虚拟机的配置文件(.vmx)损坏修复
来源://http://blog.csdn.net/houffee/article/details/18398603 VM虚拟机中使用.vmx文件保存虚拟机的所有软硬件配置,如果意外损坏的话将会出现不 ...
- Linux centOS的vm虚拟机配置详细 中文版
这里以安装cenOS6.6 为例 如果想要需要cenos 6.6 ios文件的朋友看我的另一篇关于cenos6.6版本的下载详细 文中内容是摘抄自老男孩老师的<linux 跟老男孩学Linux运 ...
- VM虚拟机扩展硬盘容量
VM虚拟机扩展硬盘容量 第一步,关闭系统,给虚拟机硬盘增加空间. 第二步,启动系统.查看硬盘大小和分区情况. 第三步,分区. 第四步,格式化分区. 第五步,挂载. 第六步,开机自动挂载. 第一步: 当 ...
- VM虚拟机的配置文件(.vmx)损坏
为了禁用时间同步,使用sublime修改vmx文件 文件第一行为.encoding = "GBK" 修改完毕,无法打开虚拟机,报 VM虚拟机的配置文件(.vmx)损坏错误 因为su ...
- VM虚拟机 安装linux系统
首先需要下载VMware10 和CentOS-6.4,我这边提供了百度网盘,可供下载链接:https://pan.baidu.com/s/1vrJUK167xnB2JInLH890fw 密码:r4jj ...
随机推荐
- SimpleMembership,成员资格提供程序、 通用的提供者和新的 ASP.NET 4.5 Web 窗体和 ASP.NET MVC 4 模板
ASP.NET MVC 4 互联网模板中添加一些新的. 非常有用的功能,构建 SimpleMembership.这些更改将添加一些很有特色,像很多更简单. 可扩展会员 API 和 OAuth 的支持. ...
- C函数实现返回多个值的方法
C语言中,一个函数最多只能实现一个返回值. int func (int b) { int a=5; if (a>b) return a; else return b; return 0; } ...
- GetLastError()返回值列表(3259个错误列表)
GetLastError()返回值列表: [0]-操作成功完成. [1]-功能错误. [2]-系统找不到指定的文件. [3]-系统找不到指定的路径. [4]-系统无法打开文件. [5]-拒绝访问. [ ...
- Day1_Python学习
内容目录 1.变量和常量 2.用户输入 3.getpass模块 4.表达式if...else 5.表达式while 6.表达式for 一.变量和常量 声明变量: name = "Jeffer ...
- Laravel --- Laravel 5.3 队列使用方法
一.设置存储方式 在config/queue.php中查看队列驱动,在.env 设置[QUEUE_DRIVER] 主要介绍数据库驱动 二.数据库驱动 1.修改.env CACHE_DRIVER=fil ...
- Python自学day-10
一.多进程 程序中, 大量的计算占用CPU资源,而IO操作不占CPU资源.当程序需要进行大量计算时,Python采用多线程运行的速度不一定比单线程快多少.但是当程序是IO密集型的,那就应该使用多线程来 ...
- 手动实现 SpringMVC
前几章我们已经分析了 Spring 的源码并且手动实现了一个 IOC/DI 容器. 这章我们在自己实现的 Spring 框架的基础上实现一个 SpringMVC 框架. 我们自己实现的 Spring ...
- 【python3两小时快速入门】入门笔记02:类库导入
昨晚遇到了一个问题:pip下载了request类库,以及在pyCharm的setting中下载了request类库,项目左侧也能显示出requst文件夹,但是引入报错! 这里贴一下我的解决方案,在此记 ...
- java中关于IO流的知识总结(重点介绍文件流的使用)
今天做软件构造实验一的时候,用到了java文件流的使用,因为之前学的不是很踏实,于是决定今天好好总结一下, 也方便以后的回顾. 首先,要分清IO流中的一些基础划分: 按照数据流的方向不同可以分为:输入 ...
- ubuntu镜像快速下载
由于官网服务器在国外,下载速度奇慢,所以我们可以利用阿里云镜像下载ubuntu ubuntu 14.04: http://mirrors.aliyun.com/ubuntu-releases/14.0 ...