获取物理磁盘物理索引号

int GetPhysicalDriveIndex(char cLable)
{
    char szDriverName[64] = "\\\\.\\C:\0";  
    szDriverName[4] = cLable;
    int PhysicalDriveNum = -1;
    HANDLE hDev = CreateFile(szDriverName,
        0,
        FILE_SHARE_READ|FILE_SHARE_WRITE,
        NULL,
        OPEN_EXISTING,
        0,
        NULL);
    if (hDev == INVALID_HANDLE_VALUE)
    {
        return PhysicalDriveNum;
    }

    STORAGE_DEVICE_NUMBER sdn;
    DWORD dwBytesRetuned=0;

    long res = DeviceIoControl(hDev,
        IOCTL_STORAGE_GET_DEVICE_NUMBER,
        NULL,
        0,
        &sdn,sizeof(sdn),
        &dwBytesRetuned,
        NULL);
    if(res)
    {
        PhysicalDriveNum = (int)sdn.DeviceNumber;
    }
    CloseHandle(hDev);

    return PhysicalDriveNum;
}

你可能感兴趣的:(获取物理磁盘物理索引号)