WIN32磁盘操作代码
+ -

使用IOCTL_SCSI_PASS_THROUGH_DIRECT获取硬盘的名称

2021-09-07 206 0

Windows下的物理磁盘名称被命名为PhysicalDrive0~N
可以通过IOCTL_SCSI_PASS_THROUGH_DIRECT获取该磁盘的名称

BOOL CDisk::GetDiskNameById(int nId, TCHAR* pName)
{
    TCHAR buff[512];
    _stprintf_s(buff, TEXT("\\\\.\\PhysicalDrive%d"), nId);
    HANDLE hDevice = CreateFile(buff, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
    if (hDevice == INVALID_HANDLE_VALUE)
    {
        return FALSE;
    }


#define IOCTL_SCSI_BASE                 FILE_DEVICE_CONTROLLER
#define IOCTL_SCSI_PASS_THROUGH_DIRECT  CTL_CODE(IOCTL_SCSI_BASE, 0x0405, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
    typedef struct _SCSI_PASS_THROUGH_DIRECT {
        USHORT  Length;
        UCHAR  ScsiStatus;
        UCHAR  PathId;
        UCHAR  TargetId;
        UCHAR  Lun;
        UCHAR  CdbLength;
        UCHAR  SenseInfoLength;
        UCHAR  DataIn;
        ULONG  DataTransferLength;
        ULONG  TimeOutValue;
        PVOID  DataBuffer;
        ULONG  SenseInfoOffset;
        UCHAR  Cdb[16];
    }SCSI_PASS_THROUGH_DIRECT, *PSCSI_PASS_THROUGH_DIRECT;

    SCSI_PASS_THROUGH_DIRECT sc;
    memset(&sc, 0, sizeof(SCSI_PASS_THROUGH_DIRECT));
    sc.Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
    sc.CdbLength = 16;
    sc.DataIn = 1;
    sc.DataTransferLength = 64;
    sc.TimeOutValue = 2;
    sc.Cdb[0] = 0x12;
    sc.Cdb[4] = 0x40;
    sc.DataBuffer = (PVOID)buff;

    ULONG d;
    memset(buff, 0, sizeof(buff));
    BOOL ok = DeviceIoControl(hDevice, IOCTL_SCSI_PASS_THROUGH_DIRECT, &sc, sizeof(sc), &sc, sizeof(sc), &d, NULL);
    if (!ok)
    {
        CloseHandle(hDevice);
        return FALSE;
    }

    CHAR* p = (CHAR*)buff + 8;
    for (unsigned int i = 0; i < strlen(p); i++)
    {
        pName[i] = p[i];
    }

    CloseHandle(hDevice);


    return TRUE;
}

0 篇笔记 写笔记

使用IOCTL_DISK_GET_LENGTH_INFO获取硬盘大小
使用IOCTL_DISK_GET_LENGTH_INFO获取硬盘大小char buff[512]; sprintf(buff,"\.\PhysicalDrive%d",nId); HANDLE hDevice = CreateFile(buff,GENE......
使用FAT32一键格式化硬盘
BOOL CKDiskInitDlg::FormatDisk(){ CComboBox* pList =(CComboBox*)GetDlgItem(IDC_COMBO_DISK_LIST); int nId = pList->GetCurSel(); char ......
使用IOCTL_SCSI_PASS_THROUGH_DIRECT获取硬盘的名称
Windows下的物理磁盘名称被命名为PhysicalDrive0~N可以通过IOCTL_SCSI_PASS_THROUGH_DIRECT获取该磁盘的名称BOOL CDisk::GetDiskNameById(int nId, TCHAR* pName){ TCHAR buff[512]......
使用IOCTL_DISK_GET_DRIVE_GEOMETRY获取硬盘的布局
typedef struct _DISK_GEOMETRY { LARGE_INTEGER Cylinders; MEDIA_TYPE MediaType; ULONG TracksPerCylinder; ULONG SectorsPerTra......
作者信息
我爱内核
Windows驱动开发,网站开发
好好学习,天天向上。
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!