[python-win32] USB access using win32file.deviceIOcontrol

Sebastian Friebe basti at benkers-rock.de
Wed Dec 12 19:58:40 CET 2007


Tim Roberts wrote:
> This can't be done as you have described it.
>
> The problem is that the SCSI-like command set is only used between the
> USB mass storage driver (usbstor.sys) and the USB host controller
> driver.  Above usbstor.sys, the device looks like a standard mass
> storage device.  There are no hooks in usbstor.sys to allow you to send
> commands like this, nor does usbstor.sys allow you to send USB packets
> directly.

I saw an implementation in C++ using the winAPI DeviceIOControl()
function to do low-level SCSI operations on a USB mass storage device.
It is defined as follows:

BOOL WINAPI DeviceIoControl(
  __in         HANDLE hDevice,
  __in         DWORD dwIoControlCode,
  __in_opt     LPVOID lpInBuffer,
  __in         DWORD nInBufferSize,
  __out_opt    LPVOID lpOutBuffer,
  __in         DWORD nOutBufferSize,
  __out_opt    LPDWORD lpBytesReturned,
  __inout_opt  LPOVERLAPPED lpOverlapped
);

following settings have been applied:
dwIoControlCode              = IOCTL_SCSI_PASS_THROUGH_DIRECT
lpInBuffer = lpOutBuffer     = a structure containing several settings
                               like PATH, TARGET, LUN IDs and CDB[]
                               array and a buffer for input and output
                               data

So if your statement is right, this should even not work under C++,
right? But it does.
Are there maybe some other stuff what makes it different?
I really would like to understand that topic.
Do you know some good references where I can find detailed information
about USB handling under Windows environment?

My first attempt to get it running under Python looks like this:

IOCTL_SCSI_PASS_THROUGH_DIRECT = 0x4D014

disk_handle=win32file.CreateFile("\\\\.\\j:",
                                 win32con.GENERIC_READ|win32con.GENERIC_WRITE|win32con.GENERIC_EXECUTE,
                                 win32file.FILE_SHARE_READ |win32file.FILE_SHARE_WRITE,
                                 None,
                                 win32con.OPEN_EXISTING,
                                 0,
                                 None);

data = array.array("B", byte_list)
string = win32file.DeviceIoControl(disk_handle,IOCTL_SCSI_PASS_THROUGH_DIRECT, data, 0, None)


It seems that Windows excepted the control code, cause if I use a
wrong one, I get an SYSTEM ERROR 50 - which is 'The request is not
supported.'

If I use the right control code I get sometimes the SYSTEM ERROR
1450 - "Insufficient system resources exist to complete the requested
service."

So it seemsfor me, that at least a contact to the USB driver is
possible.
What do you think?

Thanks again for your help.
Sebastian



More information about the python-win32 mailing list