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. Detect USB drive and its related information.
Forum Updated to NodeBB v4.3 + New Features

Detect USB drive and its related information.

Scheduled Pinned Locked Moved General and Desktop
11 Posts 6 Posters 14.5k 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.
  • V Offline
    V Offline
    vivekmalik2466
    wrote on last edited by
    #1

    I need to develop a software that detects when a USB drive is plugged in the a computer, get it's friendly name & drive letter.

    i am trying with GetVolumeInformation().how to i do get drive information with it.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      luca.cossaro
      wrote on last edited by
      #2

      Hi,

      if you want do to that on Windows:

      1. register to device notification
        @
        DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE, 0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);

      // ...

      DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;

      ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
      NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
      NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
      NotificationFilter.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE;

      HDEVNOTIFY hDevNotify = RegisterDeviceNotification(winId, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
      @

      1. in winEvent check the message:
        @
        if (WM_DEVICECHANGE == message->message) {

      switch(message->wParam) {
      case DBT_DEVICEARRIVAL:
      msgType = "WM_DEVICECHANGE DBT_DEVICEARRIVAL: ";
      interest = true;
      // device plugged in
      break;

          case DBT_DEVICEREMOVECOMPLETE :
              msgType = "WM_DEVICECHANGE DBT_DEVICEREMOVECOMPLETE: ";
              interest = true;
              // device plugged out
              break;
          }
      

      }

      if (interest) {
      DEV_BROADCAST_HDR* pMsgPtr = (DEV_BROADCAST_HDR*)message->lParam;
      if (pMsgPtr) {
      switch (pMsgPtr->dbch_devicetype) {
      case DBT_DEVTYP_DEVICEINTERFACE:
      // retrieve vid and pid
      break;
      case DBT_DEVTYP_VOLUME:
      DEV_BROADCAST_VOLUME* volume = (DEV_BROADCAST_VOLUME*)pMsgPtr;
      // volume gives you all information on mount point
      break;
      }
      }
      }
      @

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vivekmalik2466
        wrote on last edited by
        #3

        i want to get

        1. list of drives
          2.total size
          3.used space
          4.free space
        1 Reply Last reply
        0
        • V Offline
          V Offline
          vivekmalik2466
          wrote on last edited by
          #4

          Dear can you please let me know all the respective header files .i am unable to register it.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            luca.cossaro
            wrote on last edited by
            #5

            Hi,

            these are the headers:
            @
            #include <initguid.h>
            #include <dbt.h>
            @

            for information on disk space you can use GetDiskFreeSpaceEx (see "MSDN":http://msdn.microsoft.com/en-us/library/windows/desktop/aa364937.aspx for documentation)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mxb7642
              wrote on last edited by
              #6

              I am probably missing something obvious, but when trying to compile this code, I get the following errors:

              mainwindow.cpp: In constructor 'MainWindow::MainWindow(QWidget*)':
              mainwindow.cpp:19: error: 'DEV_BROADCAST_DEVICEINTERFACE' was not declared in this scope
              mainwindow.cpp:24: error: 'DEVICE_NOTIFY_WINDOW_HANDLE' was not declared in this scope
              mainwindow.cpp:24: error: 'RegisterDeviceNotificationW' was not declared in this scope

              I've included initguid.h, dbt.h, as well as windows.h and winuser.h. Am I missing something?

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tobias.hunger
                wrote on last edited by
                #7

                That is of course not at all portable. If you care about other platforms you could put this code behind a nice interface that can get extended to other platforms later.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mxb7642
                  wrote on last edited by
                  #8

                  That is what I intend to do but the errors I'm getting are on Windows 7 professional SP1.

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    josh h
                    wrote on last edited by
                    #9

                    You need to specify which minimum version of Windows you are building for by setting WINVER. These routines were not available on older versions windows so they are only defined in the header files if WINVER is greater than or equal to 0x500.

                    Try adding the following line before you include the files windows.h and dbt.h:

                    #define WINVER 0x0501

                    You could also add the define to the project file.

                    See the following MSDN article for details on setting WINVER:
                    http://msdn.microsoft.com/en-gb/library/windows/desktop/aa383745

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      vivekmalik2466
                      wrote on last edited by
                      #10

                      this function give u the infomation of usb drive plz pass drive letter as parameter .

                      #include <winbase.h>
                      #include <windows.h>
                      #include <initguid.h>
                      #include <dbt.h>

                      @bool IsUsbDevice( wchar_t letter )
                      {
                      wchar_t volumeAccessPath[] = L"\\.\X:";
                      volumeAccessPath[4] = letter;

                      HANDLE deviceHandle = CreateFileW(
                                  volumeAccessPath,
                                  0,                // no access to the drive
                                  FILE_SHARE_READ | // share mode
                                  FILE_SHARE_WRITE,
                                  NULL,             // default security attributes
                                  OPEN_EXISTING,    // disposition
                                  0,                // file attributes
                                  NULL);            // do not copy file attributes
                      
                      // setup query
                      STORAGE_PROPERTY_QUERY query;
                      memset(&query, 0, sizeof(query));
                      query.PropertyId = StorageDeviceProperty;
                      query.QueryType = PropertyStandardQuery;
                      //query.AdditionalParameters.
                      
                      // issue query
                      DWORD bytes;
                      STORAGE_DEVICE_DESCRIPTOR devd;
                      STORAGE_BUS_TYPE busType = BusTypeUnknown;
                      
                      if (DeviceIoControl(deviceHandle,
                                          IOCTL_STORAGE_QUERY_PROPERTY,
                                          &query, sizeof(query),
                                          &devd, sizeof(devd),
                                          &bytes, NULL))
                      {
                          busType = devd.BusType;
                          //qint64 abc=devd.Size;
                          //GET_LENGTH_INFORMATION *sizeInfo;
                          //DeviceIoControl(lDevice, IOCTL_DISK_GET_LENGTH_INFO, NULL, 0, sizeInfo, sizeof(sizeInfo), &junk, (LPOVERLAPPED) NULL);
                      } else {
                          std::wcout << L"Failed to define bus type for: " << letter;
                      }
                      CloseHandle(deviceHandle);
                      return BusTypeUsb == busType;
                      

                      }
                      @

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        CarlStenquist
                        wrote on last edited by
                        #11

                        Hi, please note that this thread deals with the USB class MSC, Mass Storage, only (right?) and so if the thread title were to be adjsted it would be more clear.
                        I'm searching for the HID class, but cannot find any Qt interface to it so far on Windows .

                        P.S. I'm not really a "lab rat". Just an applications engineer.

                        1 Reply Last reply
                        0

                        • Login

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