我正试图通过Python的COM接口控制最新的Sun VirtualBox。但是,遗憾的是,以下代码不起作用:

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 )

错误是一般的“( - 2147467262,'没有这样的接口支持',无,无)" 似乎错误的部分是我的COM通过Python处理。任何人都可以看一眼,并建议一些明显的事我做错了吗?

有帮助吗?

解决方案

问题是 FindMachine(" kubuntu")返回的对象不支持 IDispatch接口,而win32com不支持。

您可以使用我的 comtypes http:// starship .python.net / crew / theller / comtypes / ,但您需要修补存储库中的版本,使其与VirtualBox类型库一起使用。

这是一个演示会话:

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

以下是您需要的补丁:

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,
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top