Python Win32 API question.

David Bolen db3l at fitlinxx.com
Thu Jan 2 11:21:50 EST 2003


yaipa at yahoo.com (yaipa h.) writes:

> 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? It's looking more and more 
> like I will need to call a COM/DLL from my Python code to get this info. :((

Yes, you should be able to make the calls you want directly from
Python.  I agree however, that unless you have a development kit it
may be tricky to figure out what they are, since they are not built
into win32all.  In my case, I just reference the winioctl.h file from
my VC installation.  If you don't have some sort of developers kit, it
looks like other development environments like Cygwin (and probably
MingW) include this header as part of their win32 API collection, so
you can obtain it that way.

Using the functions themselves are as documented in MSDN -
DeviceIoControl is part of the Win32 core platform API, not MFC.  You
have to make sure that the buffer size you request in the
DeviceIoControl function matches the documented structure, and then
unpack the buffer manually based on that structure definition.  But
all of that information is available in MSDN.

> Thanks. An example IOCTL_ follows.
> 
> Yaipa.h
> 
> 'IOCTL_DISK_GET_DRIVE_GEOMETRY' Operation

Here's some example code using this particular call (note I've
hardcoded the IOCTL constant following interpretation of winioctl.h):

          - - - - - - - - - - - - - - - - - - - - - - - - -
import struct
import win32api
import win32file

if __name__ == "__main__":

    print 'Checking disk C:'

    info = win32api.GetVolumeInformation('c:\\')
    print 'Filesystem:', info[-1]

    hDisk = win32file.CreateFile(r'\\.\PhysicalDrive0',
                                 0,
                                 win32file.FILE_SHARE_READ|
                                 win32file.FILE_SHARE_WRITE,
                                 None,
                                 win32file.OPEN_EXISTING,
                                 0,
                                 None)

    IOCTL_DISK_GET_DRIVE_GEOMETRY=0x70000

    info = win32file.DeviceIoControl(hDisk,
                                     IOCTL_DISK_GET_DRIVE_GEOMETRY,
                                     '',24)

    # Values = (cylinders[high],cylinders[low],media_type,
    #           tracks_per_cylinder,sectors_per_track,bytes_per_sector)
    values = struct.unpack('6L',info)

    cylinders = (values[1]<<32)+values[0]
    size = cylinders * values[3] * values[4] * values[5]

    print 'Total disk space: %d (%dMB)' % (size,size/(1024*1024))
          - - - - - - - - - - - - - - - - - - - - - - - - -


--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/




More information about the Python-list mailing list