Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Python Win32 API question.

516 views
Skip to first unread message

yaipa h.

unread,
Dec 31, 2002, 7:27:58 PM12/31/02
to
All,

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. :(

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. :((

Thanks. An example IOCTL_ follows.

Yaipa.h

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

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

Tim Roberts

unread,
Jan 2, 2003, 12:44:06 AM1/2/03
to
ya...@yahoo.com (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.

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

That's:

#define IOCTL_DISK_GET_DRIVE_GEOMETRY CTL_CODE(IOCTL_DISK_BASE, 0x0000,
METHOD_BUFFERED, FILE_ANY_ACCESS)

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:

IOCTL_DISK_GET_DRIVE_GEOMETRY = 0x00070000
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, ti...@probo.com
Providenza & Boekelheide, Inc.

David Bolen

unread,
Jan 2, 2003, 11:21:50 AM1/2/03
to
ya...@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: db...@fitlinxx.com /
| FitLinxx, Inc. \ Phone: (203) 708-5192 |
/ 860 Canal Street, Stamford, CT 06902 \ Fax: (203) 316-5150 \
\-----------------------------------------------------------------------/

yaipa h.

unread,
Jan 2, 2003, 1:11:53 PM1/2/03
to
Tim,

Many Thanks, friend.

Regards of the best kind.

Alan Haffner

Tim Roberts <ti...@probo.com> wrote in message news:<ack71v4i13bucjdm7...@4ax.com>...

yaipa h.

unread,
Jan 3, 2003, 12:42:30 AM1/3/03
to
David,

This is great. Thanks

I have the DDK, but am new to it and sometimes where they poke things
doesn't make much sense. :(. 'winioctl.h' is the lead I need for the
Constant codes. The code below is a great help. It is always easier to
learn new techniques when you already have something that works already.

Regards of the best kind,

Alan Haffner

David Bolen <db...@fitlinxx.com> wrote in message news:<u8yy3a...@fitlinxx.com>...

0 new messages