c++qtusbqt5removable-storage

Qt safely remove a device


I'm look for a way to safely remove my USB key from my Qt 5.2 application but I can't find any Qt module to do that.

Is their a way to do it or do I have to hardcode it ?


Solution

  • If you mean unmounting your usb device by removal, then there is no cross-platform solution for this. Perhaps there could be added something in the QtSystems module, however the problem is that this would require admin permission or some tricks, e.g. setuid or caps on Linux and so on.

    You could do something along these lines to achieve this feature for now on your side:

    void MyClass::unmount() {
    #ifdef Q_OS_LINUX
        // See details: http://linux.die.net/man/2/umount
        if (umount(myUsbKeyPath) < 0)
            qDebug() << "Failed to umount";
    #elif Q_OS_WIN
        // See details: http://support.microsoft.com/default.aspx?scid=kb;en-us;165721
        DWORD dwBytesReturned;
        DeviceIoControl(hVolume,
                        IOCTL_STORAGE_EJECT_MEDIA,
                        NULL, 0,
                        NULL, 0,
                        &dwBytesReturned,
                        NULL);
    #endif
    }