C#控制台打开VM虚拟机

简介: 添加引用->VixCOM.dll (在vix文件夹下) VixWrapper.cs using System; using System.Collections.Generic; using System.

添加引用->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虚拟机的功能,有兴趣的可以去调用剩下的函数,做个时延函数,然后完成其他任务。

 

遗失的拂晓
目录
相关文章
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
2788 0
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.0 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.0 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
4253 1
VMware Remote Console 13.0.0 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
|
存储 Ubuntu Linux
使用VM虚拟机安装最新版Ubuntu系统
重启后开机进入登录界面 输入前面设置的密码进入系统 成功启动Ubuntu系统
|
域名解析 API PHP
VM虚拟机全版本网盘+免费本地网络穿透端口映射实时同步动态家庭IP教程
本文介绍了如何通过网络穿透技术让公网直接访问家庭电脑,充分发挥本地硬件性能。相比第三方服务受限于转发带宽,此方法利用自家宽带实现更高效率。文章详细讲解了端口映射教程,包括不同网络环境(仅光猫、光猫+路由器)下的设置步骤,并提供实时同步动态IP的两种方案:自建服务器或使用三方API接口。最后附上VM虚拟机全版本下载链接,便于用户在穿透后将服务运行于虚拟环境中,提升安全性与适用性。
1473 7
|
开发框架 .NET C#
C# 一分钟浅谈:第一个 C# 控制台应用程序
【9月更文挑战第1天】C# 是一种现代化的、面向对象的编程语言,广泛应用于桌面应用、Web 应用和游戏开发等领域。本文详细介绍如何创建第一个 C# 控制台应用程序,包括使用 Visual Studio 和 .NET SDK 的步骤,并解析常见问题及其解决方法,如控制台窗口立即关闭、编译错误和运行时错误等。通过实践,你将掌握 C# 控制台应用的基础知识,为进一步学习打下坚实基础。
922 49
|
虚拟化
如何为VM虚拟机添加D盘
这篇文章介绍了如何在VMware虚拟机中将E盘修改为D盘,并提供了详细的操作步骤,包括在计算机管理中更改驱动器号和路径,以及初始化和格式化新磁盘的指导。
如何为VM虚拟机添加D盘
|
虚拟化
VM虚拟机只有一个C盘怎么添加硬盘新分区盘符
关于如何在VMware虚拟机中解决只有一个C盘的问题,介绍了增加硬盘和创建新分区的方法。
VM虚拟机只有一个C盘怎么添加硬盘新分区盘符
|
安全 Windows
【Azure 环境】Azure 的PaaS服务如果涉及到安全漏洞问题后,我们如何确认所用服务的实例(VM:虚拟机)的操作系统已修复该补丁呢?
【Azure 环境】Azure 的PaaS服务如果涉及到安全漏洞问题后,我们如何确认所用服务的实例(VM:虚拟机)的操作系统已修复该补丁呢?
510 0
|
API 网络架构
【Azure Developer】如何通过Azure REST API 获取到虚拟机(VM)所使用的公共IP地址信息
【Azure Developer】如何通过Azure REST API 获取到虚拟机(VM)所使用的公共IP地址信息
312 0
|
存储 API 开发工具
【Azure 环境】在Azure虚拟机(经典) 的资源中,使用SDK导出VM列表的办法
【Azure 环境】在Azure虚拟机(经典) 的资源中,使用SDK导出VM列表的办法
334 0