We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Replicate Computer / Disk Management eject procedure (in script)

Medium Priority
Last Modified: 2014-03-21
Within Disk Management (of Computer Management), I'd like to write a script, .bat, .vbs, visual basic etc, that will replicate the eject feature.

Ejecting via Windows Explorer won't work. The USB cartridge for backups that I'm using, will only eject via the computer management eject procedure, so I'd like to script this.

Using windows 2008... and have VB6.

Can anyone help?
Watch Question

Top Expert 2010

I am pretty syre they both perform the same routine.

Here is how you would eject a removable device using API.

1) Open a (volume) handle to the device using CreateFile()
2) Try to lock the volume using FSCTL_LOCK_VOLUME
3) If the call above succeeds it's safe to dismount the volume using FSCTL_DISMOUNT_VOLUME
4) You want to make sure there is no (prevent removal locks) on the volume so you call IOCTL_STORAGE_MEDIA_REMOVAL and set the structure parameter to False.
5) Use IOCTL_STORAGE_EJECT_MEDIA to eject the device.
6) Close the volume handle.

This is how all of them work but I will take a wild guess and say that maybe explorer doesn't go as far to disable any ejection locks but the disk managment eject option does.
Top Expert 2010
Unlock this solution and get a sample of our free trial.
(No credit card required)


Hi. Many thanks for your help. This worked! I really thought I'd be stuck with this problem. There does seem to be some difference between explorer and disk management, perhaps as you say it's to do the locks.
Many thanks though!
Top Expert 2010

Great. So it seems that the computer managment is doing some additional work than explorer thats interesting.

I was messing around with the (Safe Remove Hardware option) and I noticed that it actually updates explorer and removes the drive from the explorer list. Which the code above doesn't do so in addition the safely remove hardware goes even one step further and updates explorer.

Here is how it works.

Option Explicit
Private Const SHCNE_DRIVEREMOVED As Long = &H80
Private Const SHCNF_PATHW As Long = 5
Private Declare Function SHChangeNotify Lib "shell32.dll" (ByVal wEventId As Long, _
  ByVal uFlags As Long, ByVal dwItem1 As Long, ByVal dwItem2 As Long) As Long
Private Sub Update(ByVal szDrive As String)
' Do some magic here.(must have trailing backslash)
Call SHChangeNotify(SHCNE_DRIVEREMOVED, SHCNF_PATHW, StrPtr(szDrive), 0)
End Sub

Open in new window

Top Expert 2010

Just to make sure, you would call the above method after you call the eject part.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.