[python-win32] Eject a CD with win32file?

Moore, Paul Paul.Moore@atosorigin.com
Mon, 25 Nov 2002 09:23:24 -0000


I use this:

import sys
import win32file
from win32con import *

drive =3D sys.argv[1]
if len(drive) !=3D 2 or drive[1] !=3D ':':
    print >>sys.stderr, "Invalid drive letter", drive
    sys.exit(1)

h =3D win32file.CreateFile(r'\\.\\' + drive, GENERIC_READ, =
FILE_SHARE_READ,
                         None, OPEN_EXISTING, 0, 0)
n =3D win32file.DeviceIoControl(h,0x002d4808, "", 0)
win32file.CloseHandle(h)

My magic 0x002d4808 is IOCTL_STORAGE_EJECT_MEDIA. I got it from the =
Visual
C Headers. If you don't have Visual C, the same constants are in the =
Mingw
win32 API library. It's a shame the values of these constants aren't
documented in MSDN.

I think the rest of the differences between my code and yours are =
security-
related - I don't know if any of them are important.

Paul.

-----Original Message-----
From: Joel Lawhead [mailto:jlawhead@bellsouth.net]
Sent: 23 November 2002 22:24
To: python-win32@python.org
Subject: [python-win32] Eject a CD with win32file?


Hi,
I'm trying to programatically eject a cd on a Win2k machine in Python.=20
I found a simple working example in C with source and an executable at:
<http://memberwebs.com/nielsen/windows/eject/>.

Based on that program and some research on MSDN it appears the steps for
ejecting a CD-ROM are to first get a file handle for the device using
win32file.CreateFile and then call win32file.DeviceIoControl() with the
IOCTL_STORAGE_EJECT_MEDIA flag.

I tried:

import win32file
from win32con import *

# Get a file handle for the CD-ROM device
hdevice =3D win32file.CreateFile('\\\\.\\G:', 0, =
0,None,OPEN_EXISTING,0,0)

# Eject the cd
win32file.DeviceIoControl(hdevice,2,"", 0, None)

The win32file.CreateFile() returns without error. But the
win32file.DeviceIoControl() call returns:

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
pywintypes.api_error: (1, 'DeviceIoControl', 'Incorrect function.')

The second parameter in DeviceIoControl is an integer but I don't know
what it is and have been guessing with several numbers to no avail. All
the documentation simply refers to the constant
"IOCTL_STORAGE_EJECT_MEDIA" and not an integer. So I'm not sure if that
call is my problem or if it's something else.

To get the job done of course I could always make an os.system() call to
the command line "eject.exe" program I mentioned above, but I feel like
I'm pretty close to doing it with Python and the win32 extenstions.=20

I don't have a lot of experience working with windows mfc calls so any
help would be appreciated.

Thanks,
Joel

_______________________________________________
Python-win32 mailing list
Python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32