Python Win32 API question.

Tim Roberts timr at
Thu Jan 2 06:44:06 CET 2003

yaipa at (yaipa h.) wrote:
> Good Cheer and Happy Holidays. In Mark Hammond's win32 toolkit there is
>a device I/O control method (win32file.DeviceIoControl). Has anyone had
>a chance to use it? If so could you provide an example of it in action.
>Also, the parameter 'dwIoControlCode' is said to use an IO control code,
>but none are listed and this library does not hold to the MFC library's
>naming convention so no chance on looking it up. :(

Did you try it?  DeviceIoControl most certainly IS part of the Win32 API,
as are all of the methods in win32file.  Look it up in MSDN.

BTW, this has nothing to do with MFC.  It's Win32.

>Finally, for the big points. Anyone belive that I will be able to use
>any of the 'IOCTL_*' enumerated values from the MFC toolkit with the 
>win32file.DeviceIoControl library method?

Absolutely.  Any ioctl you find in the Win32 include files can be used with
DeviceIoControl, assuming you can figure out which device to send them to,
and assuming you expand the macro to get a hex value for the ioctl.  The
first parameter to DeviceIoControl is the file handle of the driver that
will handle the ioctl.

>Returns information about the physical disk's geometry 
>(media type, number of cylinders, tracks per cylinder, 
>sectors per track, and bytes per sector).



Which should be 0x00070000.

>win32file Function Header
>string = DeviceIoControl(hFile, dwIoControlCode , data , readSize , ol )
>Call DeviceIoControl Parameters
>hFile           : int
> # Handle to the file
>dwIoControlCode : int 
> # IOControl Code to use.
>data            : string
> # The data to write.
>readSize        : int
> # Size of the buffer to create for the read.
>ol=None         : PyOVERLAPPED
> # An overlapped structure

So, you'd need to call win32file.CreateFile to open the special file
\\DISK0 and get a handle.  Then you can call:

handle = ...CreateFile icky details omitted...
s = DeviceIoControl(handle, IOCTL_DISK_GET_DRIVE_GEOMETRY, 0, 0, 0)

That ioctl doesn't take any input data, so you don't have to pass anything.
The information comes back to you in the string, which you will have to
parse with something like the struct module.
- Tim Roberts, timr at
  Providenza & Boekelheide, Inc.

More information about the Python-list mailing list