添加引用->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. UWP入门(十)--创建、写入和读取文件

    原文:UWP入门(十)--创建.写入和读取文件 核心的 API github代码 StorageFolder 类 StorageFile 类 FileIO 类 使用 StorageFile 对象读取和 ...

  2. 使用Boost的DLL库管理动态链接库

    Boost 1.61新增了一个DLL库,跟Qt中的QLibrary类似,提供了跨平台的动态库链接库加载.调用等功能.http://www.boost.org/users/history/version ...

  3. 更换Qt QtEmbedded库的版本出现问题及解决(交叉编译OpenSSL)

    近日将QtEmbedded库的版本由4.7.0更新到4.7.4.工具链并未改变,仍为 Target: arm-none-linux-gnueabiConfigured with: ......Thre ...

  4. WCF研究-前篇

    前篇 1.从SOA说起 2.什么是WCF 3.WCF通信模型 4.Endpoint与ABC以及元数据    1.SOA (Service Oriented  Architecture) Ø 一种组件架 ...

  5. Window Features(包括Z-Order,Layered Windows, Message-Only Windows, Owned Windows, Window的状态等)

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms632599(v=vs.85).aspx#owned_windows https: ...

  6. Codility---BinaryGap

    Task description A binary gap within a positive integer N is any maximal sequence of consecutive zer ...

  7. Spark之SparkSql

    -- Spark SQL 以编程方式指定模式 val sqlContext = new org.apache.spark.sql.SQLContext(sc) val employee = sc.te ...

  8. Fabric1.4源码解析: 链码容器启动过程

    想写点东西记录一下最近看的一些Fabric源码,本文使用的是fabric1.4的版本,所以对于其他版本的fabric,内容可能会有所不同. 本文想针对Fabric中链码容器的启动过程进行源码的解析.这 ...

  9. 使用BurpSuite的Collaborator查找.Onion隐藏服务的真实IP地址

    本文转载!!! 原文地址:http://www.4hou.com/technology/10367.html 翻译来自:http://digitalforensicstips.com/2017/11/ ...

  10. Storm —— 集群环境搭建

    一.集群规划 这里搭建一个3节点的Storm集群:三台主机上均部署Supervisor和LogViewer服务.同时为了保证高可用,除了在hadoop001上部署主Nimbus服务外,还在hadoop ...