Windows下判断U盘插入并取出盘符路径的代码。
#include
#include
char FirstDriveFromMask(ULONG unitmask)
{
char i;
for (i = 0; i < 26; ++i)
{
if
(unitmask & 0x1)
break;
unitmask =
unitmask >> 1;
}
return (i + 'A');
}
bool
Application::winEventFilter(MSG *msg, long *result)
{
bool ret
= false;
if (msg->message != WM_DEVICECHANGE)
return ret;
PDEV_BROADCAST_HDR lpdb =
(PDEV_BROADCAST_HDR)msg->lParam;
static int iCnt = 0;
char szDrivePath[3];
switch (msg->wParam)
{
case DBT_DEVICEARRIVAL:
if (lpdb->dbch_devicetype ==
DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME
lpdbv = (PDEV_BROADCAST_VOLUME)lpdb;
//if
(lpdbv->dbcv_flags & DBTF_MEDIA)
{
sprintf(szDrivePath, "%c:\\",
FirstDriveFromMask(lpdbv->dbcv_unitmask));
const
wchar_t *path = QString(szDrivePath).toStdWString().c_str();
if (szDrivePath[0] > 'B'
&&
GetDriveType(path) == DRIVE_REMOVABLE)
{
iCnt++;
if (iCnt%3 == 0)
{
qDebug() << "Incoming: "
<< szDrivePath;
ret = true;
}
}
}
}
break;
case DBT_DEVICEREMOVECOMPLETE:
break;
default:;
}
return ret;
}
阅读(2722) | 评论(0) | 转发(0) |