When you start writing your own programs to interact with GSX or ESX server, you might want to do so from a .NET application. In this article, I will show you how to do just that. It is quite simple.
In this example, I will build a simple application that illustrates how to perform the basic tasks. The application just retrieves a list of registered virtual machines and displays them in a listbox. When you double click the virtual machine, some details about that virtual machine are shown.
The sample was developed with Visual C# 2005 Express Edition.
1. Install VMWare COM API
You need to install the VMWare COM API on your system. You can install the COM API using VMWare’s installer or you can just copy the following files to a directory on your system (available when you installed the COM API on another machine, such as the GSX server itself):
VmCOM.dll
VMControlLib.dll
libeay32.dll
ssleay32.dll
Register VmCOM.dll with regsvr32 vmcom.dll.
2. Start a new project and add a reference to VmCOM.dll
In Visual C# 2005, start a new Windows Application project. In the Solution Explorer, right click your project and select Add Reference.

Select the VMWare VmCOM 1.0 Type Library and click OK.
3. Use the VMWare COM API in your code
Add a using statement to your code:
using
VMCOMLib;To connect to a VMWare server, first create a VMConnectParams object:
vmConn =
newVmConnectParams();vmConn.Hostname = "server";
vmConn.Username = "user";
vmConn.Password = "password";
To connect to the server with these credentials:
vmSrvCtl =
newVmServerCtl();vmSrvCtl.Connect(vmConn);
Now you are connected to the server, you can enumerate the registered virtual machines:
foreach
(string s in vmSrvCtl.RegisteredVmNames){ VmCtl vm = newVmCtl(); //create object to control a virtual machine
vm.Connect(vmConn, s); //connect to virtual machine
listBox1.Items.Add(newvirtualMachine(s, vm.get_Config("DisplayName")));
}
In the code snippet above, I add a virtualMachine object to a listbox. The virtualMachine class is just a custom class I use to store the path to the vmx file and the displayname of the virtual machine in each listbox item. The class has an overridden ToString() method to show the displayname of the virtual machine in the listbox.
Now you can add some code to display information about the virtual machine when you double click that machine in the listbox:
virtualMachine vmItem = (virtualMachine)listBox1.SelectedItem;string vmx = vmItem.vmx;VmCtl vm = newVmCtl();
vm.Connect(vmConn, vmx);StringBuilder sb = newStringBuilder();
//ip seems to be the only out of the box guestinfo variable
sb.AppendFormat("Name:\t {0}\n", vm.get_Config("DisplayName"));
sb.AppendFormat("Memory:\t {0}\n", vm.get_Config("memsize"));
sb.AppendFormat("IP:\t {0}\n", vm.get_GuestInfo("ip"));
MessageBox.Show(sb.ToString(), "VM Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
The above code is used in the DoubleClick event of the listbox. It just gets the vmx name from the currently selected item in the listbox and then connects to the virtual machine with the VmCtl object. You always connect to a registered virtual machine using the full path of the vmx file (configuration file) of that virtual machine. Once you are connected to the virtual machine you can do things such as starting and stopping or just retrieving information.
In the above example, I only retrieve information from the vm. When you use get_Config, you retrieve information from the vmx (such as displayName and memsize). When you use get_GuestInfo, you retrieve information you have set with the VMWare Tools. Apparantly, ip is an available GuestInfo variable that retrieves the ip address of the virtual machine. It seems there are no other such variables.



