简体   繁体   English

Qt安全地删除设备

[英]Qt safely remove a device

I'm look for a way to safely remove my USB key from my Qt 5.2 application but I can't find any Qt module to do that. 我正在寻找一种方法从我的Qt 5.2应用程序安全地删除我的USB密钥,但我找不到任何Qt模块来做到这一点。

Is their a way to do it or do I have to hardcode it ? 他们是这样做的方式还是我必须硬编码?

I've never removed USB using Qt but this simple c code will also work. 我从未使用Qt删除USB,但这个简单的c代码也可以使用。

#include <sys/mount.h>

int umount(const char *target);

If you mean unmounting your usb device by removal, then there is no cross-platform solution for this. 如果您的意思是通过删除来卸载USB设备,那么就没有跨平台的解决方案。 Perhaps there could be added something in the QtSystems module, however the problem is that this would require admin permission or some tricks, eg setuid or caps on Linux and so on. 也许在QtSystems模块中可能会添加一些东西,但问题是这需要管理员权限或一些技巧,例如Linux上的setuid或caps等等。

You could do something along these lines to achieve this feature for now on your side: 您现在可以在这些方面做一些事情来实现此功能:

void MyClass::unmount() {
#ifdef Q_OS_LINUX
    // See details: http://linux.die.net/man/2/umount
    if (umount(myUsbKeyPath) < 0)
        qDebug() << "Failed to umount";
#elif Q_OS_WIN
    // See details: http://support.microsoft.com/default.aspx?scid=kb;en-us;165721
    DWORD dwBytesReturned;
    DeviceIoControl(hVolume,
                    IOCTL_STORAGE_EJECT_MEDIA,
                    NULL, 0,
                    NULL, 0,
                    &dwBytesReturned,
                    NULL);
#endif
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM