如何检测媒体是否已插入可移动驱动器/读卡器

发布于 2024-09-07 19:22:16 字数 101 浏览 4 评论 0原文

我有一个读卡器(未插入记忆棒)。

当我插入计算机时,它在“我的电脑”中显示一个空驱动器...

是否可以知道驱动器是否有媒体(抱歉,我不知道如何称呼它)...

I have a card reader ( no memory stick is inserted ).

When i insert into my compter it shows an empty drive in My Computer...

Is it possible to know whether a drive is having a media ( sorry i am not sure how to call it ) or not...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玻璃人 2024-09-14 19:22:16

我发现 MSalters 使用 IOCTL_STORAGE_CHECK_VERIFY 的建议非常好。 IOCTL_STORAGE_CHECK_VERIFY的使用有一个小技巧。在使用DeviceIoControl函数中的IOCTL代码之前,需要在CreateFile函数中打开相应的设备:

HANDLE hDevice = CreateFile (szDeviceName,  // like "\\.\E:"
                             0,             // no access to the drive
                             FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
                             NULL, OPEN_EXISTING, 0, NULL);

对于DeviceIoControl的使用一可以使用0作为CreateFile的第二个参数,因为我们不会使用ReadFileWriteFile等函数来访问设备。 IOCTL_STORAGE_CHECK_VERIFY 的实现确实遵循一些数据读取请求。因此,为了能够使用 IOCTL_STORAGE_CHECK_VERIFY 而不会出现 ERROR_ACCESS_DENIED (5) 错误,我们必须按如下方式打开设备

HANDLE hDevice = CreateFile (szDeviceName,  // like "\\.\E:"
                             FILE_READ_DATA, // read access to the data
                             FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
                             NULL, OPEN_EXISTING, 0, NULL);

存在另一个版本的 IOCTL_STORAGE_CHECK_VERIFY - IOCTL_STORAGE_CHECK_VERIFY2 其工作方式与 IOCTL_STORAGE_CHECK_VERIFY 完全相同,但速度更快(请参阅 http://msdn.microsoft.com/en-us/library/ff560538.aspx)。要使用 IOCTL_STORAGE_CHECK_VERIFY2,只需使用 FILE_READ_ATTRIBUTES 访问权限即可打开设备:

HANDLE hDevice = CreateFile (szDeviceName,         // like "\\.\E:"
                             FILE_READ_ATTRIBUTES, // read access to the attributes
                             FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
                             NULL, OPEN_EXISTING, 0, NULL);

测试驱动器中介质是否存在的代码如下所示

DWORD cbBytesReturned;
bSuccess = DeviceIoControl (hDevice,                     // device to be queried
                            IOCTL_STORAGE_CHECK_VERIFY2,
                            NULL, 0,                     // no input buffer
                            NULL, 0,                     // no output buffer
                            &cbBytesReturned,            // # bytes returned
                            (LPOVERLAPPED) NULL);        // synchronous I/O
if (bSuccess)
    _tprintf (TEXT("the device media are accessible\n"));
else if (GetLastError() == ERROR_NOT_READY)
    _tprintf (TEXT("the device media are not accessible\n"));

I find the suggestion of MSalters to use IOCTL_STORAGE_CHECK_VERIFY very good. There are a small trick in the usage of IOCTL_STORAGE_CHECK_VERIFY. Before the usage of IOCTL code in the function DeviceIoControl one need to open the corresponding device with respect of CreateFile function:

HANDLE hDevice = CreateFile (szDeviceName,  // like "\\.\E:"
                             0,             // no access to the drive
                             FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
                             NULL, OPEN_EXISTING, 0, NULL);

For the usage of DeviceIoControl one can use 0 as a second parameter of CreateFile, because we will not use ReadFile, WriteFile etc functions to access the device. The implementation of IOCTL_STORAGE_CHECK_VERIFY do follow to some read of data requests. So to be able to use IOCTL_STORAGE_CHECK_VERIFY without having ERROR_ACCESS_DENIED (5) error we have to open the device as following

HANDLE hDevice = CreateFile (szDeviceName,  // like "\\.\E:"
                             FILE_READ_DATA, // read access to the data
                             FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
                             NULL, OPEN_EXISTING, 0, NULL);

There exist another version of IOCTL_STORAGE_CHECK_VERIFY - IOCTL_STORAGE_CHECK_VERIFY2 which works absolutely the same as IOCTL_STORAGE_CHECK_VERIFY but much more quickly (see http://msdn.microsoft.com/en-us/library/ff560538.aspx). To use IOCTL_STORAGE_CHECK_VERIFY2 one can open device with only FILE_READ_ATTRIBUTES access:

HANDLE hDevice = CreateFile (szDeviceName,         // like "\\.\E:"
                             FILE_READ_ATTRIBUTES, // read access to the attributes
                             FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
                             NULL, OPEN_EXISTING, 0, NULL);

The code which test the existence of the media in the drive can look like following

DWORD cbBytesReturned;
bSuccess = DeviceIoControl (hDevice,                     // device to be queried
                            IOCTL_STORAGE_CHECK_VERIFY2,
                            NULL, 0,                     // no input buffer
                            NULL, 0,                     // no output buffer
                            &cbBytesReturned,            // # bytes returned
                            (LPOVERLAPPED) NULL);        // synchronous I/O
if (bSuccess)
    _tprintf (TEXT("the device media are accessible\n"));
else if (GetLastError() == ERROR_NOT_READY)
    _tprintf (TEXT("the device media are not accessible\n"));
暮凉 2024-09-14 19:22:16

这种行为的原因是历史性的,可以追溯到软盘驱动器和 MS-DOS。即使其中没​​有软盘,A: 驱动器仍然是 A: 驱动器。

有时可以检查带有可移动介质的驱动器是否为空。读卡器和 CD 驱动器通常支持此功能,而软盘驱动器则不支持。您可以向驱动器发送 IOCTL_STORAGE_CHECK_VERIFY

The reason for this behavior is historical, and dates back to floppy drives and MS-DOS. The A: drive would still be the A: drive even if there was no floppy in it.

It is sometimes possible to check whether a drive with removable media is empty. Card readers and CD drives usually support this, floppy drives don't. You would send the drive a IOCTL_STORAGE_CHECK_VERIFY .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文