Controlling VirtualBox via COM with Python?

I am trying to control the latest Sun VirtualBox via a COM interface from Python. But unfortunately the following code doesn't work:

import win32com.client
VBOX_GUID = "{B1A7A4F2-47B9-4A1E-82B2-07CCD5323C3F}"
try :
  oVbox = win32com.client.Dispatch( VBOX_GUID )
  oVbox.FindMachine( "kubuntu" )
except Exception as oEx:
  print str( oEx )

      

The error is generic "(-2147467262," No such interface supported ", None, None)" It looks like the wrong part is my COM passing through Python. Anyone can take a look and suggest some obvious things I am doing wrong?

0


source to share


1 answer


The problem is that the object returned FindMachine("kubuntu")

doesn't support IDispatch interface

and win32com doesn't support this.

You can use my comtypes

package http://starship.python.net/crew/theller/comtypes/ for this, but you need to fix the version in the repository to make it work with VirtualBox type libraries.

Here's a demo session:



Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from comtypes.client import CreateObject
>>> box = CreateObject("VirtualBox.VirtualBox")
>>> m = box.FindMachine("Fedora")
>>> print m.State
4
>>> print m.CpuCount
1
>>> print m.Name
Fedora
>>>

      

And here is the patch you need:

Index: automation.py
===================================================================
--- automation.py   (revision 507)
+++ automation.py   (working copy)
@@ -753,6 +753,8 @@
     c_float: VT_R4,
     c_double: VT_R8,

+    c_ulonglong: VT_I8,
+
     VARIANT_BOOL: VT_BOOL,

     BSTR: VT_BSTR,

      

+3


source







All Articles