How to eject CD by the script on Vista correctly.
Published on August 23, 2008 By Vad_M In DesktopX

There is a known way to eject CD on XP through Windows Media Player:

Sub EjectCD()
 On Error Resume Next
  Dim objWMP,objCDD
  Set objWMP = CreateObject("WMPlayer.OCX")
  Set objCDD = objWMP.cdromCollection
   If objCDD.count > 0 Then
    For i = 0 To objCDD.count - 1
     objCDD.Item(i).Eject
    Next  
   End If
  Set objCDD = nothing
  Set objWMP = nothing
End Sub

As well there is one more way that works more correctly (through FSO and WSH):

Const ssfDRVS = &H11

Sub EjectCD()
  Dim objFSO,objWSH,objCDD,cd
   cd = ""
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objWSH = CreateObject("Shell.Application")
  Set objCDD = objFSO.Drives
   For Each obj In objCDD
    If obj.DriveType = 4 Then
     cd = obj& "\" : Exit For
    End If
   Next 
  Set objCDD = nothing
   If cd <> "" Then
    Set objCDD = objWSH.Namespace(ssfDRVS).ParseName(cd)
     objCDD.InvokeVerb("E&ject")
    Set objCDD = nothing
   End If
  Set objFSO = nothing
  Set objWSH = nothing
  Set cd = nothing
End Sub

But this code have two problems:

1. It can work only on a computers with English version of Windows.

2. The InvokeVerb method doesn't work on Vista in general!

I found this today when I tried to add the control options for CD/DVD into my new Media Player... However the problem is already solved! Please look below:

Const ssfDRVS = &H11

Sub EjectCD()
  Dim objFSO,objWSH,objCDD,cd,n
   cd = ""
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objWSH = CreateObject("Shell.Application")
  Set objCDD = objFSO.Drives
   For Each obj In objCDD
    If obj.DriveType = 4 Then
     cd = obj& "\" : Exit For
    End If
   Next 
  Set objCDD = nothing
   If cd <> "" Then
    Set objCDD = objWSH.Namespace(ssfDRVS).ParseName(cd).Verbs
     n = (objCDD.count - 5) '<== Because the "Eject" item is always the fifth from the end!
     objCDD.Item(n).DoIt()
    Set objCDD = nothing
   End If
  Set objFSO = nothing
  Set objWSH = nothing
  Set cd = nothing
  Set n = nothing
End Sub

The last code works fine not only on XP but on Vista to.

That's all I wanted to tell at the moment...


Comments
on Aug 23, 2008
So from what I see here it gives it a count and there must be something else which is part of the OS to do this being it counts to give it action. Thus also you parsed the name.


Good find!!!


GT

on Aug 23, 2008
I see that Microsoft blocks more and more options in each new OS/Service Pack.    May be they think that it will protect the system?    Ha-Ha-Ha! This is just a Chronic Paranoia because any hacker will overcome this easily less than in a minute.

Conclusion: Microsoft's nonsense really disturbs only to developers which try to make service or administrative programs for Windows...