9

Using Windows' WMI library, how can I eject CD rom mounted in a specific CD/DVD drive?

I am asking for sources from WMI docs or examples since I am using wmi.py library on Python.

It would be great if solution satisfies Windows computer newer than Windows 2000 and having multi CD-ROMs. (i.e. I have D: F: drives and both are CD-ROM drives. I might want to eject cd in F: specifically.)

Searched on the net but could not find anything relevant. The last solution must be having 3rd party binaries and executing from the shell.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214

3 Answers3

15

You can use ctypes.

import ctypes

ctypes.windll.WINMM.mciSendStringW(u"set cdaudio door open", None, 0, None)

UPDATE:

If you have more that one drive, you can use to open command to initialize a specific device before calling the function above. For example (not tested).

ctypes.windll.WINMM.mciSendStringW(u"open D: type cdaudio alias d_drive", None, 0, None)
ctypes.windll.WINMM.mciSendStringW(u"set d_drive door open", None, 0, None)

Also, see the documentation on how to check return values

Garett
  • 16,632
  • 5
  • 55
  • 63
  • interestingly, didn't work for me. just prints 0 to console. I have 2 CD-ROM drives. D: and F:, D: forvirtually mounted ISO images, and F: for real cdrom drive. Maybe it is the reason? – ahmet alp balkan Jul 05 '10 at 15:19
  • You can use the open command to initialize a specific drive. I updated the answer accordingly. – Garett Jul 05 '10 at 19:19
  • 1
    I've found the problem. If I mount 2 CD-ROMs (lets say D: and F:) and D: (first in appearance order) is not ejectable drive (such as virtual mounting) it won't exit and as far as I observed, nothing solves this. However, if I disable this drive and use physical tray, it works! Thanks. – ahmet alp balkan Jul 21 '10 at 20:31
  • I would suggest closing the resource once the job is done: ctypes.windll.WINMM.mciSendStringW("close d_drive", None, 0, None) – Soitje Oct 25 '22 at 15:00
3

WMI itself doesn't provide means to eject CD/DVD drives. There're other solutions though, which involve using Windows API functions, for example:

  • Using the mciSendString function. Can't help you with the Python code, but here's the C# example to help you get the idea:

    mciSendString("open f: type cdaudio alias cdrom", null, 0, IntPtr.Zero);
    mciSendString("set cdrom door open", null, 0, IntPtr.Zero);
    
  • Using the DeviceIOControl function. An example (also in C#) is here.

Community
  • 1
  • 1
Helen
  • 87,344
  • 17
  • 243
  • 314
0

you can using pygame

pygame.cdrom.CD.eject()
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – PCM Nov 13 '21 at 05:28
  • 1
    Note that the [`pygame.cdrom`](https://www.pygame.org/docs/ref/cdrom.html) module _is non functional in pygame 2.0 and above, unless you have manually compiled pygame with SDL1. This module will not be supported in the future. One alternative for Python cdrom functionality is pycdio._ However, pycdio doesn't seem to work with recent Python version (3.10) either... This answer seems outdated. – wovano Jul 28 '22 at 19:23