Hi everyone, I'm doing some experiences with python in Windows Vista and I was wondering to know how to eject the tray of CDs.

Thanks, Dan.

Recommended Answers

All 2 Replies

Well, I'm not Dan, but here's a little hack adapted from http://www.dotnetspider.com/resources/15834-eject-close-cd-tray.aspx for Python. It's only two lines of code!
To open:

import ctypes
ctypes.windll.winmm.mciSendStringW("set cdaudio door open", None, 0, None)

To close:

import ctypes
ctypes.windll.winmm.mciSendStringW("set cdaudio door closed", None, 0, None)

Note, if you're using python 2.x, either change mciSendStringW to mciSendStringA, or change the first argument to a unicode string (recommended) by prefixing it with a lowercase u.

commented: nice Windows hack +12

Thanks Scru, it works perfectly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.