How to eject USB flash disk?
-
Hi, all
I am developing a Qt application to transmit and receive files via USB, and read some posts in the forum, now I have detected the plug-in of the USB flash disk, but I don't know how to eject it, could you help me please?
Best regards!Reference blog: https://blog.csdn.net/u014597198/article/details/72820737
PC: Windows7 64bits SP1
Qt: 5.12.4
QtCreator: 4.9.2 -
@tovax I don't think you can do this with Qt. You will have to use native APIs or things like "umount" on Linux.
-
Hi, all
I am developing a Qt application to transmit and receive files via USB, and read some posts in the forum, now I have detected the plug-in of the USB flash disk, but I don't know how to eject it, could you help me please?
Best regards!Reference blog: https://blog.csdn.net/u014597198/article/details/72820737
PC: Windows7 64bits SP1
Qt: 5.12.4
QtCreator: 4.9.2 -
@tovax I don't think you can do this with Qt. You will have to use native APIs or things like "umount" on Linux.
-
@tovax I don't think you can do this with Qt. You will have to use native APIs or things like "umount" on Linux.
-
It's more a C++ problem.
It can be done with c++ (https://cboard.cprogramming.com/cplusplus-programming/172697-cplusplus-eject-usb-stick.html, assuming the code, that was posted there, works)
-
@jsulm @Pl45m4
I have been using the "usb-ejecter.exe" downloaded from the Internet before, and recently I found the code to eject the USB flash drive on win10, and it is currently running well.bool usbDriveEject(const QString letter) { QString device_path = QStringLiteral("%1:\\").arg(letter); QString error_string; const char* temp = "\\\\.\\"; char device_path1[10] = { 0 }; memcpy(device_path1, temp, strlen(temp)); QByteArray dp = device_path.toLocal8Bit(); device_path1[4] = dp.at(0); device_path1[5] = dp.at(1); HANDLE handleDevice = CreateFileA(device_path1, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr); bool is_handle_invalid = (handleDevice == INVALID_HANDLE_VALUE); if (is_handle_invalid) { error_string = "Device is not connection to system!"; qDebug() << GetLastError(); return false; } // Do this in a loop until a timeout period has expired const int try_lock_volume_count = 3; int try_count = 0; for (; try_count < try_lock_volume_count; ++try_count) { DWORD dwBytesReturned; if (!DeviceIoControl(handleDevice, FSCTL_LOCK_VOLUME, nullptr, 0, nullptr, 0, &dwBytesReturned, nullptr)) { qDebug() << "Device is using....." << try_count; break; } QThread::sleep(1); } if (try_count == try_lock_volume_count) { error_string = "Device is using, try again later"; CloseHandle(handleDevice); return false; } DWORD dwBytesReturned = 0; PREVENT_MEDIA_REMOVAL PMRBuffer; PMRBuffer.PreventMediaRemoval = FALSE; if (!DeviceIoControl(handleDevice, IOCTL_STORAGE_MEDIA_REMOVAL, &PMRBuffer, sizeof(PREVENT_MEDIA_REMOVAL), nullptr, 0, &dwBytesReturned, nullptr)) { error_string = QStringLiteral("Unmount failed! error code:%1").arg(GetLastError()); qDebug() << "DeviceIoControl IOCTL_STORAGE_MEDIA_REMOVAL failed:" << GetLastError(); CloseHandle(handleDevice); return false; } long bResult = 0; DWORD retu = 0; bResult = DeviceIoControl(handleDevice, IOCTL_STORAGE_EJECT_MEDIA, nullptr, 0, nullptr, 0, &retu, nullptr); if (!bResult) { error_string = QStringLiteral("Disconnect IGU failed! error code:%1").arg(GetLastError()); CloseHandle(handleDevice); qDebug() << "Disconnect IGU IoControl failed error:" << GetLastError(); return false; } CloseHandle(handleDevice); return true; }