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:

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);

Help will be greatly appreciated.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.