Daniele
Daniele

Reputation: 23

USB self disconnect

I am not familiar with USB and I am not able (with delphi) to improve my software with a self disconnect USB function.

The program is on and runs from a USB memory drive (e. g. F:/) and when it stop what I try to do is to auto eject the device.

I also tried to do a .bat file that runs a program that disable the USB device. If this .bat file is launched from local drive (C:/) everything is ok, but if this .bat file is launched from USB stick (F:/) it fails.

Here the link where I got the information:

How can i remove a USB flash disk programmatically using delphi?

Any suggestion in delphi?

Upvotes: 0

Views: 420

Answers (1)

David Heffernan
David Heffernan

Reputation: 613592

The problem you have is the the process you are executing was run from thee drive that you are ejecting. The executable file is locked and that prevents your drive from being ejected.

It is clear what you need to do. You must make sure that no files on the drive are locked. That means that you must terminate your process. You then need another process, run from a different drive, to perform the ejection. Do the following:

  1. From process A, write an executable file, to a drive other than the drive to be ejected. For instance write to the temp directory.
  2. Start this other executable, process B say, with a call to CreateProcess. Pass an inheritable handle for process A to process B on the command line.
  3. Process A terminates.
  4. Process B waits until the handle to process A is signaled and then ejects the drive.
  5. Process B terminates.

Upvotes: 2

Related Questions