Opened 2 years ago
#20750 new defect
Examples to control VM via COM do not work
Reported by: | sweber | Owned by: | |
---|---|---|---|
Component: | other | Version: | VirtualBox 6.1.30 |
Keywords: | Cc: | ||
Guest type: | Windows | Host type: | Windows |
Description
Hi!
I have a VM named Win8.1, running on a Win10 host, which can be controlled manually via the VM manager.
Now, I'd like to control it via COM, but even the examples shipped with the SDK do not work.
Python
The file SDKRef.pdf lists the following Python code in section 2.3.1. I just changed the name of the VM to Win8.1 and added two print commands:
import win32com.client vbox = win32com.client.Dispatch("VirtualBox.VirtualBox") session = win32com.client.Dispatch("VirtualBox.Session") mach = vbox.findMachine("Win8.1") print("Machine.State: ", mach.State) print("Machine SnapshotFolder: ", mach.SnapshotFolder) progress = mach.launchVMProcess(session, "gui", "") progress.waitForCompletion(-1)
The output is:
Machine.State: 1 Machine SnapshotFolder: C:\VMs\Win8.1\Snapshots Traceback (most recent call last): File "C:\Users\webers\Desktop\vbox.py\vbox.py", line 8, in <module> progress = mach.launchVMProcess(session, "gui", "") File "<COMObject <unknown>>", line 2, in launchVMProcess pywintypes.com_error: (-2147352571, 'Typenkonflikt.', None, 3)
The output of the two print commands is correct, so COM in general works, but launching the VM fails with error "Typenkonflikt" (this is german, so it's not a Python error, but comes from my german VM installation or the german Win10 host)
VBS
In sdk\bindings\mscom\vbs\sample, there is this listing, where I only inserted my VM name:
' $Id: vboxinfo.vbs 135976 2020-02-04 10:35:17Z bird $ '' @file ' ??? ' ' ' Copyright (C) 2009-2020 Oracle Corporation ' ' This file is part of VirtualBox Open Source Edition (OSE), as ' available from http://www.virtualbox.org. This file is free software; ' you can redistribute it and/or modify it under the terms of the GNU ' General Public License (GPL) as published by the Free Software ' Foundation, in version 2 as it comes in the "COPYING" file of the ' VirtualBox OSE distribution. VirtualBox OSE is distributed in the ' hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. ' Sub Print(str) Wscript.Echo str End Sub Sub StartVm(vb, mach) Dim session, progress Set session = CreateObject("VirtualBox.Session") Set progress = vb.openRemoteSession(session, mach.id, "gui", "") progress.waitForCompletion(-1) session.close() End Sub Sub StopVm(vb, mach) Dim session, progress Set session = CreateObject("VirtualBox.Session") vb.openExistingSession session, mach.id session.console.powerDown().waitForCompletion(-1) session.close() End Sub Sub Main Dim vb, mach set vb = CreateObject("VirtualBox.VirtualBox") Print "VirtualBox version " & vb.version ' Safe arrays not fully functional from Visual Basic Script, as we ' return real safe arrays, not ones wrapped to VARIANT and VBS engine ' gets confused. Until then, explicitly find VM by name. ' May wish to use hack like one described in ' http://www.tech-archive.net/Archive/Excel/microsoft.public.excel.programming/2006-05/msg02796.html to handle safearrays ' if desperate Set mach = vb.findMachine("Win8.1") Print "Machine: " & mach.name & " ID: " & mach.id StartVm vb, mach End Sub Main
Again, the print commands work correctly. But then, line 26 (Set progress = vb.openRemoteSession(session, mach.id, "gui", "") throws an error:
Line: 26 Column: 3 Error: The object does not support the proberty or method: 'vb.openRemoteSession' Code: 800A01B6 Source: Run time error in Microsoft VBScript
I've also replaced "openRemoteSesstion" by "launchVMProcess", with the only result that this is replaced in the error message, too.
So, is there any functional example on how to control a VM via COM?