Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to eject USB flash disk?
Qt 6.11 is out! See what's new in the release blog

How to eject USB flash disk?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • tovaxT Offline
    tovaxT Offline
    tovax
    wrote on last edited by
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • jsulmJ jsulm

      @tovax I don't think you can do this with Qt. You will have to use native APIs or things like "umount" on Linux.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #3

      @tovax

      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)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      tovaxT 1 Reply Last reply
      2
      • tovaxT tovax

        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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        Pl45m4P tovaxT 2 Replies Last reply
        4
        • jsulmJ jsulm

          @tovax I don't think you can do this with Qt. You will have to use native APIs or things like "umount" on Linux.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #3

          @tovax

          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)


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          tovaxT 1 Reply Last reply
          2
          • jsulmJ jsulm

            @tovax I don't think you can do this with Qt. You will have to use native APIs or things like "umount" on Linux.

            tovaxT Offline
            tovaxT Offline
            tovax
            wrote on last edited by
            #4

            @jsulm Thank you very much! According to your suggestion, I found some useful blogs about my question.
            (https://blog.csdn.net/qq2399431200/article/details/52336230)

            Best regards!

            1 Reply Last reply
            0
            • Pl45m4P Pl45m4

              @tovax

              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)

              tovaxT Offline
              tovaxT Offline
              tovax
              wrote on last edited by
              #5

              @Pl45m4 Hi, Pl45m4, thank you very much for your reply, I will read this artical carefully!
              All the best!

              1 Reply Last reply
              0
              • tovaxT Offline
                tovaxT Offline
                tovax
                wrote on last edited by
                #6

                @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;
                }
                
                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved