#define IOCTL_STORAGE_QUERY_PROPERTY 0x2D1400
//// 查询属性输出的数据结构
// typedef struct _STORAGE_DEVICE_DESCRIPTOR {
// ULONG Version; // 版本
// ULONG Size; // 结构大小
// UCHAR DeviceType; // 设备类型
// UCHAR DeviceTypeModifier; // SCSI-2额外的设备类型
// BOOLEAN RemovableMedia; // 是否可移动
// BOOLEAN CommandQueueing; // 是否支持命令队列
// ULONG VendorIdOffset; // 厂家设定值的偏移
// ULONG ProductIdOffset; // 产品ID的偏移
// ULONG ProductRevisionOffset; // 产品版本的偏移
// ULONG SerialNumberOffset; // 序列号的偏移
// STORAGE_BUS_TYPE BusType; // 总线类型
// ULONG RawPropertiesLength; // 额外的属性数据长度
// UCHAR RawDeviceProperties[1]; // 额外的属性数据(仅定义了象征性的1个字节)
// } STORAGE_DEVICE_DESCRIPTOR, *PSTORAGE_DEVICE_DESCRIPTOR;
// typedef enum _STORAGE_BUS_TYPE {
// BusTypeUnknown = 0x00,
// BusTypeScsi,
// BusTypeAtapi,
// BusTypeAta,
// BusType1394,
// BusTypeSsa,
// BusTypeFibre,
// BusTypeUsb,
// BusTypeRAID,
// BusTypeiScsi,
// BusTypeSas,
// BusTypeSata,
// BusTypeSd,
// BusTypeMmc,
// BusTypeMax,
// BusTypeMaxReserved = 0x7F
// } STORAGE_BUS_TYPE, *PSTORAGE_BUS_TYPE;
char szBusType[][100] =
{
"BusTypeUnknown" ,
"BusTypeScsi",
"BusTypeAtapi",
"BusTypeAta",
"BusType1394",
" BusTypeSsa",
"BusTypeFibre",
"BusTypeUsb",
" BusTypeRAID",
"BusTypeiScsi",
" BusTypeSas",
" BusTypeSata",
" BusTypeSd",
" BusTypeMmc",
" BusTypeMax",
" BusTypeMaxReserved "
};
char * flipAndCodeBytes (const char * str,
int pos,
int flip,
char * buf)
{
int i;
int j = 0;
int k = 0;
buf [0] = '\0';
if (pos <= 0)
return buf;
if ( ! j)
{
char p = 0;
// First try to gather all characters representing hex digits only.
j = 1;
k = 0;
buf[k] = 0;
for (i = pos; j && str[i] != '\0'; ++i)
{
char c = tolower(str[i]);
if (isspace(c))
c = '0';
++p;
buf[k] <<= 4;
if (c >= '0' && c <= '9')
buf[k] |= (unsigned char) (c - '0');
else if (c >= 'a' && c <= 'f')
buf[k] |= (unsigned char) (c - 'a' + 10);
else
{
j = 0;
break;
}
if (p == 2)
{
if (buf[k] != '\0' && ! isprint(buf[k]))
{
j = 0;
break;
}
++k;
p = 0;
buf[k] = 0;
}
}
}
if ( ! j)
{
// There are non-digit characters, gather them as is.
j = 1;
k = 0;
for (i = pos; j && str[i] != '\0'; ++i)
{
char c = str[i];
if ( ! isprint(c))
{
j = 0;
break;
}
buf[k++] = c;
}
}
if ( ! j)
{
// The characters are not there or are not printable.
k = 0;
}
buf[k] = '\0';
if (flip)
// Flip adjacent characters
for (j = 0; j < k; j += 2)
{
char t = buf[j];
buf[j] = buf[j + 1];
buf[j + 1] = t;
}
// Trim any beginning and end space
i = j = -1;
for (k = 0; buf[k] != '\0'; ++k)
{
if (! isspace(buf[k]))
{
if (i < 0)
i = k;
j = k;
}
}
if ((i >= 0) && (j >= 0))
{
for (k = i; (k <= j) && (buf[k] != '\0'); ++k)
buf[k - i] = buf[k];
buf[k - i] = '\0';
}
return buf;
}
void getdisknum(){
try
{
for(int i = 0; i < 26; i++)
{
char * lpMem = new char[559];
if (lpMem == NULL)
{continue;}
ZeroMemory(lpMem, 559);
PSTORAGE_DEVICE_DESCRIPTOR sdn = (PSTORAGE_DEVICE_DESCRIPTOR)lpMem;
char* lpbuffer = "S2R7NXAH204756V";
CHAR szPhysicalDiskNum[MAX_PATH] = {0};
sprintf_s(szPhysicalDiskNum, _T("\\\\.\\PhysicalDrive%d"), i);
HANDLE hFile = CreateFile(szPhysicalDiskNum, 0, 0, 0, 1u, 0, 0);
STORAGE_PROPERTY_QUERY query ;
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;
if (hFile != INVALID_HANDLE_VALUE)
{
DWORD r = 0;
DWORD c = sizeof(sdn);
BOOL re = DeviceIoControl(hFile, 0x2D1400, &query,sizeof(query), descrip, MEMSIZE, &r,NULL);
if (descrip->BusType == BusTypeUsb ||
descrip->BusType == BusTypeUnknown)
{
continue;
}
CHAR szSerialNum[MAX_PATH] = {0};
char serialNumber [1000];
char modelNumber [1000];
char vendorId [1000];
char productRevision [1000];
flipAndCodeBytes (lpMem,
descrip -> VendorIdOffset,
0, vendorId );
flipAndCodeBytes (lpMem,
descrip -> ProductIdOffset,
0, modelNumber );
flipAndCodeBytes (lpMem,
descrip -> ProductRevisionOffset,
0, productRevision );
flipAndCodeBytes (lpMem,
descrip -> SerialNumberOffset,
1, serialNumber );
if (szSerialNum[1] == 0X30)
{
continue;
}
string s = serialNumber;
s += " 硬盘类型 ";
s += szBusType[descrip->BusType];
m_List.InsertString(m_List.GetCount(), s.data() );
delete[] lpMem;
lpMem = NULL;
CloseHandle(hFile);
}
}
}
catch(...){
}
}