qt - 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' );
}
解决方案
要么:
#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
推荐阅读
- reactjs - Graphql调用axios多次调用性能/重构
- python - 如何使用名称将工作分配给特定集群
- amazon-web-services - EC2 连接超时:ec2 错误:异步完成但错误:10061,
- php - 如果产品已经在购物篮中,我正在尝试从单个页面和产品上的“添加到购物车”按钮更改链接和文本
- sql-server - 存储具有不同小数位数的十进制数的最佳方法
- php - 两列 where 子句用逗号合并
- scala - 如何将scala中的类转换为代数数据类型?
- c++ - 使用函数参数、指针和正确的转换来理解 Boost::any
- logging - Kusto - 嵌套分组
- javascript - 如何在Angular中获取上一个/下一个数组字符串