How to stop USB mass storage device programmatically

Hi,

I have searched the list before asking this question,
but could not find enough information. Sorry…

I would like to be able to safely stop the USB mass
storage device (USB flash drive) from a Win32 program.
I.e. to act the same way, as Unplug or Eject Hardware
applet, when you choose "Stop USB Mass Storage device

  • Drive(N:)".

How can I do this?

Thank you very much in advance.

Regards,
Leonid.

Open the drive exclusively and as if you were going to send SCSI passthru
requests. This will force the system to flush all dirty buffers to the
media. You can then send down and eject IoCtl if you need it. You might
need admin rights for some of the requests.

“Leonid Zhigunov” wrote in message
news:xxxxx@ntdev…
>
> Hi,
>
> I have searched the list before asking this question,
> but could not find enough information. Sorry…
>
> I would like to be able to safely stop the USB mass
> storage device (USB flash drive) from a Win32 program.
> I.e. to act the same way, as Unplug or Eject Hardware
> applet, when you choose “Stop USB Mass Storage device
> - Drive(N:)”.
>
> How can I do this?
>
> Thank you very much in advance.
>
> Regards,
> Leonid.
>
>
>

Thank you very much for reply.

Maybe I am doing something wrong,
but this method does not help.

I open the drive, lock it and try to dismount.
The dismount request fails. The subsequent
IOCTL STORAGE_EJECT_MEDIA have no effect.

How does the “Unplug or Eject Hardware” applet work?
What steps should I perform to stop USB Mass Storage
device?

Hope for your help.

Regards,
Leonid.

Open the drive exclusively and as if you were going to send SCSI passthru
requests. This will force the system to flush all dirty buffers to the
media. You can then send down and eject IoCtl if you need it. You might
need admin rights for some of the requests.

“Leonid Zhigunov” wrote in message
> news:xxxxx@ntdev…
> >
> > Hi,
> >
> > I have searched the list before asking this question,
> > but could not find enough information. Sorry…
> >
> > I would like to be able to safely stop the USB mass
> > storage device (USB flash drive) from a Win32 program.
> > I.e. to act the same way, as Unplug or Eject Hardware
> > applet, when you choose “Stop USB Mass Storage device
> > - Drive(N:)”.
> >
> > How can I do this?
> >
> > Thank you very much in advance.
> >
> > Regards,
> > Leonid.
> >
> >
> >

The source code for the DevCon sample in the DDK is a good example of
how to use the device manager to do this. Here’s how you can do this
at the command prompt with DevCon. Find determine the device IDs for
all USB drives.

C:>devcon find USBSTOR\DISK*

USBSTOR\DISK&VEN_WDC_WD12&PROD_00BB-00CAA1&REV_17.0\0B020142050CA1A7&0:
WDC WD12 00BB-00CAA1 USB Device
1 matching device(s) found.

Select the disk you want to disable and then use DevCon to disable it:

C:>devcon disable USBSTOR.…A1A7&0:

USBSTOR\DISK&VEN_WDC_WD12&PROD_00BB-00CAA1&REV_17.0\0B020142050CA1A7&0:
Disabled

1 device(s) disabled.

To enable the device use:

C:>devcon enable USBSTOR.…A1A7&0:

USBSTOR\DISK&VEN_WDC_WD12&PROD_00BB-00CAA1&REV_17.0\0B020142050CA1A7&0:
Enabled
1 device(s) enabled.

You will need to use the entire device ID up to the colon (:), I used
the … for brevity so the line wouldn’t wrap in the reply.

…John

On Fri, 29 Aug 2003 09:19:16 -0400, “Leonid Zhigunov”
wrote:

>
>Hi,
>
>I have searched the list before asking this question,
>but could not find enough information. Sorry…
>
>I would like to be able to safely stop the USB mass
>storage device (USB flash drive) from a Win32 program.
>I.e. to act the same way, as Unplug or Eject Hardware
>applet, when you choose “Stop USB Mass Storage device
>- Drive(N:)”.
>
>How can I do this?
>
>Thank you very much in advance.
>
>Regards,
>Leonid.
>
>

Hello all,

Be careful to use this function:

devcon disable USBSTOR\DISK *

“John Hensley” ???:xxxxx@ntdev…
>
>
> The source code for the DevCon sample in the DDK is a good example of
> how to use the device manager to do this. Here’s how you can do this
> at the command prompt with DevCon. Find determine the device IDs for
> all USB drives.
>
> C:>devcon find USBSTOR\DISK*
>
> USBSTOR\DISK&VEN_WDC_WD12&PROD_00BB-00CAA1&REV_17.0\0B020142050CA1A7&0:
> WDC WD12 00BB-00CAA1 USB Device
> 1 matching device(s) found.
>
>
> Select the disk you want to disable and then use DevCon to disable it:
>
> C:>devcon disable USBSTOR.…A1A7&0:
>
> USBSTOR\DISK&VEN_WDC_WD12&PROD_00BB-00CAA1&REV_17.0\0B020142050CA1A7&0:
> Disabled
>
> 1 device(s) disabled.
>
>
> To enable the device use:
>
> C:>devcon enable USBSTOR.…A1A7&0:
>
> USBSTOR\DISK&VEN_WDC_WD12&PROD_00BB-00CAA1&REV_17.0\0B020142050CA1A7&0:
> Enabled
> 1 device(s) enabled.
>
> You will need to use the entire device ID up to the colon (:), I used
> the … for brevity so the line wouldn’t wrap in the reply.
>
> …John
>
>
> On Fri, 29 Aug 2003 09:19:16 -0400, “Leonid Zhigunov”
> wrote:
>
> >
> >Hi,
> >
> >I have searched the list before asking this question,
> >but could not find enough information. Sorry…
> >
> >I would like to be able to safely stop the USB mass
> >storage device (USB flash drive) from a Win32 program.
> >I.e. to act the same way, as Unplug or Eject Hardware
> >applet, when you choose “Stop USB Mass Storage device
> >- Drive(N:)”.
> >
> >How can I do this?
> >
> >Thank you very much in advance.
> >
> >Regards,
> >Leonid.
> >
> >
>
>
>