|
-
Jul 30th, 2011, 11:34 AM
#1
[VB6] Properly Eject CD/DVD and USB Drives Safely
Updated 20-January-2017
See post #20 for the attachment
There is a lot of code floating around the Web for programatically ejecting CD/DVDs and removable drives such as USB flash drives. Much of it is a bit flaky, and some of it works by using disapproved approaches involving registry spelunking and calls to APIs that are not meant for use by applications (worst offender: setupapi.dll).
In any case, Microsoft documented the proper process many years ago in the KB Support Article Q165721: How To: Ejecting Removable Media in Windows NT/Windows 2000/Windows XP
Of course the code example there is written in C, but it is easily converted to VB6 and refactored to remove a bunch of layered procedure calls. The result is a small static module EjectVolMod.bas with one public function:
EjectVolume(Driveletter)
I have wrapped this in a demo Project EjectDrive that you can try out. There are notes in MainMod.bas and in the ReadMe file to explain how to do IDE testing and compile this as a fully-functioning command line EXE. These notes only apply to the demo Project and are not required when using EjectVolume in your own programs.
There are constants relating to drive locking timeouts that you may wish to adjust. I found the KB Article's 10 seconds a bit long and shaved it to 3 seconds.
The code works in Vista and Windows 7. I assume since it was written for Win2K and XP it still works there too!
One thing the ReadMe fails to mention about the demo:
You can test in the IDE, or you can compile and prepare the EXE (as described in the ReadMe) for command line execution. There is no point in double-clicking the EXE, it is meant to be run from the command line passing the drive to eject as an argument, as in:
C:\Users\Fred\Desktop\EjectVolume>EjectDrive E
I hope that clarifies that point.
Last edited by dilettante; Jan 20th, 2017 at 08:30 AM.
Reason: removed broken attachment link
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|