如何获取物理存储设备列表? [英] How to get a list of physical storage devices?

查看:231
本文介绍了如何获取物理存储设备列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得物理存储设备的列表。

我看过一些代码,但实际上是循环的,并且像暴力一样。

我想知道什么是获取物理存储磁盘列表的一般方式。

I want to get a list of physical storage devices.
I've seen some code but that actually loops and does something like brute force.
I want to know what is the general way of getting the list of physical storage disks.

我找到 CreateFile() 。但我不明白如何正确使用它。我需要一个非wmi解决方案。

I've found CreateFile(). But I cannot understand how to use it properly. I need a non-wmi solution. and it's better if it doesn't query registry.

推荐答案

我使用下面的代码,枚举所有的卷和然后查找其对应的物理驱动器:

I've used the following code, that enumerates all the volumes and then looks for their corresponding physical drives:

#include <windows.h>
#include <commctrl.h>
#include <winioctl.h>

typedef struct _STORAGE_DEVICE_NUMBER {
  DEVICE_TYPE  DeviceType;
  ULONG  DeviceNumber;
  ULONG  PartitionNumber;
} STORAGE_DEVICE_NUMBER, *PSTORAGE_DEVICE_NUMBER;

void PrintVolumes()
{
    char volName[MAX_PATH];
    HANDLE hFVol;
    DWORD bytes;

    hFVol = FindFirstVolume(volName, sizeof(volName));
    if (!hFVol)
    {
        printf("error...\n");
        return;
    }
    do
    {
        size_t len = strlen(volName);
        if (volName[len-1] == '\\')
        {
            volName[len-1] = 0;
            --len;
        }

        /* printf("OpenVol %s\n", volName); */
        HANDLE hVol = CreateFile(volName, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
        if (hVol == INVALID_HANDLE_VALUE)
            continue;

        STORAGE_DEVICE_NUMBER sdn = {0};
        if (!DeviceIoControl(hVol, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL,
                    0, &sdn, sizeof(sdn), &bytes, NULL))
        {
            printf("error...\n");
            continue;
        }
        CloseHandle(hVol);

        printf("Volume Type:%d, Device:%d, Partition:%d\n", (int)sdn.DeviceType, (int)sdn.DeviceNumber, (int)sdn.PartitionNumber);
        /* if (sdn.DeviceType == FILE_DEVICE_DISK)
            printf("\tIs a disk\n");
            */
    } while (FindNextVolume(hFVol, volName, sizeof(volName)));
    FindVolumeClose(hFVol);
}

这篇关于如何获取物理存储设备列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆