Controlling VirtualBox via COM from Python?
-
05-07-2019 - |
Question
I'm trying to control latest Sun VirtualBox via it's COM interface from Python. But, unfortunately, the following code don'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 )
Error is general "(-2147467262, 'No such interface supported', None, None)" It seems that the wrong part is my COM handing via Python. Anyone can drop a look and suggest some obvious thing i'm doing wrong?
Solution
The problem is that the object returned by FindMachine("kubuntu")
does not support the IDispatch interface
, and win32com does not support that.
You could use my comtypes
package http://starship.python.net/crew/theller/comtypes/ for that, but you need to patch the version in the repository to make it work with the 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 that 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,