C#窗体实现打开关闭VM虚拟机

vixclass.cs//定义开机、关机等函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
	using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using VixCOM;

namespace dome
{
class vixclass
{
public VixCOM.IVixLib IvixLib;
public ulong m_vixError;
public VixCOM.IHost m_hostHandle;
public VixCOM.IVM m_vmHandle;
// public VixCOM.IJob jobHandle;

public vixclass()
{
IvixLib = new VixCOM.VixLibClass();
m_vixError=0;
m_hostHandle = null;
m_vmHandle = null;
//jobHandle = null;

}


public ulong GetError()
{
return m_vixError;

}

/// <summary>
/// 创建链接
/// </summary>
public bool Connect(string _hostname,string _username, string _password)
{
int hostType = VixCOM.Constants.VIX_SERVICEPROVIDER_VMWARE_WORKSTATION;

int vixVersion = VixCOM.Constants.VIX_API_VERSION;
vixVersion = -1;

int[] propertyIds = new int[1] { VixCOM.Constants.VIX_PROPERTY_JOB_RESULT_HANDLE };

object results = new object();

IJob jobHandle = IvixLib.Connect(vixVersion, hostType, _hostname, 0, _username, _password, 0, null, null);

//jobHandle = IvixLib.Connect(vixVersion, hostType, hostname, 0, user, password, 0, null, null);


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>
///打开vmxPath的虚拟机
/// </summary>

public bool OpenVm(string vmxPath)
{
IJob jobHandle = m_hostHandle.OpenVM(vmxPath, 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>
/// 启动虚拟机
/// </summary>
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>
/// 关闭虚拟机
/// </summary>

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>
/// 重启虚拟机
/// </summary>

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);

}
}
}

Form1.cs//主窗体,

textbox1//记录选择的虚拟机的路径

btnselect//选择路径

btnstart//打开虚拟机

btnclose//关闭虚拟机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace dome
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}



private void btnselect_Click(object sender, EventArgs e)
{
OpenFileDialog loSaveFile = new OpenFileDialog();
loSaveFile.Filter = ".vmx文件(*.vmx)|*.vmx";
if (loSaveFile.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = loSaveFile.FileName;
}
}

private void btnstart_Click(object sender, EventArgs e)
{
try
{
vixclass vix = new vixclass();
string vmxpath = textBox1.Text;
vix.Connect(null, "Administrator", null);
vix.OpenVm(@vmxpath);
vix.PowerOn();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());

}


}

private void btnclose_Click(object sender, EventArgs e)
{
try
{
vixclass vix = new vixclass();
string vmxpath = textBox1.Text;
vix.Connect(null , "Administrator", null);
vix.OpenVm(@vmxpath);
vix.PowerOff();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

注:添加引用:VixCOM.DLL