从Python通过COM控制VirtualBox?

发布于 2024-07-19 03:51:25 字数 404 浏览 8 评论 0原文

我正在尝试通过 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,'不支持此类接口',无,无)” 看来错误的部分是我通过 Python 处理的 COM。 任何人都可以看看并提出一些我做错的明显事情吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水染的天色ゝ 2024-07-26 03:51:25

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

您可以使用我的 comtypeshttp://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,

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,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文