首页 > 解决方案 > qDebug 函数 char FirstDriveFromMask( ULONG unitmask )

问题描述

尝试在 QT 中制作项目,我需要检测任何新的 USB 设备并在我的 main.cpp 中返回该字母。

我在谷歌上找到了这个,它应该可以工作,但我不知道如何通过调用函数 char FirstDriveFromMask(ULONG unitmask) 来使用简单的 qDebug() 在我的 main.cpp 中打印驱动程序字母。

你可以帮帮我吗?

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' );
}

标签: qtchardeviceqdebug

解决方案


要么:

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

或使用更好的格式

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

如果该输出进入默认调试控制台而不是 Windows,您必须遵循以下答案:Qt qDebug() 在 Windows shell 中不起作用并在 project.pro 文件中进行小幅更改:

CONFIG += console


推荐阅读