DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: USB Dismount not turning off my USB Drive?

  1. #1
    Join Date
    Dec 2008
    Posts
    4

    USB Dismount not turning off my USB Drive?

    I'm making an app that needs to safely remove USB Drives and SD cards.

    When i execute my code,the drive gets forced not to be ready.

    I have an old 128MB usb drive and when i start transferring data to it and execute my code,a msg box pops up saying: USB Cannot copy to "FileName" Device not ready.

    And also,when i execute the code the drive stays visible in the pc and can be accessed again without having to replug.

    Is that normal?
    Am i doing some thing wrong?
    Am i missing something?

    Heres my code:

    Code:
    HANDLE File;
        DWORD BytesReturned;
        char DrivePath[MAX_PATH] = "\\\\.\\J:";
        File = CreateFile(DrivePath,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);
        DeviceIoControl(File,FSCTL_LOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);
        DeviceIoControl(File,FSCTL_DISMOUNT_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);
        DeviceIoControl(File,IOCTL_STORAGE_MEDIA_REMOVAL,NULL,NULL,NULL,NULL,&BytesReturned,NULL);
        DeviceIoControl(File,FSCTL_UNLOCK_VOLUME,NULL,NULL,NULL,NULL,&BytesReturned,NULL);
        CloseHandle(File);
            if(&BytesReturned == 0)
            MessageBox("The Drive Could Not be Removed,Is being used by an app or has not finished an operation","Note",MB_OK);
            else
            MessageBox("You can now remove the USB Drive","Removed",MB_OK);
    Is Disabling the drive while is being in a read/write process dangerous?

    Help will be greatly appreciated.

  2. #2
    Join Date
    Dec 2007
    Posts
    401
    > when i execute the code the drive stays visible in the pc
    > and can be accessed again without having to replug.

    usb disk drives (without removable media) are akin to normal disks; they can have a partition which has a volume. unmounting the volume does not disconnect the disk device. the disk remains connected and the volume on it can be mounted again when required.

    to prepare a disk for safe removal, use CM_Request_Device_Eject_Ex() which is part of the config manager api (cfgmgr32.h). http://msdn.microsoft.com/en-us/library/ms790891.aspx

    here is a sample which illustrate thes use of this function: http://maveriik.blogspot.com/2007/10...-for-safe.html

    > Is Disabling the drive while is being in a read/write process dangerous?

    it is not dangerous if a read is active, but it can result in loss of data if a write is in progress or write cacheing is enabled on the disk.

    AFAIK, (short of physically pulling the drive out), removal fails as long as there is even one open handle to the disk or to the storage volume. so, this would not be a danger if you attempt to do this from code; you wouldn't be allowed to to it.

Similar Threads

  1. Dynamic drive mapping
    By prem.bhazeen in forum VB Classic
    Replies: 2
    Last Post: 06-20-2006, 11:34 AM
  2. Installing VB6 on a USB drive?
    By Crimplato in forum VB Classic
    Replies: 14
    Last Post: 05-05-2005, 08:41 PM
  3. Zip Drive
    By Dehreorn in forum Open Source
    Replies: 2
    Last Post: 05-07-2002, 03:19 PM
  4. Differentiating system drive types
    By Patrick Spence in forum VB Classic
    Replies: 4
    Last Post: 10-17-2000, 12:04 PM
  5. Differentiating system drive types
    By Patrick Spence in forum VB Classic
    Replies: 0
    Last Post: 10-17-2000, 10:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center