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) |