添加引用->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. 使用scratchbox2建立交叉编译环境

    使用scratchbox2建立交叉编译环境,使交叉编译不再烦人..... os:ubuntu 12.04.4 x64 1. 安装相关工具sudo apt-get install debootstrap ...

  2. Qt项目里的源代码默认都是Unicode,原因大概是因为qmake.conf里的定义

    MAKEFILE_GENERATOR = MINGWQMAKE_PLATFORM = win32 mingwCONFIG += debug_and_release debug_and_release_ ...

  3. Qt云服务/云计算平台QTC(Qt Cloud Services)入门(0)

    在这个“大数据”的时代,传统的跨平台C++库Qt已经将魔爪丧心病狂的伸向了“云计算”.在2012年的Qt开发者大会上,Qt发布了BaaS(Backend as a Service)服务——Engini ...

  4. Delphi开发 Android 程序启动画面简单完美解决方案

    原文在这里 还是这个方法好用,简单!加上牧马人做的自动生成工具,更是简单. 以下为原文,向波哥敬礼! 前面和音儿一起研究 Android 下启动画面的问题,虽然问题得到了解决,但是,总是感觉太麻烦,主 ...

  5. DUI-模态对话框的实现

    模态对话框要求自己实现自己的消息循环,当然,建议它还是处于主线程中,所以最好是由它再调用主线程的消息循环函数,此时主线程自身的消息循环函数被阻塞,等待模板对话框的消息循环函数退出 参考代码如下: 1 ...

  6. nginx(一) nginx详解

    nginx是一个被广泛使用的集群架构组件,我们有必要对它有足够的了解.下面将先认识nginx:包括应用场景.nginx基本架构.功能特性.并发模型以及配置说明,最后我们再总结下,为什么选择nginx的 ...

  7. SpringBoot整合Redis注意的一些问题

    1:ERR value is not an integer or out of range 1-1:背景 使用redisTemplate.opsForValue().increment(key, de ...

  8. OpenProj打开不了或者提示”Failed to load Java VM Library”的错误的解决方案

    一.双击打开OpenProj.exe没反应的解决方案: 1) 修改OpenProj1.4.0.ini,将Maximum Version=any改为Maximum Version=1.7,保存. 2)这 ...

  9. 打包成war包之后如何读取配置文件

    今天工作开发中遇到一个问题:在idea运行的项目读取配置文件没有问题,打包成war包之后就会报错java.io.FileNotFoundException: class path resource 原 ...

  10. 06、MySQL—列类型

    1.整数类型 I.有符号整型 (1) Tinyint:单字节整形,系统采用一个字节来保存的整形:一个字节 = 8位,最大能表示的数值是0-255. (2) Smallint:双字节整形,系统采用两个字 ...