How to eject CD using WMI and Python?

送分小仙女□ 提交于 2019-12-03 13:02:33

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

Helen

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!