views:

106

answers:

2

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.

+1  A: 

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.

Helen
+1  A: 

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
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
You can use the open command to initialize a specific drive. I updated the answer accordingly.
Garett
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