添加引用->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. Jstack线程状态BLOCKED/TIMED_WAITING/WAITING解释

    一.线程5种状态 新建状态(New) 新创建了一个线程对象. 就绪状态(Runnable) 线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中,变得可运行,等待获 ...

  2. 【转】 远程连接mysql

    转自:http://www.linuxidc.com/Linux/2013-05/84813.htm 1.确认能ping通 2.确认端口能telnet通.如果user表的host值是localhost ...

  3. 「玩转树莓派」树莓派 3B+ 配置无线WiFi

    前言 网线不方便还花钱,有自带的无线 WiFi 模块为啥不用. 网络模式 这里我们先介绍两种网络模式,WPA-Personal 与 WPA-Enterprise. WPA-Personal 大多数家庭 ...

  4. 高并发IM系统架构优化实践

    互联网+时代,消息量级的大幅上升,消息形式的多元化,给即时通讯云服务平台带来了非常大的挑战.高并发的IM系统背后究竟有着什么样的架构和特性? 以上内容由网易云信首席架构师内部分享材料整理而成 相关阅读 ...

  5. ssm中mapper注入失败的传奇经历

    最近因为要测试一个功能,需要用最短的时间来启动服务,开启测试程序,但平常所用的框架中已经集成了各种三方的东西,想着那就再重新搭建一个最简单的ssm框架吧. 搭建可参考:简单ssm最新搭建 搭建过程并不 ...

  6. Educational Codeforces Round 66 (Rated for Div. 2) A

    A. From Hero to Zero 题目链接:http://codeforces.com/contest/1175/problem/A 题目 ou are given an integer n ...

  7. Python程序中的协程操作-gevent模块

    目录 一.安装 二.Gevent模块介绍 2.1 用法介绍 2.2 例:遇到io主动切换 2.3 查看threading.current_thread().getName() 三.Gevent之同步与 ...

  8. 使用SQL行转列函数pivot遇到的问题

    背景:对投票的结果按照单位进行汇总统计,数据库中表记录的各个账号对各个选项的投票记录.马上想到一个解决方案,先根据单位和选项进行Group By,然后再行转列得出单位对各个选项的投票情况. with ...

  9. 设计和编写一个异步通用Picker选择器,用于时间日期、城市、商品分类的选择

    目录 一.功能规划 二.最底层基础实现 (1)Picker界面和功能实现 (2)不同类型的选择器基础实现 三.数据源层 (1)时间日期 (2)多级同步分类,如:城市 (3)多级异步分类,如:城市 四. ...

  10. 基于SpringBoot从零构建博客网站 - 开发设置主页标识和修改个人信息功能

    由于守望博客系统中支持由用户自己设置个人主页的URL的后半段,所以必须要用户设置该标识的功能,而且是用户注册登录之后自动弹出的页面,如果用户没有设置该标识,其它的操作是不能够操作的,同时要求主页标识只 ...