attachUSBDevice() raising exception on held Usb device state

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
Herve C
Posts: 1
Joined: 1. Jul 2019, 15:41

attachUSBDevice() raising exception on held Usb device state

Post by Herve C »

hi,

I am currently working on a projet using python3, and I encounter an issue with USB device attachement :
Host : Windows 10 (1903)
guest: Windows 7 sp1
Virtualbox API 5.2.XX

I read on the documentation that on Windows the device can be in BUSY state (and it is), after calling attachUSBDevice(), the device is then in HELD state.
As I read in the doc,
Note: Due to differences in USB stack implementations in Linux and Win32, states USBDeviceState_
Busy and USBDeviceState_Unavailable are applicable only to the Linux
version of the product. This also means that (IConsole::attachUSBDevice()) can only
succeed on Win32 if the device state is USBDeviceState_Held.
So I said to myself let's recall attachUSBDevice()

the result of the second call is an exception:

Code: Select all

File "C:\Users\hch\AppData\Local\Temp\gen_py\3.6\D7569351-1750-46F0-936E-BD127D5BC264x0x1x3.py", line 1279, in AttachUSBDevice
    , aCaptureFilename)
pywintypes.com_error: (-2147352567, 'Une exception s’est produite.', (0, None, None, None, 0, -2147467259), None)

vbox.VboxException: NS_ERROR_FAILURE: Une exception s’est produite.
    raise VboxException(vboxErr(com_e))
Here is my code to attach a device

Code: Select all

def attach_usb_device(mach, device):

    session = None
    try:
        if mach.state == vb_const.MachineState_Running:
            session = vb_ctx['global'].openMachineSession(mach) 
            console = session.console

            if (device.state == vb_const.USBDeviceState_Available) or\
               (device.state == vb_const.USBDeviceState_Busy):
                console.attachUSBDevice(device.id, None)
            if device.state == vb_const.USBDeviceState_Held:
                console.attachUSBDevice(device.id, None)

    except pythoncom.com_error as com_e:
        raise VboxException(vboxErr(com_e))
    else:
        if session:
            if session.state == vb_const.SessionState_Locked:
                vb_ctx['global'].closeMachineSession(session)
I think i'm missing something...
thank for your help
SmithersTheOracle
Posts: 60
Joined: 28. Dec 2019, 08:58
Primary OS: MS Windows 7
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: attachUSBDevice() raising exception on held Usb device state

Post by SmithersTheOracle »

Well I haven't got to attachUSBDevice yet, and I know this is an old topic, but the SDKRef says the second argument is for a captureFilename and I see you've got 'None' in your calls. I'm not a Python expert, but Google says a lot about that error meaning a file can't be found. It sounds like that needs to be an existing file to capture traffic to.

From the SDKRef:
captureFilename Filename to capture the USB traffic to.
I'll post again if I get to it, and I find a guaranteed solution, so there will be a permanent record of the fix.
Post Reply