2

I am trying to eject an optical disc on a Windows PC connected to a robotic disc changer. I have the following powershell script which works most of the time:

$path=$args[0]
$sh = New-Object -Comobject "Shell.Application"
$sh.Namespace(17).Items() | 
    Where-Object { $_.Type -eq "Removable Disc" } |
    foreach { 
        if($_.Path -eq $path) { $_.InvokeVerb("Eject") }
    }

However, sometimes it doesn't work, presumably because Windows thinks the drive is still in use.

Is there any way I can get a response code from this to tell if it has successfully ejected the disc? Or otherwise tell if the disc is still mounted?

Alternatively, does anyone know a nice C++ way of doing it, which would be even better as I could avoid the need to call a separate script...

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Nick C
  • 319
  • 8
  • 23
  • You could, for example, loop over a brief routine which first tries to eject the disc, and then checks after a moment to see whether it can get a directory listing of the drive's root; if so, the drive failed to eject and you'd want to loop around again, while if not, you could bomb out of the loop with confidence that the drive had actually ejected. Unpleasantly heuristic, sure, but unless `InvokeVerb` has some kind of sensible return value, you'll probably be stuck with something like it. – Aaron Miller Sep 13 '13 at 16:04
  • That was my first thought, but I was hoping there would be a nicer way of doing it... – Nick C Sep 13 '13 at 16:14
  • Again, if you can get a sensible return value back from `InvokeVerb`, then you can just check that and it'll tell you whether or not the eject succeeded. If you can't, though, your only alternatives are to use a heuristic loop, or to reimplement in some other language that does a better job exposing the Windows API, at much higher cost in time and complexity. (I assume here, of course, that even the C API makes it possible to know whether an attempt to eject a drive succeeded, but this being the Windows API, that assumption may well be in error.) – Aaron Miller Sep 13 '13 at 16:17

0 Answers0