将特定SCSI命令发送到Windows中的SCSI设备

Windows是否具有特定的接口,我可以通过它发送特定的scsi命令,例如查询到scsi设备?我在网上搜索,发现传递了对SCSI Pass Through接口的引用.但它很模糊.

是否有关于如何使用它的API的文档?

最佳答案

#include <iostream>
#include <windows.h>
#include <winioctl.h>
#define ULONG_PTR ULONG
#include <ntddscsi.h> //from SDK
#include <spti.h>      //from DDK 
using namespace std;

int demo()
{
    HANDLE hDisk;
    SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER sptdwb; 
    ULONG length = 0;
    DWORD bytesReturn;
    BYTE bufDataRead[64*1024+10];
    int iRet;        

    hDisk = CreateFile(path,GENERIC_READ | GENERIC_WRITE,     
            FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,OPEN_EXISTING,0,NULL                                 
            );
    if (hDisk ==INVALID_HANDLE_VALUE)  {              
          return 0;
    }
    ZeroMemory(&sptdwb, sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER));
    sptdwb.sptd.Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
    sptdwb.sptd.PathId = 0;
    sptdwb.sptd.TargetId = 1;
    sptdwb.sptd.Lun = 0;
    sptdwb.sptd.CdbLength = 6;
    sptdwb.sptd.DataIn = SCSI_IOCTL_DATA_IN;
    sptdwb.sptd.SenseInfoLength = 24;
    sptdwb.sptd.DataTransferLength = 8; 
    sptdwb.sptd.TimeOutValue = 2;
    sptdwb.sptd.DataBuffer = bufDataRead; 
    sptdwb.sptd.SenseInfoOffset = offsetof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER,ucSenseBuf);       
    sptdwb.sptd.Cdb[0] = 0x12;
    sptdwb.sptd.Cdb[1] = 0x00;
    sptdwb.sptd.Cdb[2] = 0x00;
    sptdwb.sptd.Cdb[3] = 0x00;
    sptdwb.sptd.Cdb[4] = 0xFF;
    sptdwb.sptd.Cdb[5] = 0x00;

    length = sizeof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER);
    iRet = DeviceIoControl(hDisk,
            IOCTL_SCSI_PASS_THROUGH_DIRECT,
            &sptdwb,
            length,
            &sptdwb,
            length,
            &bytesReturn,
            NULL);
    if (0 == iRet)  {
        printf("inquiry fail");
        return 0;
    } else {
    //Check returned data in sptdwb.sptd.DataBuffer.
    }       
    return 0;

}

点赞