[python-win32] USB access using win32file.deviceIOcontrol

Tim Roberts timr at probo.com
Wed Dec 12 20:29:05 CET 2007


Sebastian Friebe wrote:
> 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(
> ...
> 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.
>   

OK, you talked me into it.

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

That number is correct.

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

GENERIC_EXECUTE should not be required, but if your C++ example had it,
you might as well keep it.  Does the CreateFile succeed?  You get a
reasonable handle?  Note that on Vista you must be elevated to open a
volume directly.

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

That's fundamentally correct.  How are you creating the
SCSI_PASS_THROUGH structure in "byte_list"?  Are you sure it is 42
bytes?  Are you setting all the fields correctly?  How are you setting
the DataBuffer pointer?  Have you set the Length field correctly?

I would have guessed it would be easier to use the struct module to
build the buffer, rather than array.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list