gpt4 book ai didi

c++ Qt/Win32 - USB 以编程方式弹出,但仍显示在 QFileSystemModel 和 GetLogicalDrives() 中

转载 作者:太空宇宙 更新时间:2023-11-04 13:37:26 24 4
gpt4 key购买 nike

我有一个用 QFileSystemModel 填充的 QTreeView,我将根路径设置为“\”(如“\\”)。这很好用,可以看到本地驱动器和 USB 驱动器。我的问题是,当我使用 DeviceIoControl(..., FSCTL_DISMOUNT_VOLUME, ...) 以编程方式弹出 USB 驱动器时(我正在为 kiosk 模式下的 Win7 应用程序编程,因此它需要处理 Windows 级别的事件),然后通过 DeviceIoControl(..., IOCTL_STORAGE_EJECT_MEDIA, ...),虽然两个函数都返回 true,并且 Windows 系统服务托盘没有显示 USB 图标,Windows 资源管理器没有 USB 条目,但我的 QFileSystemModel 仍然看到驱动器。

我确实有一个派生自 QSortFilterProxyModel 的代理模型,它正在过滤所看到的内容,因为我真的只想看到 USB 驱动器。这正在调用我的 USBController 类来检测已安装的可移动设备。该函数调用 GetLogicalDrives(),它实际上返回我刚刚以编程方式弹出的 USB 的驱动器号。

我四处寻找解决这个难题的办法,但还没有想出这个办法。有没有人有什么建议?这是相关代码:

来自 UsbController.h 的函数:

bool UsbController::ejectDrive(char driveletter, QString &errmsg)
{
char devicepath[7];
char format[] = "\\\\.\\?:";
strcpy_s(devicepath, format);
devicepath[4] = driveletter;
errmsg = "";

DWORD dwRet = 0;
wchar_t wtext[7];
size_t textlen = 7;
mbstowcs_s(&textlen, wtext, devicepath,strlen(devicepath)+1);
HANDLE hVol = CreateFile(wtext, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hVol == INVALID_HANDLE_VALUE)
{
FormatErrorMsg("CreateFile: ", errmsg);
return false;
}

if(!DeviceIoControl(hVol, FSCTL_LOCK_VOLUME, 0, 0, 0, 0, &dwRet, 0))
{
FormatErrorMsg("Lock Volume: ", errmsg);
return false;
}

if(!DeviceIoControl(hVol, FSCTL_DISMOUNT_VOLUME, 0, 0, 0, 0, &dwRet, 0))
{
FormatErrorMsg("Dismount Volume: ", errmsg);
return false;
}

if (!DeviceIoControl(hVol, IOCTL_STORAGE_EJECT_MEDIA, 0, 0, 0, 0, &dwRet, 0))
{
FormatErrorMsg("Eject Media: ", errmsg);
return false;
}

CloseHandle(hVol);
return true;
}

QMap<QString,QString> UsbController::getMountedRemovables()
{
DWORD test = GetLogicalDrives();
DWORD mask = 1;
UINT type = 0;
WCHAR wdrive[] = L"?:\\"; // use as a drive letter template

QMap<QString,QString> removables;
for (int i = 0; i < 32; i++)
{
if (test & mask)
{
wdrive[0] = (char)('A' + i); // change letter in template
type = GetDriveType(wdrive);
switch (type) {
case DRIVE_REMOVABLE:
{
QString qdrive = QString((char)('A' + i)) + ":";
if (!removables.contains(qdrive))
{
QString name = mountNameFromDriveLetter((char)('A' + i));
removables.insert(qdrive, name);
}
break;
}
default: break;
}
}
mask = mask << 1;
}

return removables;
}

我的代理模型中的函数(数据使用 acceptedRemovables QMap,它通过调用 UsbController 在 detectMountedRemovables() 中填充:

QVariant DriveFilterProxyModel::data(const QModelIndex & index, int role) const
{
QVariant data = QSortFilterProxyModel::data(index, role);

if (role == Qt::DisplayRole)
{
QString source = data.toString();

for (int i = 0; i < acceptList.size(); i++)
{
if (source.contains(acceptList[i]))
{
QString drivestring = source;
if (drivestring.contains('('))
{
drivestring = drivestring.remove(0, drivestring.indexOf('(')+1);
drivestring = drivestring.remove(drivestring.indexOf(')'), drivestring.size()-drivestring.indexOf(')')+1);
}
QMap<QString,QString>::const_iterator iter = acceptedNonRemovables.find(drivestring);
if (iter != acceptedNonRemovables.end())
{
if (source.contains("Data")) // no path as yet applied
{
QString newsource(source);
newsource.replace("Data", iter.value());
qDebug() << "DriveFilterProxyModel::filterAcceptsRow(): Mapped drive: should replace \"Data\" with: " << iter.value() << " to produce: " << newsource;
return newsource;
}
}
else
{
iter = acceptedRemovables.find(drivestring);
if (iter != acceptedRemovables.end())
{
if (!source.contains('(')) // no drive name found
{
QString newsource = QString("Removable Disk (%1)").arg(source);
qDebug() << "DriveFilterProxyModel::filterAcceptsRow(): USB drive: should change simple drive letter to: " << newsource;
return newsource;
}
}
}
}
}
}

return data;
}

void DriveFilterProxyModel::detectMountedRemovables()
{
acceptedRemovables = UsbController::getMountedRemovables();
resetAcceptList();
}

最佳答案

我猜系统仍然可以识别驱动器,但它仍处于弹出模式。

查看Enumerating all available drive letters in Windows问题可能是您在那里找到了可行的解决方案。

关于c++ Qt/Win32 - USB 以编程方式弹出,但仍显示在 QFileSystemModel 和 GetLogicalDrives() 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29102553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com