Chinaunix首页 | 论坛 | 博客
  • 博客访问: 631239
  • 博文数量: 138
  • 博客积分: 3067
  • 博客等级: 中校
  • 技术积分: 1565
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-21 12:44
文章分类

全部博文(138)

文章存档

2016年(5)

2014年(4)

2012年(1)

2011年(2)

2010年(10)

2009年(19)

2008年(97)

我的朋友

分类:

2008-07-29 15:29:42

I use DeviceIoControl() to get SCSI info recently, whcih related with a device[here a disk volume]



typedef struct _SCSI_ADDRESS {
  ULONG  Length;
  UCHAR  PortNumber;
  UCHAR  PathId;
  UCHAR  TargetId;
  UCHAR  Lun;
}SCSI_ADDRESS, *PSCSI_ADDRESS;

you can use DeviceIoControl with parameter IOCTL_SCSI_GET_ADDRESS get SCSI_ADDRESS.




// code:

void get_scsi_info(const std::string &strVolume)
{
    std::string deviceName = "\\\\.\\" + strVolume;

    // Opening a handle to the disk

    HANDLE hDiskDevice = CreateFile(
        deviceName.c_str(),
        GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess

        FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode

        NULL, // lpSecurityAttributes

        OPEN_EXISTING, // dwCreationDistribution

        0, // dwFlagsAndAttributes

        NULL // hTemplateFile

        );

    if (hDiskDevice == INVALID_HANDLE_VALUE)
    {
        wprintf(L"\nERROR: Cannot open device '%s'. "
            L"Did you specified a correct SCSI device? "
            L"[CreateFile() error: %d]\n",
            deviceName.c_str(), GetLastError());
        return;
    }

    PSCSI_ADDRESS                        SCSIAddress;
        BOOL status;
        ULONG returnedLength;

    SCSIAddress = (PSCSI_ADDRESS) malloc(SCSI_INFO_BUFFER_SIZE) ;
    if ( SCSIAddress == NULL) {
        wprintf(L"Error in allocating memory\n");
        CloseHandle ( hDiskDevice );    
        return;
    }

    // Get the SCSI inquiry data for all devices for the given SCSI bus

    status = DeviceIoControl(    
                            hDiskDevice,
                            IOCTL_SCSI_GET_ADDRESS,
                            NULL,
                            0,
                            SCSIAddress,
                            SCSI_INFO_BUFFER_SIZE,
                            &returnedLength,
                            NULL );

    if ( !status ) {
        wprintf(L"Error in IOCTL_SCSI_GET_INQUIRY_DATA\n" );
        free( SCSIAddress );
        CloseHandle ( hDiskDevice );    
        return;
    }

    char buffer[128];
    sprintf(buffer, "%d %d %d\n",
        SCSIAddress->PortNumber,
        SCSIAddress->TargetId,
        SCSIAddress->Lun);
    printf("%s", buffer);
    
    free(SCSIAddress);
    CloseHandle(hDiskDevice);
    
    return;
}

阅读(2192) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~