none
USB Dismount not turning off my USB Drive? RRS feed

  • Question

  • 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.

    When i start transferring data to my USB Drive 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 the part of my code that unmounts the drive:
    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 with my code while is being written dangerous?

    Help will be appreciated.
    Tuesday, December 9, 2008 4:55 AM

Answers

  • Windows assumes removable drives will be yanked and it disables write behind caching to make sure the drive can be removed as soon as a dialog dismisses.
    I am a professional developer and I am registered with Microsoft Connect. I am also a vegan. I also do web development. Economics and chess are two of my many interests.
    Tuesday, December 9, 2008 4:37 PM

All replies

  • Does some one has a remote idea if this is how it is supposed to work?
    Tuesday, December 9, 2008 4:23 PM
  • What do you mean "Windows does this automatically"?
    Tuesday, December 9, 2008 4:35 PM
  • Start transferring a folder to an USB drive that you don't care if it looses data and yank it.

    You will need to restart windows to be able to use the USB drive again cuz windows will not recognize it.

    I'm making this part of the code so that if you are transferring data to the drive the code stops it, so you don't loose data if you remove the USb drive(Pen Drive,Flash Drive,what ever you cant to call it).
    Tuesday, December 9, 2008 4:44 PM