0

trying to make project in QT, I need to detect any new usb device and return the letter in my main.cpp.

I found this with google and it should work but I don't know how to have a print of the driver letter in my main.cpp with simple qDebug() by calling the function char FirstDriveFromMask(ULONG unitmask).

Could you help me?

void Main_OnDeviceChange( HWND hwnd, WPARAM wParam, LPARAM lParam )
 {
  PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam;
  TCHAR szMsg[80];

  switch(wParam )
   {
    case DBT_DEVICEARRIVAL:
      // Check whether a CD or DVD was inserted into a drive.
      if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
       {
        PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;

        if (lpdbv -> dbcv_flags & DBTF_MEDIA)
         {
          StringCchPrintf( szMsg, sizeof(szMsg)/sizeof(szMsg[0]),
                           TEXT("Drive %c: Media has arrived.\n"),
                           FirstDriveFromMask(lpdbv ->dbcv_unitmask) );

          MessageBox( hwnd, szMsg, TEXT("WM_DEVICECHANGE"), MB_OK );
         }
       }
      break;

    case DBT_DEVICEREMOVECOMPLETE:
      // Check whether a CD or DVD was removed from a drive.
      if (lpdb -> dbch_devicetype == DBT_DEVTYP_VOLUME)
       {
        PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;

        if (lpdbv -> dbcv_flags & DBTF_MEDIA)
         {
          StringCchPrintf( szMsg, sizeof(szMsg)/sizeof(szMsg[0]),
                           TEXT("Drive %c: Media was removed.\n" ),
                           FirstDriveFromMask(lpdbv ->dbcv_unitmask) );

          MessageBox( hwnd, szMsg, TEXT("WM_DEVICECHANGE" ), MB_OK );
         }
       }
      break;

    default:
      /*
        Process other WM_DEVICECHANGE notifications for other
        devices or reasons.
      */
      ;
   }
}

/*------------------------------------------------------------------
   FirstDriveFromMask( unitmask )

   Description
     Finds the first valid drive letter from a mask of drive letters.
     The mask must be in the format bit 0 = A, bit 1 = B, bit 2 = C,
     and so on. A valid drive letter is defined when the
     corresponding bit is set to 1.

   Returns the first drive letter that was found.
--------------------------------------------------------------------*/

char FirstDriveFromMask( ULONG unitmask )
 {
  char i;

  for (i = 0; i < 26; ++i)
   {
    if (unitmask & 0x1)
      break;
    unitmask = unitmask >> 1;
   }

  return( i + 'A' );
}
Philippe
  • 25
  • 1
  • 7

1 Answers1

0

Either this:

#include <QDebug>
///
qDebug() <<
  "Drive" << FirstDriveFromMask(lpdbv ->dbcv_unitmask)  << ": Media has arrived";

or with a bit better formatting

qDebug() <<
  QString("Drive %1: Media has arrived").arg(FirstDriveFromMask(lpdbv ->dbcv_unitmask));

And if that output going to default debug console rather than Windows you have to follow the answer: Qt qDebug() doesn't work in Windows shell and make a small change in project.pro file:

CONFIG += console

Alexander V
  • 8,351
  • 4
  • 38
  • 47
  • Hello, I try that but nothing happen when insert or unplug usb drive. – Philippe Sep 03 '18 at 14:24
  • I thought you wanted qDebug(). Of, I see: it is not going to Windows console? https://stackoverflow.com/questions/13431734/qt-qdebug-doesnt-work-in-windows-shell – Alexander V Sep 03 '18 at 19:34
  • no I'm not looking to have windows console, I was looking really for qDebug() because if some event happened with this line: qDebug() << QString("Drive %1: Media has arrived").arg(FirstDriveFromMask(lpdbv ->dbcv_unitmask)); I'm supposed to see the device letter on creator application output with when I plug usb device, but nothing, like nothing is detected. – Philippe Sep 03 '18 at 22:49
  • even I never need to implement that to get output, I try that also yes, same result – Philippe Sep 04 '18 at 14:52