获取物理磁盘路径 [英] Get Physical Disk Paths

查看:23
本文介绍了获取物理磁盘路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取 Windows 下所有物理磁盘的列表并链接到 这个 昨天的问题确实有效,但它似乎没有找到我未格式化的硬盘(虽然我认为这没什么区别,但磁盘是通过 USB 连接的).

I'm wanting to get a list of all physical disks under Windows and was linked to this question yesterday which does work, but it doesn't seem to find my unformatted hard drive (and although I don't think it makes a difference, the disk is connected via USB).

是否有其他解决方案可以为所有连接的硬盘获取\\.\PhysicalDrive"名称?

Is there another solution to getting the "\\.\PhysicalDrive" name for ALL connected hard disks?

推荐答案

对于在以后的搜索中可能遇到此问题的任何人,以下是我最终想到的.请注意,有一些未定义的类型是外部库的一部分或由我定义,而不是 Windows 的一部分.例如nowideqDebug

For anyone who may come across this in future searches, here's what I eventually came up with. Note that there are a few undefined types that are a part of external libraries or defined by me, and not a part of Windows. e.g. nowide and qDebug

评论是,至少可以说,但是交叉引用函数调用的 MSDN 文档应该会让你走上正轨.目标是列出每个磁盘的每个列表,并获取所需的信息,以便我获得设备的 HANDLE,并填充即插即用信息(设备描述/名称).

The commenting is poor to say the least, but cross-referencing MSDN documentation for the function calls should get you on your way. The goal was to list every list every disk, and obtain the required information to both allow me to get a HANDLE for the device, AND populate the Plug and Play information (the device description/name).

typedef struct _DISK_DRIVE_INFORMATION
{
    std::string Path;
    std::string FriendlyName;
} DISK_DRIVE_INFORMATION;

unsigned i;
DWORD dwSize, dwPropertyRegDataType = SPDRP_PHYSICAL_DEVICE_OBJECT_NAME;
CONFIGRET r;
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
SP_DEVICE_INTERFACE_DATA interfaceData;

TCHAR szDeviceInstanceID [MAX_DEVICE_ID_LEN];
TCHAR szDesc[1024];

GUID HddClass;
HddClass = GUID_DEVINTERFACE_DISK;//GUID_DEVCLASS_DISKDRIVE;

// List all connected disk drives
hDevInfo = SetupDiGetClassDevs (&HddClass, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hDevInfo == INVALID_HANDLE_VALUE)
    return;

// Find the ones that are driverless
for (i = 0; ; i++)
{
    DeviceInfoData.cbSize = sizeof (DeviceInfoData);
    // Get the next device info
    if (!SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData))
        break;
    interfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
    // Get the next device interface
    if (!SetupDiEnumInterfaceDevice(hDevInfo, NULL, &HddClass, i, &interfaceData))
    {
        break;
    }

    // Get the device ID
    r = CM_Get_Device_ID(DeviceInfoData.DevInst, szDeviceInstanceID , MAX_PATH, 0);
    if (r != CR_SUCCESS)
        continue;

    // To add to the vector
    DISK_DRIVE_INFORMATION AddToVector;

    DWORD requiredSize = 0;

    // Get the path
    SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, NULL, NULL, &requiredSize, NULL);
    SP_INTERFACE_DEVICE_DETAIL_DATA* data = (SP_INTERFACE_DEVICE_DETAIL_DATA*) malloc(requiredSize);

    data->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);


    if (!SetupDiGetDeviceInterfaceDetail(hDevInfo, &interfaceData, data, requiredSize, NULL, NULL))
    {
        continue;
    }

    AddToVector.Path = nowide::convert(std::wstring(data->DevicePath));
    qDebug("Disk path: %s", AddToVector.Path.c_str());

    // Friendly name (e.g. SanDisk Cruzer USB...)
    SetupDiGetDeviceRegistryProperty (hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME,
                                      &dwPropertyRegDataType, (BYTE*)szDesc,
                                      sizeof(szDesc),   // The size, in bytes
                                      &dwSize);
    AddToVector.FriendlyName = nowide::convert(std::wstring((TCHAR*)szDesc));
    qDebug("Friendly name: %s", AddToVector.FriendlyName.c_str());

    OutVector.push_back(AddToVector);
    delete data;
}

这篇关于获取物理磁盘路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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